jwaliszko
jwaliszko

Reputation: 17074

Verbatim environment inside LaTeX cell?

I would like to insert some XML inside a LaTeX table, so I thought that \begin{verbatim}.. will be a good solution for preserving the syntax, but it does not work like this:

\begin{tabular}{ ll }
   sample & 
   \begin{verbatim}
      <how>
          <to value="make" />
          <this value="work" />
      </how>
   \end{verbatim}
\end{tabular}

How can I make this work?

Upvotes: 21

Views: 20279

Answers (3)

CarLaTeX
CarLaTeX

Reputation: 305

With fancyvrb package you could use BVerbatim with baseline=t and let LaTeX work for you.

Here I've created a new verbatim environment (Code) for convenience:

\documentclass{article}

\usepackage{fancyvrb}
\DefineVerbatimEnvironment{Code}{BVerbatim}{baseline=t}

\begin{document}
\begin{tabular}{ ll }
    sample & 
    \begin{Code}
    <how>
    <to value="make" />
    <this value="work" />
    </how>
    \end{Code}
\end{tabular}
\end{document}

Here the result:

enter image description here

Upvotes: 2

Alok Singhal
Alok Singhal

Reputation: 96241

Try with

\begin{tabular}{lp{5in}}

I.e., change the verbatim environment to be in p type column. Other solutions are to use multicols or a minipage for the verbatim environment.

Upvotes: 8

zwol
zwol

Reputation: 140856

You need to put it inside a minipage, like so:

\begin{tabular}{ ll }
sample &
\begin{minipage}{3in}
\begin{verbatim}
<how>
   <to value="make" />
   <this value="work" />
</how>
\end{verbatim}
\end{minipage}
\end{tabular}

Unfortunately, this means you have to decide how wide the column will be in advance (that's what the {3in} part does). I usually start with 3in and then adjust it up or down until the page looks good and I stop getting overfull hbox messages.

Upvotes: 27

Related Questions