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

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:

\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

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%