3

I have been trying to define a verbatim environment in plain TeX with |...| for inline and ||...|| for display. Optimally, with an easy way to alter so that the visibility of spaces can be toggled (e.g. commenting out a line of code or using a different character like *...* and **...**).

I know that tugboat.sty defines this (see lines 1632-1963), but it is a very long process and frankly quite hard to decipher. I was wondering what the easiest way to do this in plain TeX is.

I have also seen a few questions regarding verbatim in plain TeX (see Define a special verbatim commmand and How to display code snippets in plain TeX), but these both use commands (e.g. \verb{...} and \verbatim ...\endverbatim).

This is for my own curiosity, as way to better understand TeX, and for use in my own plain TeX documents.

btshepard
  • 680
  • 1
    It's not like that the author of tugboat intentionally write it in a difficult way, so maybe tugboat.dtx have the documentation of that code for you to read...? – user202729 Jun 25 '22 at 06:43

1 Answers1

7

here is a short implementation if it's what you're after (probably not very robust).

\catcode`|=\active
\def|{\begingroup \def\do##1{\catcode`##1=12 }\dospecials \verbA}
\def\verbA#1{\ifx|#1\catcode`|=12 \expandafter\verblong \else \verbshort#1\fi}
\long\def\verbshort#1|{\tt#1\endgroup}
\edef\tmp{\long\def\noexpand\verblong##1\string|\string|}
    \tmp{$$\hbox{\tt#1}$$\endgroup}

lorem ipsum |{fjasdf&@(^#(%%| dolor ||\bye|\end|| \bye

output

plante
  • 1,349
  • Thanks, this is exactly what I was looking for! Couldn't you do \tmp{\centerline{\tt#1}\endgroup} instead of using math mode and \hbox? – btshepard Jun 25 '22 at 18:58