[racket] Using scribble as a shim for latex

115 views
Skip to first unread message

J. Ian Johnson

unread,
May 14, 2012, 11:38:59 AM5/14/12
to users
I've been writing my papers in scribble and having a mostly good experience. I use samth's trick of using an "exact" function that maps whatever it gets (almost) directly to latex:

(define exact-style (make-style "identity" '(exact-chars)))
(define (exact . items)
(make-element exact-style (map content->latex-content items)))

Where "identity" is defined in an extra *.tex file I include with ++style:

\newcommand{\identity}[1]{#1}

I have a big map around content objects to translate out Unicode to The Right Thing. I snatched the big case expression in latex-render.rkt and added many more cases.

****
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.

-Ian
____________________
Racket Users list:
http://lists.racket-lang.org/users

Norman Gray

unread,
May 14, 2012, 12:13:57 PM5/14/12
to J. Ian Johnson, users

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

Prabhakar Ragde

unread,
May 14, 2012, 12:25:57 PM5/14/12
to us...@racket-lang.org
Ian 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.

I've been using Scribble on top of LaTeX/Beamer for course slides. I
spent some time trying to get the listings package to work without too
much effort, and gave up. It's very sensitive, as LaTeX code can be. My
sense is that it would require some work down at the rendering level in
Scribble/Racket (or, worse, some LaTeX macro hacking), but I'd love to
be proven wrong.

Those of us who are interested in using Scribble in this fashion should
maybe try to figure out some way to coordinate efforts and share tips. --PR

J. Ian Johnson

unread,
May 14, 2012, 2:15:37 PM5/14/12
to Norman Gray, users, Prabhakar Ragde
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" <nor...@astro.gla.ac.uk>
To: "J. Ian Johnson" <ia...@ccs.neu.edu>
Cc: "users" <us...@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.
>
Reply all
Reply to author
Forward
0 new messages