Archive for verbatim

\verbatim [beamer package]

Posted in R, Statistics, University life with tags , , , , , , on June 12, 2012 by xi'an

Once again working on my slides for the AMSI Lecture 2012 tour, it took me a while to get the following LaTeX code (about the family reunion puzzle) to work:

\begin{frame}[fragile,label=notleM2]
\slidetitle{A family meeting}
\begin{block}{Random switch of couples}
\only<1>{
\begin{itemize}
\item Pick two couples [among the 20 couples] at random with probabilities proportional
to the number of other couples they have not seen\\
{\verb+prob=apply(meet(T)==0,1,sum)+}
\item switch their respective position during one of the 6 courses
\item accept the switch with Metropolis--Hsatings probability\\
{\verb#log(runif(1))<(penalty(old)-penalty(new))/gamma#}
\end{itemize}
}
\only<2>{
\begin{verbatim}
for (t in 1:N){
  prop=sample(1:20,2,prob=apply(meet(T)==0,1,sum))
  cour=sample(1:6,1)
  Tp=T
  Tp[prop[1],cour]=T[prop[2],cour]
  Tp[prop[2],cour]=T[prop[1],cour]
  penatp=penalty(meet(Tp))
  if (log(runif(1))<(penat-penatp)/gamma){
    T=Tp
    penat=penatp}
  }
\end{verbatim}
}
\end{block}
\end{frame}

since I was getting error messages of the form

[86] (./simulation.38.vrb) [87] (./simulation.39.vrb
!Illegal parameter number in definition of \beamer@doifinframe.
                 l
l.12 }
?

Using two frames in a row instead of the “only<2>” version  did not help…
Continue reading

what’s wrong with package comment?!

Posted in Books, R, Statistics, University life with tags , , , , on May 4, 2012 by xi'an

I spent most of the Sunday afternoon trying to understand why defining

\newcommand{\era}{\end{comment}}

did not have the same effect as writing the line

\end{comment}

until I found there is a clash due to the comment package… The assuredly simple code

\documentclass{book}
\usepackage{comment}
\begin{document}

\begin{comment}
prompt, you could embark on an on-line visit of the main features of {\tt R} by typing \verb+demo()+ after the prompt (make sure to test \verb+demo(image)+ and \verb+demo(graphics)+ to get an idea of the
\era

Self-explanatory.
\end{document}

produces an error message:

Runaway argument?
! File ended while scanning use of \next.
<inserted text>
                \par
<*> moretest.tex

This is quite an inconvenience as I need to compile my solution manual for “Introducing Monte Carlo Methods with R” with the even-numbered exercises commented out or not depending on the version… (Leaving this package out and using the comment command within the verbatim package does not work either because era does not seem to be recognised as the end of a commented part…)

LaTeX surprises

Posted in Books, University life with tags , , , , , on January 18, 2010 by xi'an

When 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

Runaway argument?! 
File ended while scanning use of \next.
<inserted text>
\par

Strange, isn’t it?!

Verbatim code

Posted in Books, Linux with tags , , , , , on October 27, 2009 by xi'an

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.

Follow

Get every new post delivered to your Inbox.

Join 557 other followers