# Hard whitespace

I am using plainTeX and would like to typeset some (python) code examples. Switching to \tt gets me partway there, but I (obviously) need the whitespace respected. Is there a trick for doing this? The last time I needed this I ended up writing a preprocessor that inserted appropriate \ everywhere. Oh dear, I guess I also need to eliminate paragraph indenting, and force newlines.

-
Did the answers help you in the end? If so it might be time to accept one to conclude this thread. If not please point out what is still missing. Thanks. –  Martin Scharrer May 10 '11 at 11:44

I would use \verbatim from eplain.

\input eplain

\verbatim
def foo():
pass
|endverbatim
\bye

-

In general TH.'s answer is preferable. It also disables other special characters like \ which might be used in the Python code.

You can make plainTeX or LaTeX print all spaces using \obeyspaces. For line endings there is \obeylines which actually turns them into paragraphs (\par). The paragraph indention can be removed by setting \parindent=0pt.

This should be done inside a group of course. Make sure to comment all line endings for you code including the trailing \endgroup (or }), otherwise they will be printed as well.

\begingroup
\parindent=0pt\relax
\obeyspaces%
\let =\ %
\obeylines%

\endgroup%

-
It appears that \obeyspaces in this case does not respect the indent (whitespace at the start of the line), only whitespace in the middle of the line. –  Simon Burton Apr 7 '11 at 1:47
@Simon: Ok, that's funny. Add \let =\ % behind \obeylines to avoid this. The space is made active, i.e. is a macro which produces a space like \space. Using the above line it will use \  instead. –  Martin Scharrer Apr 7 '11 at 14:14
@Martin: maybe you can add an explanation why the simple \obeyspaces\obeylines doesn't do the trick. The fact is that \obeylines turns the end-of-line into \par and a normal space doesn't start horizontal mode (and in vertical mode spaces are ignored), while \  does start horizontal mode. –  egreg Sep 16 '11 at 19:21

there's a rather elaborate verbatim routine in the tugboat plain macros, in the file tugboat.sty. it's too long to repeat here, but the code is pretty well documented and can probably be "lifted" without too much hassle. the rationale for developing this code was exactly what was asked for here -- observing precise spacing, especially at the beginnings of code lines, to be able to present tex macro code intelligibly.

tugboat.sty is on tex live in the /tex/plain/tugboat-plain area, and also on ctan.

-

I have tried to use plain TeX to typeset source code (in many languages). As it proved to be a hard task, and even harder if you wanted some eye candy like keyword highlighting, nowadays I prefer to use LaTeX to do the job with the listing environment and to include the output as a figure.

Ex:

% dclass and package loading
\usepackage{listings}

% stuff, stuff, stuff,

\lstset{
rulecolor=\color{BlockGray},
rulesepcolor=\color{TopGray},
language=Python,
numbers=left,
numberstyle=\tiny\color{yellow},
numberblanklines=false,
basicstyle=\footnotesize,
showstringspaces=false,
}

% stuff, stuff, stuff,

\begin{document}

% stuff, stuff, stuff,

\begin{lstlisting}
from twisted.internet import reactor

def helloWorld():
print "Hello World!"

reactor.callWhenRunning(helloWorld)
reactor.callWhenRunning(reactor.stop)

reactor.run()
\end{lstlisting}

% stuff, stuff, stuff,

-
Please note the plain-tex-tag of the OP. –  morbusg Apr 6 '11 at 19:09
Yep, I noticed since the beginning. So, I edited my post so I could better explain what I'm use to do when I want to typeset source code. –  PEdroArthur Apr 6 '11 at 19:16
@PEdroArthur: I have also found to my own detriment that one must stick to the OP's question and nothing else. If someone works in plainTeX without studying the TeXbook (especially App E were the answer to this question is) then he is in for a long suffering. My feeling is also switch to Latex and use listings, but it is not the right answer. –  Danie Els Apr 7 '11 at 16:10
@Martin and Pedro: I think this answer is fine. It says use LaTeX to generate the listing and then include that as a graphic in the plain TeX document. The answer would be better if it included a complete example for making the listing in LaTeX (including document class and any packages needed) as well as including the code necessary to include it as a graphic in the plain TeX document. –  TH. Apr 7 '11 at 16:49
@PEdro: I'd probably use graphicx in plain TeX, but my point was that a complete answer would turn your answer from a fine one into a good one. –  TH. Apr 7 '11 at 17:38