Subject: Leading spaces verbatim
Good day to everyone, I'm trying to make a verbatim block of text with copiable leading spaces so that students can just copy my Python code snippets into their IDE. I believe the question has been asked before (though I confess I can't find the right search terms to find the answer again). I get that PDFs are not the optimal way to give code to students (which is why for longer snippets, I just give them the files or I attach them into the PDF), but for short snippets it would be so much simpler... I recently stumbled onto this article: https://www.overleaf.com/latex/examples/using-luatex-to-convert-interword-gl... wherein the author replaces the TeX glue with spaces and kerning. I was wondering whether the same idea could be applied within the typing environment but making a primitive verbatim environment using \obeylines and \obeyspaces did not result in copiable spaces. Unfortunately, whilst I have some concepts of how luatex works on the input file, the step wherein a pdf is formed is fairly misterious to me, meaning I can't really understand the Lua code from said article (https://gist.github.com/Semptum/076cca71bc251ebe19ff96c5effca3cb). Hence, my three questions: - Is it even realistic to make such a typing environment? - What would be the best path to do so? - Can someone recommend some reading material specifically about this kind of typographical adjustments using LuaTex? Stuff like adjusting the glyphs's position, replacing characters with combined characters etc... Sorry if that's a bit vague, as you can see from the rest of the message, I never really went too deep into the later stages of the TeX pipeline. Best regards, Andre
On 1/28/23 03:01, Kalouguine Andre via ntg-context wrote:
Good day to everyone,
I'm trying to make a verbatim block of text with copiable leading spaces so that students can just copy my Python code snippets into their IDE.
Hi André, I wonder whether I’m getting your point with the following sample: \showframe \setupinteraction[state=start] \starttext \startbuffer[abc] def foo(x): if x == 0: bar() else: baz(x) foo(x - 1) \stopbuffer \comment[location=leftmargin, space=yes, buffer=abc, title={Python code}] {}\typebuffer[abc] \stoptext This might solve what you need. At least, this is the approach that I use to provide text that can be directly copied. I’m on Linux and last time I tested on Windows there were extra lines in the comments. Just in case it might help, Pablo
Hi Pablo,
\showframe \setupinteraction[state=start] \starttext \startbuffer[abc] def foo(x): if x == 0: bar() else: baz(x) foo(x - 1) \stopbuffer \comment[location=leftmargin, space=yes, buffer=abc, title={Python code}] {}\typebuffer[abc] \stoptext
This might solve what you need. At least, this is the approach that I use to provide text that can be directly copied.
I'm on Linux and last time I tested on Windows there were extra lines in the comments.
That's actually something I had never thought of, using PDF comments, thanks!. It's a really nice approach, though weirdly it doesn't work on Okular (Linux+KDE) for me, even though attachments worked fine. I'll try and see why that is. What I had in mind was rather the following: \starttext \starttyping[space=on] def foo(x): y = bar(x) if y: return x return y foo(1) \stoptyping \stoptext but with ordinary char32 spaces rather than \textcontrolspace characters. Best regards,
On 1/28/23 14:31, Kalouguine Andre via ntg-context wrote:
Hi Pablo,
Hi André,
[...] That's actually something I had never thought of, using PDF comments, thanks!.
When I faced a similar task, I thought it could be the way of solving the issue (how to copy and paste ConTeXt code and console commands).
It's a really nice approach, though weirdly it doesn't work on Okular (Linux+KDE) for me, even though attachments worked fine. I'll try and see why that is. Using Fedora 37 (and having just installed Okular), I see that annotations are tricky to open in Okular.
First you have to select the Annotations tab on the left pane, go to the annotation and then right-click on it to open (the pop-up note). Evince has a more effective approach: double-clicking on the icon opens the note (afaIk, this is similar to Acrobat). I hope this helps, Pablo
On 28.01.2023 17:37, Pablo Rodriguez via ntg-context wrote:
Using Fedora 37 (and having just installed Okular), I see that annotations are tricky to open in Okular.
First you have to select the Annotations tab on the left pane, go to the annotation and then right-click on it to open (the pop-up note).
Evince has a more effective approach: double-clicking on the icon opens the note (afaIk, this is similar to Acrobat).
Right you are, it works with your method, thanks! I think the students have Evince so perhaps it's the best solution in the absence of copiable spaces in verbatim mode. Thanks again! Best regards, Andre
On 28.01.2023 17:37, Pablo Rodriguez via ntg-context wrote:
Using Fedora 37 (and having just installed Okular), I see that annotations are tricky to open in Okular.
First you have to select the Annotations tab on the left pane, go to the annotation and then right-click on it to open (the pop-up note).
Evince has a more effective approach: double-clicking on the icon opens the note (afaIk, this is similar to Acrobat). Right you are, it works with your method, thanks! I think the students have Evince so perhaps it's the best solution in the absence
On 1/28/2023 9:26 PM, Kalouguine Andre via ntg-context wrote: there are some provisions for spaces in text but for verbatim it is a bit more tricky \setuptyping[space=on] you now see visual spaces show up that can be copied now, say that we add real spaces, which you can do after and this (in cont-new.mkxl): \unprotect \setvalue{\??typingspace\v!character}{\chardef\obeyedspace32 } \protect and then: \setuptyping[space=character] in sumatra pdf: default : spaces are copied as spaces (so three become three on : visual spaces for every one character : funny newlines when more than two spaces Cheats that add some kerning before and after confuse viewers even more. so: this is all very viewer dependent! (The suggested comment method is more reliable.) Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
On 1/28/2023 9:26 PM, Kalouguine Andre via ntg-context wrote: On 28.01.2023 17:37, Pablo Rodriguez via ntg-context wrote:
Using Fedora 37 (and having just installed Okular), I see that annotations are tricky to open in Okular.
First you have to select the Annotations tab on the left pane, go to the annotation and then right-click on it to open (the pop-up note).
Evince has a more effective approach: double-clicking on the icon opens the note (afaIk, this is similar to Acrobat). Right you are, it works with your method, thanks! I think the students have Evince so perhaps it's the best solution in the absence
On 28.01.2023 22:54, Hans Hagen via ntg-context wrote: there are some provisions for spaces in text but for verbatim it is a bit more tricky \setuptyping[space=on] you now see visual spaces show up that can be copied now, say that we add real spaces, which you can do after and this (in cont-new.mkxl): \unprotect \setvalue{\??typingspace\v!character}{\chardef\obeyedspace32 } \protect and then: \setuptyping[space=character] in sumatra pdf: default : spaces are copied as spaces (so three become three on : visual spaces for every one character : funny newlines when more than two spaces OK, I see, thanks for the explanation! Indeed, when I look at the makeup, there is no glue there to offset the line. But in Okular it just ignores those spaces.
Cheats that add some kerning before and after confuse viewers even more.
so: this is all very viewer dependent! (The suggested comment method is more reliable.)
Hans
Unfortunately as I just found out, comments are also viewer dependent... For instance in Firefox, indentations are lost. So for now, attached files for longer snippets and visual spaces with a string replacement operation afterwards are the way to go it seems. Best regards, Andre
On 1/29/23 17:58, Kalouguine Andre via ntg-context wrote:
On 28.01.2023 22:54, Hans Hagen via ntg-context wrote:
[…] so: this is all very viewer dependent! (The suggested comment method is more reliable.) Unfortunately as I just found out, comments are also viewer dependent... For instance in Firefox, indentations are lost.
Hi André, as far as I know, this may be a bug in PDF.js. Could you report it to the developers (https://github.com/mozilla/pdf.js)? Many thanks for your help, Pablo
Am Sat, 28 Jan 2023 03:01:24 +0100 schrieb Kalouguine Andre via ntg-context:
I'm trying to make a verbatim block of text with copiable leading spaces so that students can just copy my Python code snippets into their IDE.
This basically doesn't work (quite unrelated to the tex code). The PDF viewers have all their own mind here and none of them is really "code friendly". See e.g. https://tex.stackexchange.com/a/472112/2388 It works sometimes a bit better if the PDF is tagged, but there too you can't currently rely on four spaces staying four spaces (and not all PDF reader understand tagging). Better attach files with code. -- Ulrike Fischer http://www.troubleshooting-tex.de/
participants (4)
-
Hans Hagen
-
Kalouguine Andre
-
Pablo Rodriguez
-
Ulrike Fischer