[racket] Using scribble as a shim for latex

From: J. Ian Johnson (ianj at ccs.neu.edu)
Date: Mon May 14 14:15:37 EDT 2012

Thanks, Norman. Prabhakar, I put on github (as is) what I've primarily been using in scribble:

https://github.com/ianj/scribble-latex-utils/

The lstlisting environment has a partially implemented interface in utils.rkt using Norman's trick.

-Ian
----- Original Message -----
From: "Norman Gray" <norman at astro.gla.ac.uk>
To: "J. Ian Johnson" <ianj at ccs.neu.edu>
Cc: "users" <users at racket-lang.org>
Sent: Monday, May 14, 2012 12:13:57 PM GMT -05:00 US/Canada Eastern
Subject: Re: [racket] Using scribble as a shim for latex


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.