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 have to write a table with two columns and in each column I have to place small C code snippets. Using the \code{} in each column seems to be very tedious because I have to write \code{} in each line on each box of the table.

\begin{table}[htp]
\caption{Possible transformations}
\begin{minipage}{\columnwidth}
\centering
{\small
\begin{tabular}{|l|l|}\hline\hline
code1           & code2 \\\hline
\code{float PI = 3.14;} & \code{constexpr double PI = 3.14;} \\\hline
\code{if(A > B) A = B } & \code{void F(T\& A, const T\& B)}\\
                & \code{\{if (A>B) A=B; \} } \\\hline
\end{minipage}
\label{tab:general-transformation}
\end{table}

Also I want 2-3 lines of code in each box. How do I do that? Thanks.

share|improve this question
    
Please add a complete minimal working example (MWE) that illustrates your problem. –  Kurt Sep 3 '12 at 19:48
1  
Note that an MWE should include document class, all necessary packages and definitions (where does \code come from, for example?) and a document environment. In other words, we should be able to copy the whole thing into a new file and compile it. At the same time, it should be minimal, so any packages etc. that doesn't influence the problem, should be left out. –  Torbjørn T. Sep 3 '12 at 20:08

1 Answer 1

up vote 3 down vote accepted

You could use the tabu and the fancyvrb packages:

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{tabu}

\begin{document}

\begin{tabu*}{XX}
\tabucline-
\Verb!&_$^#$!
& 
\Verb!___$^#$!
\\[-\normalbaselineskip]
\begin{Verbatim}
Some lines
of a long
verbatim fragment
\end{Verbatim}
&
\begin{Verbatim}
Some lines
of another long
verbatim fragment
\end{Verbatim}
\\
\tabucline-
\end{tabu*}

\end{document}

enter image description here

Of course, if your verbatim material doesn't require any special treatment and doesn't include any special characters, you can use the >{...} syntax to add \ttfamily to the columns and simply say something like this:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{array}
\usepackage{booktabs}

\newcolumntype{C}[1]{>{\small\ttfamily}p{#1}}

\begin{document}

\noindent\begin{tabular}{C{3cm}C{4.5cm}}
\toprule
code1 & code2 \\
\midrule
float PI = 3.14; & constexpr double PI = 3.14; \\
\midrule
if(A > B) A = B  & void F(T\& A, const T\& B) \\
& \{if (A>B) A=B; \} \\
\bottomrule
\end{tabular}

\end{document}

enter image description here

share|improve this answer

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.