1

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.

1
  • 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. Sep 3, 2012 at 20:08

1 Answer 1

3

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .