[racket] Using scribble as a shim for latex

From: Norman Gray (norman at astro.gla.ac.uk)
Date: Mon May 14 12:13:57 EDT 2012

Ian, hello.

On 2012 May 14, at 16:38, J. Ian Johnson wrote:

> Anyway, I now find myself wanting use the listings package to write some pseudocode with mathescape (thus, verbatim is not good enough). I cannot use the identity trick for the lstlisting environment for whatever reason. It causes pdflatex to explode.
> 
> That is, (bad):
> \identity{\begin{lstlisting}
> Test
> \end{lstlisting}}
> 
> (good):
> \begin{lstlisting}
> Test
> \end{lstlisting}
> 
> Why this is, I don't know. However, I don't know a way to generate the good case here. Is this possible? I really hope it is, since I'll have to use latex directly if not.

This will be at least hard, possibly very hard.

If I'm reading the source correctly, then the lstlisting environment has LaTeX read the environment contents a line at a time (it's a tail recursion), stopping when it (more or less) string matches the \end{lstlisting} alone on a line.  Or something like that -- I'm not completely sure what's happening in there, but the general technique is to play exciting fun and games with TeX's tokenisation mechanism.

The {verbatim} environment does something broadly similar.

Your \identity function, on the other hand, reads its entire argument at once, which means it's tokenised with standard TeX conventions before lstlisting has a chance to get to work.

About the only way you could get (what I take to be) the right effect would be to do something like the following:

\documentclass{article}
\usepackage{listings}
\begin{document}

\setbox0=\vbox{
\begin{lstlisting}
Test
\end{lstlisting}
}
% box0 is now typeset and contained in a box
\copy0 % display box0 on the page

\box0 % display box0 and discard its contents

% \box0 is now empty

\end{document}

The box0 can probably be carried around in some useful way, though not looked into.

Note, though, that box0 is usually used as a scratch box, so if you turn this into a package, start with \newbox\mylistings, and replace '0' with '\mylistings' throughout.

Have fun....

Norman


-- 
Norman Gray  :  http://nxg.me.uk
SUPA School of Physics and Astronomy, University of Glasgow, UK



Posted on the users mailing list.