6

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 ?

2

1 Answer 1

4

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.)

1
  • Thank you very much for the explanation and the solution ! Commented Oct 17, 2012 at 13:21

You must log in to answer this question.

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