7

I need help aligning text that spans multiple lines where I have Verbatim code in between. Here is a mock example to show what I mean:

Fruit        Amount
Apple        $3.00

\begin{verbatim}
... some code here.
\end{verbatim}

Orange       $4.00
Peach        $7.55

\begin{verbatim}
... more code.
\end{verbatim}

Grapes       $11.50

So basically I need the Fruit and Amount columns to always align as shown, even through the verbatim code. My question is, how can this be done with the least amount of pain? :)

  • 1
    Try to avoid using the words latex and harder in the same sentence. Some of us are still infantile at heart and our minds are likely to stray :-) – paxdiablo Feb 9 '11 at 2:58
  • @Chris R use a monospace font for verbatim text and make sure you have an editor that supports tabbing. – Yiannis Lazarides Feb 9 '11 at 4:26
8

You can insert

\noalign{%
\begin{verbatim}
Some verbatim code, but I'm not inspired,
sorry.
\end{verbatim}
}

inside a tabular, or any alignment, immediately after \\ (in fact, immediately after TeX's \cr, which is hidden in \\ in LaTeX).

LaTeX's tabular is probably the way to go in this case:

\begin{tabular}{lr}% choose whichever column specification you like
  Fruit & Price \\
  Fruit & Price \\
  \noalign{%
    \begin{verbatim}
    Some code
    \end{verbatim}
  }
  Fruit & Price \\
  Fruit & Price
\end{tabular}
| improve this answer | |
  • I have no idea what this code does, but it works brilliantly :) – Chris R Feb 9 '11 at 5:13
  • It's ugly, but I have to admit it works well. – Will Robertson Feb 9 '11 at 6:18
  • @Will: it is ugly. I changed it to a LaTeX solution that seems neater. – Bruno Le Floch Feb 9 '11 at 6:26
  • looks much better now! To be honest, I didn't realise you could put verbatim inside a \noalign. Oops, me. – Will Robertson Feb 9 '11 at 11:50
  • @Will: I didn't realize it either, and I was trying a different way when I stumbled upon this fact in the TeXbook. – Bruno Le Floch Feb 9 '11 at 12:49
5

What defines the alignment of the fruit? Can you just write something like

\begin{tabular}{@{}p{5cm}l@{}}
  Peach & \$4.00 \\
  Banana & \$6.77
\end{tabular}

for each block? Otherwise, if you need to match up lengths through the document based on, say, the largest word in the column then check out the eqparbox package. You could write, e.g.,

\documentclass{article}
\usepackage{eqparbox}
\begin{document}
\newcommand\fruit[2]{%
  \noindent\eqparbox[b]{fruit}{#1}\qquad\$#2 \par
}

\fruit{Apple}{1.00}
\fruit{Grapes}{7.00}
\begin{verbatim}
Some verbatim code, but I'm not inspired,
sorry. &*#^^7
\end{verbatim}
\fruit{Peach}{4.00}
\fruit{Banana}{3.00}

\end{document}

And then compile the document twice.

| improve this answer | |
  • Thanks, it was the latter case - I was about to look into eqparbox then Bruno posted a solution that worked very well for me. – Chris R Feb 9 '11 at 5:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.