When rendering computer code and output in LateX, I usually resort to the basic verbatim environment, as in
\begin{verbatim}
> matrix(1:4,ncol=3)
[,1] [,2] [,3]
[1,] 1 3 1
[2,] 2 4 2
Warning message:
data length [4] is not a submultiple or multiple of the number of columns [3] in matrix in: matrix(1:4, ncol = 3)
\end{verbatim}
However, I do not like the way tildes are represented in this environment, being considered as accents rather than standard characters, as in the R command
lm(y~x)
Asking Springer-Verlag TeXperts for help during my last round of corrections of Introducing Monte Carlo Methods with R before it left for the printer, I received a solution which is to use the alltt environment instead, which allows for LaTeX commands within the verbatim rendering, using \(\sim\) instead of ~. (I have been looking for this solution for ages and of course, provided with the solution of using alltt instead, I immediately found a lot of related posts!) There is a drag, though, which is that curly brackets {} vanish from the R code, unless replaced with the LaTeX versions \(\{\) and \(\}\).
When ~is used in the in-line rendering
\verb+lm(y~x)+
alltt does not work but a simple
\verb+lm(y+$\sim$\verb+x)+
is sufficient.
LaTeX surprises
Posted in Books, University life with tags comments, Introducing Monte Carlo Methods with R, LaTeX, solution manual, typos, verbatim on January 18, 2010 by xi'anWhen compiling the solution manual to “Introducing Monte Carlo Methods with R” into a version with only the odd-numbered exercises, I used a \relax{…} command to comment out the even-numbered solutions. This produced a strange bug when applied to a paragraph that included the lines
\begin{verbatim} > system.time({for (t in 1:100) y=rmnorm(n=1,var=Sigma)}) user system elapsed 0.028 0.000 0.028 \end{verbatim}as somehow the curly bracket } in the group of commands was understood as the end of the \relax command. It took me a while to spot the reason for this bug as I implicitely assumed that anything within the verbatim environment was not understood as a LaTeX command. But the bug was also helpful in pointing out an extra curly bracket } in an R code provided as a solution (within the verbatim environment). While switching between the long and the short versions of the solution manual to “Introducing Monte Carlo Methods with R”, I also found a point I had been seeking for a while, namely that
\begin{comment} \subsection{Exercise \ref{exo:helpme}} Test the \verb+help+ command on the functions \verb+seq+, \verb+sample+, and \verb+order+. \end{comment}works out nicely to comment out whole paragraphs once the verbatim package is included. However, if I try to shorten the syntax by defining
\newcommand\become{\begin{comment}} \newcommand\begone{\end{comment}}I get a compilation error
Strange, isn’t it?!
Share:
Like this:
1 Comment »