如何删除表格中逐字周围的空格

如何删除表格中逐字周围的空格

我了解到有两种方法可以将逐字放入表格中。

\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}   

这两种方式看起来都不太好,因为我的逐字周围的空格太多/太少。

在此处输入图片描述

我如何获得适当的逐字边距?

答案1

我建议fancyvrb和它的BVerbatim环境。

\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}

在此处输入图片描述

答案2

以下是内容垂直居中的版本:

在此处输入图片描述

\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}

答案3

假设整个代码块都放在一个框内。我们称其为框\verbbox。让代码周围有一个紧密的框的最简单方法是使用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将确保\verbbox其宽度恰好足以容纳所有内容。使用框中的代码,您可以使用以下方法在顶部和底部添加一些填充adjustbox

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

其中仅针对顶部/底部padding=<left/right> <top/bottom>设置为。5pt

这是一个完整的例子:

在此处输入图片描述

\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}

答案4

请将的值设置\topsep为应该0可以帮你修复,修改后的代码为:

\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}

输出

在此处输入图片描述

相关内容