For some teaching I've been doing, I'd like to have code from two different languages side-by-side. To do so, I hacked together a newcommand which uses a tabular environment to arrange two verb environments, but the problems are myriad:
Indentation can only be accomplished by changing spaces to ~'s
Any math symbols have to be escaped with
\, which requires knowing which ones to escape and which ones not to (can't escape the <'s for instance, otherwise it complains)The
<-assignment operator gets changed into an upside down!-when compiled.
The listings environment doesn't work either. Error message is:
Runaway argument?
! Forbidden control sequence found while scanning use of \lst@next.
<inserted text>
\par
l.23 \codeListing{x <- y}{f(g(x))}
Here's some code to reproduce the problem:
\documentclass[english]{article}
\usepackage[latin9]{inputenc}
\usepackage{geometry}
\begin{document}
\newcommand{\codeListing}[2]{
\begin{tabular}{p{8cm} p{8cm}}
\begin{lstlisting}
#1
\end{lstlisting}
&
#2\tabularnewline
\end{tabular}
}
\section{verbatim test}
\begin{verbatim}
logLik <- function(x) {
y <<- x^2+2
return(sum(sqrt(y+7)))
}
\end{verbatim}
\section{codeListing test}
\codeListing{}{
logLik <- function(x) \{
~~y <<- x\^2+2
~~return(sum(sqrt(y+7)))
\}
}
\end{document}
Is there any way to make a newcommand wrap parameter-passed code? It doesn't have to be keyword-highlighted or do any of the other tricks that the fancier packages do; it just has to preserve whitespace and no interpret code as latex commands.



minipageorparallelenvironment. Note also thatlistingsandminted(and a few others) are specialized in printing computer code (which is closer to your need than verbatim). – raphink Aug 13 '11 at 06:33listingspackage. Read its manual by typingtexdoc listingsin your DOS prompt. – Display Name Aug 13 '11 at 07:48listingsovermintedor any of the others? – Ari B. Friedman Aug 13 '11 at 07:52minted. I useshowexplandlistingspackages. The former internally useslistings. I don't know whymintedexists and what it wants to improve. :-) – Display Name Aug 13 '11 at 07:58mintedwas developed to improve on some problems withlistingscapacity to properly "understand" the code it was typesetting – Seamus Aug 13 '11 at 13:59