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 ?

  • 1
  • 1
    I believe that this question covers your problem (especially the answer by xport): http://tex.stackexchange.com/questions/24574/how-to-put-verb-command-inside-of-textbf-block Some more information is in answer by Werner here: http://tex.stackexchange.com/questions/33113/verb-in-custom-command – yo' Oct 17 '12 at 08:36

1 Answers1

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

David Carlisle
  • 757,742
yo'
  • 51,322