ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Subject: Leading spaces verbatim
@ 2023-01-28  2:01 Kalouguine Andre via ntg-context
  2023-01-28 12:55 ` Pablo Rodriguez via ntg-context
  2023-01-30 16:30 ` Ulrike Fischer via ntg-context
  0 siblings, 2 replies; 9+ messages in thread
From: Kalouguine Andre via ntg-context @ 2023-01-28  2:01 UTC (permalink / raw)
  To: ntg-context; +Cc: Kalouguine Andre


[-- Attachment #1.1: Type: text/plain, Size: 1715 bytes --]

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-glue-to-spaces-and-kerns/sfdkdkybrvkv
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

[-- Attachment #1.2: Type: text/html, Size: 2333 bytes --]

[-- Attachment #2: Type: text/plain, Size: 496 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Subject: Leading spaces verbatim
  2023-01-28  2:01 Subject: Leading spaces verbatim Kalouguine Andre via ntg-context
@ 2023-01-28 12:55 ` Pablo Rodriguez via ntg-context
  2023-01-28 13:31   ` Kalouguine Andre via ntg-context
  2023-01-30 16:30 ` Ulrike Fischer via ntg-context
  1 sibling, 1 reply; 9+ messages in thread
From: Pablo Rodriguez via ntg-context @ 2023-01-28 12:55 UTC (permalink / raw)
  To: Kalouguine Andre via ntg-context; +Cc: Pablo Rodriguez

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
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Subject: Leading spaces verbatim
  2023-01-28 12:55 ` Pablo Rodriguez via ntg-context
@ 2023-01-28 13:31   ` Kalouguine Andre via ntg-context
  2023-01-28 16:37     ` Pablo Rodriguez via ntg-context
  0 siblings, 1 reply; 9+ messages in thread
From: Kalouguine Andre via ntg-context @ 2023-01-28 13:31 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Kalouguine Andre


[-- Attachment #1.1: Type: text/plain, Size: 1020 bytes --]

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,

[-- Attachment #1.2: Type: text/html, Size: 3358 bytes --]

[-- Attachment #2: Type: text/plain, Size: 496 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Subject: Leading spaces verbatim
  2023-01-28 13:31   ` Kalouguine Andre via ntg-context
@ 2023-01-28 16:37     ` Pablo Rodriguez via ntg-context
  2023-01-28 20:26       ` Kalouguine Andre via ntg-context
  0 siblings, 1 reply; 9+ messages in thread
From: Pablo Rodriguez via ntg-context @ 2023-01-28 16:37 UTC (permalink / raw)
  To: Kalouguine Andre via ntg-context; +Cc: Pablo Rodriguez

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
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Subject: Leading spaces verbatim
  2023-01-28 16:37     ` Pablo Rodriguez via ntg-context
@ 2023-01-28 20:26       ` Kalouguine Andre via ntg-context
  2023-01-28 21:54         ` Hans Hagen via ntg-context
  0 siblings, 1 reply; 9+ messages in thread
From: Kalouguine Andre via ntg-context @ 2023-01-28 20:26 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Kalouguine Andre


[-- Attachment #1.1: Type: text/plain, Size: 648 bytes --]

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

[-- Attachment #1.2: Type: text/html, Size: 1590 bytes --]

[-- Attachment #2: Type: text/plain, Size: 496 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Subject: Leading spaces verbatim
  2023-01-28 20:26       ` Kalouguine Andre via ntg-context
@ 2023-01-28 21:54         ` Hans Hagen via ntg-context
  2023-01-29 16:58           ` Kalouguine Andre via ntg-context
  0 siblings, 1 reply; 9+ messages in thread
From: Hans Hagen via ntg-context @ 2023-01-28 21:54 UTC (permalink / raw)
  To: Kalouguine Andre via ntg-context; +Cc: Hans Hagen

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
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
-----------------------------------------------------------------

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Subject: Leading spaces verbatim
  2023-01-28 21:54         ` Hans Hagen via ntg-context
@ 2023-01-29 16:58           ` Kalouguine Andre via ntg-context
  2023-01-30 10:54             ` Pablo Rodriguez via ntg-context
  0 siblings, 1 reply; 9+ messages in thread
From: Kalouguine Andre via ntg-context @ 2023-01-29 16:58 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Kalouguine Andre


[-- Attachment #1.1: Type: text/plain, Size: 1877 bytes --]

On 28.01.2023 22:54, Hans Hagen via ntg-context wrote:

> 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
 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

[-- Attachment #1.2: Type: text/html, Size: 5260 bytes --]

[-- Attachment #2: Type: text/plain, Size: 496 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Subject: Leading spaces verbatim
  2023-01-29 16:58           ` Kalouguine Andre via ntg-context
@ 2023-01-30 10:54             ` Pablo Rodriguez via ntg-context
  0 siblings, 0 replies; 9+ messages in thread
From: Pablo Rodriguez via ntg-context @ 2023-01-30 10:54 UTC (permalink / raw)
  To: Kalouguine Andre via ntg-context; +Cc: Pablo Rodriguez

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

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Subject: Leading spaces verbatim
  2023-01-28  2:01 Subject: Leading spaces verbatim Kalouguine Andre via ntg-context
  2023-01-28 12:55 ` Pablo Rodriguez via ntg-context
@ 2023-01-30 16:30 ` Ulrike Fischer via ntg-context
  1 sibling, 0 replies; 9+ messages in thread
From: Ulrike Fischer via ntg-context @ 2023-01-30 16:30 UTC (permalink / raw)
  To: ntg-context; +Cc: Ulrike Fischer

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/

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2023-01-30 16:30 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-28  2:01 Subject: Leading spaces verbatim Kalouguine Andre via ntg-context
2023-01-28 12:55 ` Pablo Rodriguez via ntg-context
2023-01-28 13:31   ` Kalouguine Andre via ntg-context
2023-01-28 16:37     ` Pablo Rodriguez via ntg-context
2023-01-28 20:26       ` Kalouguine Andre via ntg-context
2023-01-28 21:54         ` Hans Hagen via ntg-context
2023-01-29 16:58           ` Kalouguine Andre via ntg-context
2023-01-30 10:54             ` Pablo Rodriguez via ntg-context
2023-01-30 16:30 ` Ulrike Fischer via ntg-context

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).