Why does verb throw an error using in multicolumn command?

I encounter an error when using the \verb command within the multicolumn command. I reduced the problem to the example below:

\documentclass{article}

\begin{document}
\begin{table}[htb]
\begin{center}
\begin{tabular}{c|c}
\multicolumn{2}{c}{\verb+test+}
\end{tabular}
\end{center}
\end{table}

\end{document}


Error message: \verb illegal in command argument

1. Why is using \verb not allowed within \multicolumn ?

2. How can I workaround this issue ?

1 Answer

You can use the package fancyvrb, I hope that the following example is self-explanatory (With credit to Werner):

\documentclass{article}
\usepackage{fancyvrb}% http://ctan.org/pkg/fancyvrb
\begin{document}
\DefineShortVerb{\#}% # denotes verbatim opening/closing character
\SaveVerb{VerbA}#test#

\begin{table}[htb]
\begin{center}
\begin{tabular}{c|c}
Row 1 & Row 1 \\
\multicolumn{2}{c}{\UseVerb{VerbA}} \\
Row 3 & Row 3
\end{tabular}
\end{center}
\end{table}

\end{document}


The reasoning why you cannot use \verb in macro argument is a bit more complicated. As an argument, the \verb|test| is 1st read+stored in memory, and 2nd processed. While the command \verb needs to be proceesed at the same time as its read, basically because it has to remember the delimiter (| or # or whatever) and only then it can read the argument test.

(In real, it is more complicated and has to do with TeX inner mechanism of reading and interpreting its input, I only tried to give a short overview.)

• Thank you very much for the explanation and the solution ! – Ananke Leda Oct 17 '12 at 13:21