# Multiline listing in a table environment

I'm trying to simulate an effect that is very easy to do in WYSIWYG environments, namely having a table with some cells containing somewhat complex multi-line entities. For instance, consider the following table (created with OpenOffice):

In particular, I'm concerned about the middle cell at the bottom there; I can't find a way to create that in LaTeX using the table environment. The problem seems to be that it has no way of distinguishing the \\ linebreak as referring to the contents of the cell, or to the structure of the table.

I know about the multirow package and how it theoretically allows things like this, but only in the clumsiest of ways; I would have to set something like a colspan on every row of that source. If I were to add a line to the beginning of the listing, I'd have to manually shift each row after that down.

What I would love is some sort of easy thing to wrap my code in which makes it all belong to a single table row. I feel certain there must be a way to do that, but Google wasn't being very helpful, so I thought I'd ask here.

-
add comment

## 1 Answer

You can use lstlising environment from listings package to replace verbatim.

For example:

\documentclass{article}
\usepackage{listings}
\lstset{basicstyle=\ttfamily}
\begin{document}
\begin{tabular}{|c|c|}
\hline
\begin{lstlisting}
!@#$%^$%&
!@#$%^$%&
\end{lstlisting}
&
\begin{lstlisting}
!@#$%^$%&
!@#$%^$%&
\end{lstlisting}
\\ \hline
\begin{lstlisting}
!@#$%^$%&
!@#$%^$%&
\end{lstlisting}
&
\begin{lstlisting}
!@#$%^$%&
!@#$%^$%&
\end{lstlisting}
\\ \hline
\end{tabular}
\end{document}
-
Works like a charm :) Thanks a lot, Leo! –  Adrian Petrescu Jan 5 '11 at 21:11
add comment