How to use \verb in the environment tabular?
Here is one way. Save the verbatim stuff in a verbbox
before the tabular
, and then invoke it from within the tabular
.
\documentclass{article}
\usepackage{array,verbatimbox}
\begin{document}
\begin{myverbbox}{\sfverb}\sffamily\end{myverbbox}
\begin{tabular}{|c|c|}\hline
\multicolumn{2}{|c|}{\sfverb}\\
\hline
\end{tabular}
\end{document}
Above, I use a named verbbox
. If there is only one needed at a time, you can just use the default-named verbbox
:
\documentclass{article}
\usepackage{array,verbatimbox}
\begin{document}
\begin{verbbox}\sffamily\end{verbbox}
\begin{tabular}{|c|c|}\hline
\multicolumn{2}{|c|}{\theverbbox}\\
\hline
\end{tabular}
\end{document}
You can't use \verb
in the argument to another command, in this case \multicolumn
.
For the specific problem, \verb
is not necessary; I suggest to do
\newcommand{\cs}[1]{\texttt{\symbol{`\\}#1}}
and now
\multicolumn{2}{c|}{\cs{sffamily}}
will work.
I would use verbdef
to define a macro with the verbatim content outside the tabular and then use the macro inside it.
\documentclass{article}
\usepackage{array}
\usepackage{verbdef}
\begin{document}
\verbdef\myverbtext|\sffamily|
\begin{tabular}{|c|c|}\hline
\multicolumn{2}{c|}{\myverbtext}
\end{tabular}
\end{document}
The newverbs
package also provides a \collectverb{<code>}<char><verbatim><char>
macro which collects the verbatim for you and feeds it to a macro. However, \collectverb{\multicolumn{2}{c|}}|\sffamily|
does not work here as \collectverb
is not allowed between tabular rows (! Misplaced \omit.
).