5

I want to build a table with a column of verbatim, but I don't want to use \verb command for every line (too many rows), how should I do it? I tried to use array package to define new column type but unsuccessful, anyone can shed some light? An example of what I wanna achieve:

\documentclass{article}

\begin{document}
\begin{tabular}{l l}
\verb|some text here| & some text here \\
\verb|some text here| & some text here \\
\verb|some text here| & some text here \\
\end{tabular}
\end{document}
  • I need verbatim (instead of just begin{tabular}{>{\ttfamily}l<{\ttfamily}} as the first row contained many reserved characters.

Thank you!

1
  • Welcome to TeX.SX! With the shortvrb package you might abbreviate to |text| instead of \verb|text|, but not less than this. Just think to the fact that you need some way for TeX to understand where the verbatim text ends.
    – egreg
    Mar 3, 2016 at 21:49

1 Answer 1

6

Verbatim mode is really very special. In some cases one can use the & for delimiting an argument (see the collcell package), but with verbatim this becomes essentially impossible, because the & is not the cell delimiter any more as soon as verbatim mode is started.

The shortvrb package allows to say

\MakeShortVerb{\|}

thereby declaring that, from the point on, |...| is equivalent to \verb|...|. Of course you'll need \verb+...+ (or similar) if the verbatim text contains |.

Note however that this will make | illegal in math, but there's the substitute \vert (or better the pair \lvert and \rvert provided by amsmath) for it.

So the best you can have is

\documentclass{article}
\usepackage{shortvrb}

\MakeShortVerb{\|}

\begin{document}

\begin{tabular}{l l}
|some % text here| & some text here \\
|some $ text here| & some text here \\
|some { text here| & some text here \\
\end{tabular}

\end{document}

enter image description here

1
  • In principle, one could use & as the verbatim delimiter, and then insert another & at the end of the verbatim text, e.g., \newcommand*\SetUpVerbCol{\toks@\expandafter{\verb@egroup &}\edef\verb@egroup{\the\toks@}\verb &}, followed by, say, \begin{tabular}{l>{\SetUpVerbCol}ll}Normal text &verb_text^{$& more text that uses \verb|\verb|\\ … \end{tabular}. And indeed this works, if… the first token in the verbatim column is not a control sequence (say, \foo), because otherwise TeX tries to expand it as soon as it scans the opening &, and this cannot be avoided in any way!
    – GuM
    Mar 4, 2016 at 0:40

You must log in to answer this question.

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