Verbatim environment inside a box

2,430 views
Skip to first unread message

ca2004

unread,
Nov 22, 2010, 1:02:34 PM11/22/10
to LaTeX Users Group
Hi everyone,


I have a programming code, and I need to put this code in a verbatim
environment, and around the code, create a box, and put all this in a
figure environment. So,, I tried to use the following code:

\begin{figure}[h!]
\mbox{
\begin{verbatim}
code 1
code 2
code 3
...

\end{verbatim}
}
\end{figure}

when I compile, appear an error, but If I take out the \mbox command
the compiling process is right, no errors, I tried to use \fbox
instead \mbox, but appears a error, too.

Does anyone know what is happening, why does the error appear.


Thanks,

Manuel

Bruno Lopes

unread,
Nov 22, 2010, 3:17:36 PM11/22/10
to latexus...@googlegroups.com
For multiple lines use \makebox instead of \mbox.

Bruno


--
You received this message because you are subscribed to the Google Groups "LaTeX Users Group" group.
To post to this group, send email to latexus...@googlegroups.com.
To unsubscribe from this group, send email to latexusersgro...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/latexusersgroup?hl=en.




--
Bruno Lopes Vieira

Linux User #324250
Curriculum Vitae: http://lattes.cnpq.br/7793315334001237
Website: www.tecmf.inf.puc-rio.br/BrunoLopes
--
"Never express yourself more clearly than you are able to think."
(Niels Bohr)

Peter Flynn

unread,
Nov 22, 2010, 6:20:16 PM11/22/10
to latexus...@googlegroups.com
On Mon, Nov 22, 2010 at 6:02 PM, ca2004 <mcastro...@gmail.com> wrote:
Hi everyone,


I  have a programming code, and I need to put this code in a verbatim
environment, and around the code, create a box,  and put all this in a
figure environment. So,, I tried to use  the following code:

Don't.
Use the fancyvrb package instead: it has built-in features for doing exactly this..
 

\begin{figure}[h!]
\mbox{
\begin{verbatim}
code 1
code 2
code 3
...

\end{verbatim}
}
\end{figure}

when I compile, appear an error, but If I take out the \mbox command
the compiling process is right, no errors, I tried  to use \fbox
instead \mbox, but appears a error, too.

Does anyone know what is happening, why does the error appear.

You cannot use the standard verbatim environment as the argument to a command, because it gets swallowed by TeX in a way that makes it unprocessable. fancyvrb's Verbatim environment avoids this:

\usepackage{fancyvrb}
...
\begin{figure}
\caption{stuff}
\label{foo}
\begin{Verbatim}[frame=single]
stuff
\end{Verbatim}
\end{figure}

///Peter

Paulo Rogerio

unread,
Nov 23, 2010, 8:03:19 AM11/23/10
to LaTeX Users Group
Have you tried use a listings package?
This package is a source code for LaTeX, which allows to write a
programming code
It also have an interface with the package fancyvrb.

An example code are below:

\usepackage{xcolor}
\usepackage{listings}
\lstset{
language=C++,
keywordstyle=\color{black},
frame=single
}

\begin{document}

\begin{lstlisting}
for (i=0;i<10,i++){
i = i + 10;
cout<<i;
}
\end{lstlisting}

On 22 nov, 20:20, Peter Flynn <anglebrac...@gmail.com> wrote:

Bruno Lopes

unread,
Nov 23, 2010, 11:43:34 AM11/23/10
to latexus...@googlegroups.com
Another option in the same way of Paulo's is using the package algorithmic, wich has a beautiful idented way of expressing algorithms.

--
You received this message because you are subscribed to the Google Groups "LaTeX Users Group" group.
To post to this group, send email to latexus...@googlegroups.com.
To unsubscribe from this group, send email to latexusersgro...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/latexusersgroup?hl=en.

James McElhannon

unread,
Nov 23, 2010, 2:07:16 PM11/23/10
to LaTeX Users Group
I also use listings. I don't put the source into my latex file, instead I
have listings retrieve it with \lstinputlisting.

I have latex at the end of a long build process where it compiles the source
that I am going to include. That way there are no typos in the source.

One thing that I thought was especially cool about the listings package is
that it can number the lines in the output and you can embed latex commands
in the source. Using this combination, you can insert a label into the
source code, have latex not print the latex to keep it pretty in the
document, and then reference the line numbers. So your text can say "... as
shown on line 10 and lines 15-30...".

James

\begin{document}

--

Reply all
Reply to author
Forward
0 new messages