Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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? :)

share|improve this question
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
add comment

migrated from stackoverflow.com Feb 9 '11 at 4:06

This question came from our site for professional and enthusiast programmers.

2 Answers

up vote 8 down vote accepted

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}
share|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
add comment

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.

share|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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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