3

When using tabular and p it is possible to insert Verbatim block in a table using minipage

\documentclass[]{article}%
\usepackage{fancyvrb}
\begin{document}    
\begin{tabular}{|p{2in}|p{3in}|}
\hline
col 1 & \begin{minipage}{3in}
         \begin{Verbatim}
             test
         \end{Verbatim}
        \end{minipage}
\\\hline
\end{tabular}    
\end{document}

However, I use tabularx and use X instead of p for that column. But this does not work

\documentclass[]{article}%
\usepackage{tabularx}
\usepackage{fancyvrb}
\begin{document}

\begin{tabularx}{\paperwidth}{|p{2in}|X|}
\hline
col 1 & \begin{minipage}{4in}
         \begin{Verbatim}
             test
         \end{Verbatim}
        \end{minipage}
\\\hline
\end{tabularx}

\end{document} 

Error is

>pdflatex foo.tex 
This is pdfTeX, Version 3.1415926-2.4-1.40.13 (TeX Live 2012/Debian)
 restricted \write18 enabled.
.....
Style option: `fancyvrb' v2.7a, with DG/SPQR fixes, and firstline=lastline fix 
<2008/02/07> (tvz) (/usr/share/texlive/texmf-dist/tex/latex/graphics/keyval.sty
)) (./foo.aux)
! Argument of \FV@BeginScanning has an extra }.
<inserted text> 
                \par 
l.14 \end{tabularx}

At first I thought may be I need extra {} around the minipage, but that did not help

\documentclass[]{article}%
\usepackage{tabularx}
\usepackage{fancyvrb}
\begin{document}

\begin{tabularx}{\paperwidth}{|p{2in}|X|}
\hline
col 1 & {\begin{minipage}{4in}
         \begin{Verbatim}
             test
         \end{Verbatim}
        \end{minipage}
        }
\\\hline
\end{tabularx}

\end{document} 

Next, I removed the {} and replaced X by p so now it is the same as tabular, but this also did not work:

\documentclass[]{article}%
\usepackage{tabularx}
\usepackage{fancyvrb}
\begin{document}

\begin{tabularx}{\paperwidth}{|p{2in}|p{3in}|}
\hline
col 1 & \begin{minipage}{3in}
         \begin{Verbatim}
             test
         \end{Verbatim}
        \end{minipage}       
\\\hline
\end{tabularx}

\end{document} 

Next, I put back the {} and kept p in there, but this did not work either

\documentclass[]{article}%
\usepackage{tabularx}
\usepackage{fancyvrb}
\begin{document}

\begin{tabularx}{\paperwidth}{|p{2in}|p{3in}|}
\hline
col 1 & {\begin{minipage}{3in}
         \begin{Verbatim}
             test
         \end{Verbatim}
        \end{minipage}       
        }
\\\hline
\end{tabularx}

\end{document} 

question is: How to use Verbatim in tabularx ?

Assuming it is possible with a magic trick, I'd like to use X actually instead of p since I want to tell it to use all the remaining space left.

But with minipage it needs a width, but when it is in an X column, I do not know the width of that column. How to handle that minipage wants width value, but it is sitting in an X column ?

But first, I'd to know even if Verbatim is possible inside tabularx

Nasser
  • 20,220

2 Answers2

4

Short answer no it's not possible (as documented) tabularx reads its body as a macro argument and you can't use verbatim in a macro.

what you can do is

enter image description here

\documentclass[]{article}%
\usepackage{tabularx}
\usepackage{fancyvrb}
\newsavebox\mybox
\begin{document}


\begin{lrbox}{\mybox}\begin{minipage}[t]{3in}
         \begin{verbatim}
             test \ $ {
         \end{verbatim}
\end{minipage}\end{lrbox}

\noindent
\begin{tabularx}{\textwidth}{|X|p{3in}|}
\hline
col 1 & \usebox\mybox
\\\hline
\end{tabularx}

\noindent X\dotfill X



\end{document} 

I assume you didn't really want \paperwidth as the width and also you have to have an X column otherwise tabularx can not do anything about the target width you supply.

David Carlisle
  • 757,742
0

Just come across this question. Using my cprotectinside package:

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

\cprotectinside{@}\begin{tabularx}{\textwidth}{|X|p{3in}|} \hline col 1 &amp; @\begin{minipage}{3in} \begin{Verbatim} test \ $ { \end{Verbatim} \end{minipage}@ \\\hline \end{tabularx}

\end{document}

Same assumptions as in David Carlisle's answer apply.

Output is as you expect:

output image

Note that the normal \verb cannot be used here, because tabularx environment redefines it to "intentionally" make it more limited. But if you insist, there's a workaround \let\normalverb\verb then use \normalverb, see my other answer for an example.

user202729
  • 7,143