3

I learned there are two ways to put verbatim inside tabular.

\begin{tabular}{cccp{3cm}}
    \hline
    SID & the number of elements & length & the number of its non-empty\\
    \hline
    1   & 2                      & 4      & \begin{verbatim}
        <c>
        <d>
        <a>
        <{cd}>
        <ca>
        <da>
        <cd>
        <dd>
        <{ad}>
        <{cd}a>
        \end{verbatim} \\
    \hline
\end{tabular}

\begin{tabular}{cccc}
    \hline
    SID & the number of elements & length & the number of its non-empty\\
    \hline
    1   & 2                      & 4      & \begin{minipage}{3cm}
    \begin{verbatim}
    <c>
    <d>
    <a>
    <{cd}>
    <ca>
    <da>
    <cd>
    <dd>
    <{ad}>
    <{cd}a>
    \end{verbatim}\end{minipage} \\
    \hline
\end{tabular}   

Neither of the two ways look good because there are too much/too few spaces around my verbatim.

enter image description here

How do I get proper margin of my verbatim?

4 Answers 4

5

I suggest fancyvrb and its BVerbatim enviroment.

\documentclass{article}
\usepackage{fancyvrb,booktabs}

\newenvironment{tabularverbatim}
 {\VerbatimEnvironment
  \begin{BVerbatim}[baseline=c,formatcom=\setlength{\baselineskip}{\normalbaselineskip}]}
 {\end{BVerbatim}}

\begin{document}

\begin{tabular}{cccc}
\toprule
SID & the number of elements & length & the number of its non-empty \\
\midrule
1 & 2 & 4 &
\begin{tabularverbatim}
<c>
<d>
<a>
<{cd}>
<ca>
<da>
<cd>
<dd>
<{ad}>
<{cd}a>
\end{tabularverbatim}
\\ % <--- should be after the \end{tabularverbatim} line
\bottomrule
\end{tabular}

\end{document}

enter image description here

1

Here is a version with vertically centered contents:

enter image description here

\documentclass{article}
\usepackage{listings} % For thelstlisting environment
\usepackage{makecell} % For manual linebreaks in table headers
\renewcommand{\theadfont}{\normalsize}
\usepackage{booktabs} % For horizontal lines with better spacing

\begin{document}

{\lstset{aboveskip=0pt,belowskip=0pt}
\begin{tabular}{cccc}
    \toprule
    \thead{SID} & \thead{number of\\ elements} & \thead{length} & \thead{number of\\ its non-empty}\\
    \midrule
    1   & 2                      & 4      & \begin{lstlisting}
<c>
<d>
<a>
<{cd}>
<ca>
<da>
<cd>
<dd>
<{ad}>
<{cd}a>
\end{lstlisting} \\
    \bottomrule
\end{tabular}}


\end{document}
1

Assume you have the entire code block inside a box. Let's call this box \verbbox. The easiest way to have a tight box around the code would be to use varwidth:

\usepackage{varwidth}

\newsavebox{\verbbox}
\begin{lrbox}{\verbbox}
\begin{varwidth}{\linewidth}
\begin{verbatim}
<c>
<d>
<a>
<{cd}>
<ca>
<da>
<cd>
<dd>
<{ad}>
<{cd}a>
\end{verbatim}
\end{varwidth}
\end{lrbox}

varwidth will ensure that \verbbox has only the maximum width needed to fit everything inside of it. With the code in a box, you can add some padding on the top and bottom using adjustbox:

\usepackage{adjustbox}
%...
\adjustbox{padding=0pt 5pt}{\usebox{\verbbox}}

where the padding=<left/right> <top/bottom> is set at 5pt for only the top/bottom.

Here's a complete example:

enter image description here

\documentclass{article}

\usepackage{varwidth,adjustbox}

\begin{document}

\newsavebox{\verbbox}
\begin{lrbox}{\verbbox}
\begin{varwidth}{\linewidth}
\begin{verbatim}
<c>
<d>
<a>
<{cd}>
<ca>
<da>
<cd>
<dd>
<{ad}>
<{cd}a>
\end{verbatim}
\end{varwidth}
\end{lrbox}

\begin{tabular}{cccp{3cm}}
  \hline
  SID & the number of elements & length & the number of its non-empty\\
  \hline
  1   & 2                      & 4      & 
    \adjustbox{padding=0pt 5pt}{\usebox{\verbbox}} \\
  \hline
\end{tabular}

\end{document}
0

Please make the value of \topsep should be 0 will help you to fix it, and the modified code is:

\documentclass{book}

\begin{document}

\setlength{\topsep}{0pt}
\begin{tabular}{cccp{3cm}}
    \hline
    SID & the number of elements & length & the number of its non-empty\\
    \hline
    1   & 2                      & 4      & \begin{verbatim}
        <c>
        <d>
        <a>
        <{cd}>
        <ca>
        <da>
        <cd>
        <dd>
        <{ad}>
        <{cd}a>
        \end{verbatim} \\
    \hline
\end{tabular}

\begin{tabular}{cccc}
    \hline
    SID & the number of elements & length & the number of its non-empty\\
    \hline
    1   & 2                      & 4      & \begin{minipage}[t]{3cm}
    \begin{verbatim}
    <c>
    <d>
    <a>
    <{cd}>
    <ca>
    <da>
    <cd>
    <dd>
    <{ad}>
    <{cd}a>
    \end{verbatim}\end{minipage} \\
    \hline
\end{tabular} 
\end{document}

Output

enter image description here

3
  • 1
    Your current solution changes \topsep globally, which will probably lead to bad spacing in other places of the document.
    – siracusa
    Commented Oct 12, 2019 at 7:48
  • 1
    Your solution only seems to change the spacing above the verbatim environment. What about the space below?
    – leandriis
    Commented Oct 12, 2019 at 9:08
  • Understood the issue, just place the tag \setlength{\topsep}{0pt} before \begin{verbatim} will solve the global spacing affect...
    – MadyYuvi
    Commented Oct 14, 2019 at 5:33

You must log in to answer this question.

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