Tables – How to Put Verbatim Inside \multicolumn in Tabular

multicolumntables

I need to put Verbatim text in tabular cell, which works fine, except when I want to extend the cell to all the columns, then I get an error

  File ended while scanning use of \FancyVerbGetLine.

Here is MWE which works, then will show the one that gives the error

\documentclass{article}
\usepackage{fancyvrb}
\begin{document}

\begin{tabular}{|p{2in}|p{2in}|}       \hline
text & text \\\hline
%
\begin{Verbatim}
verbatim text here
\end{Verbatim}
&
\\\hline
%
text & text    \\\hline
\end{tabular}
\end{document}

Mathematica graphics

But the actual cell I have is long and has Verbatim in it and I needed to extend it over all columns.

\documentclass{article}
\usepackage{fancyvrb}
\begin{document}

\begin{tabular}{|p{2in}|p{2in}|}       \hline
text & text \\\hline
%
\multicolumn{2}{|p{4in}|}
{
\begin{Verbatim}
  verbatim text here
\end{Verbatim}
} 
\\\hline
%
text & text    \\\hline
\end{tabular}
\end{document}

Now

pdflatex foo.tex
This is pdfTeX, Version 3.14159265-2.6-1.40.15 (TeX Live 2014) 
 restricted \write18 enabled.
entering extended mode
(./foo.tex
LaTeX2e <2014/05/01>
Babel <3.9l> and hyphenation patterns for 79 languages loaded.
(/usr/local/texlive/2014/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/local/texlive/2014/texmf-dist/tex/latex/base/size10.clo))
(/usr/local/texlive/2014/texmf-dist/tex/latex/fancyvrb/fancyvrb.sty
Style option: `fancyvrb' v2.7a, with DG/SPQR fixes, and firstline=lastline fix 
<2008/02/07> (tvz)
(/usr/local/texlive/2014/texmf-dist/tex/latex/graphics/keyval.sty)) (./foo.aux)
! FancyVerb Error:
  Extraneous input `long text here \end {Verbatim} \@endpbox \hskip \tabcolsep 
\hskip -.5\arrayrulewidth \vrule width\arrayrulewidth \hskip -.5\arrayrulewidth
 \hbox {}\endgroup \ignorespaces ' between \begin{Verbatim}[<key=value>] and li
ne end
.
\FV@Error ... {FancyVerb Error:
\space \space #1
}

l.13 }

This sounds like another one of those environment not allowed inside another environment type issues, which I need help resolving since I do not know how to fix this.

Best Answer

You cannot include verbatim environment inside table, because the macro you are using for tables reads the data as parameter text. It means that the text is tokenized already and cannot be re-tokenized by verbatim environment.

One solution is usage of \halign primitive directly, but this is not, what you probably want. Second solution is to prepare the box with verbatim environment before usage of the tabular environment:

\documentclass{article}
\usepackage{fancyvrb}
\newbox\verbbox
\begin{document}

\setbox\verbbox=\vbox{\hsize=4in
\begin{Verbatim}
  verbatim text here $ \ & etc.
\end{Verbatim}
}

\begin{tabular}{|p{2in}|p{2in}|}       \hline
text & text \\\hline
%
\multicolumn{2}{|c|}
{
\box\verbbox
}
\\\hline
%
text & text    \\\hline
\end{tabular}
\end{document}
Related Question