cmidrule
当我在由包生成的表中有一个空行时,我想插入一个,datatool
并使用\bottomrule
。这两者都会在行上方产生多余的垂直空间:
我该如何消除这两个多余的垂直空间呢?
代码:
\documentclass{article}
\usepackage{datatool}
\usepackage{booktabs}
\usepackage{filecontents}
\begin{filecontents*}{foo.dat}
Line1, AAAA
Line2, BBBB
,
Line4, DDDD
Line5, EEEE
\end{filecontents*}
\newcommand{\PrintDTLTable}[1]{%
% #1 = database to search
\begin{tabular}{c c}\toprule
Label & Cost \\\cmidrule{1-2}
\DTLforeach{#1}{%
\RowID=RowID,%
\Label=Label%
}{%
\DTLifnullorempty{\RowID}{%
\\%[-\baselineskip]
%\vspace*{-\baselineskip}%
\cmidrule(lr){1-2}%
}{%
\RowID & \Label \\
}%
}%
\\\bottomrule
\end{tabular}
}%
\begin{document}
\DTLloaddb[noheader,keys={RowID,Label}]{myDB}{foo.dat}
\PrintDTLTable{myDB}
\end{document}
答案1
在 中tabular
,\baselineskip
设置为0pt
,但您可以使用\normalbaselineskip
来纠正额外的空格:
\documentclass{article}
\usepackage{datatool}
\usepackage{booktabs}
\usepackage{filecontents}
\begin{filecontents*}{foo.dat}
Line1, AAAA
Line2, BBBB
,
Line4, DDDD
Line5, EEEE
\end{filecontents*}
\newcommand{\PrintDTLTable}[1]{%
% #1 = database to search
\begin{tabular}{c c}\toprule
Label & Cost \\\cmidrule{1-2}
\DTLforeach{#1}{%
\RowID=RowID,%
\Label=Label%
}{%
\DTLifnullorempty{\RowID}{%
\\[-\normalbaselineskip]
\cmidrule(lr){1-2}%
}{%
\RowID & \Label \\
}%
}%
\\[-\normalbaselineskip]
\bottomrule
\end{tabular}
}%
\begin{document}
\DTLloaddb[noheader,keys={RowID,Label}]{myDB}{foo.dat}
\PrintDTLTable{myDB}
\end{document}
答案2
按照惯例,最好提前建造好桌体并立即交付。
\begin{filecontents*}{\jobname.dat}
Line1, AAAA
Line2, BBBB
,
Line4, DDDD
Line5, EEEE
\end{filecontents*}
\documentclass{article}
\usepackage{datatool}
\usepackage{booktabs}
\newcommand{\PrintDTLTable}[1]{%
% #1 = database to search
\def\tabledata{}%
\DTLforeach{#1}{%
\RowID=RowID,%
\Label=Label%
}{%
\DTLifnullorempty{\RowID}{%
\edef\tabledata{\expandonce{\tabledata}\noexpand\cmidrule(lr){1-2}}
}{%
\edef\tabledata{%
\expandonce{\tabledata}%
\expandonce{\RowID} &
\expandonce{\Label} \noexpand\\%
}%
}%
}%
\begin{tabular}{c c}\toprule
Label & Cost \\\cmidrule{1-2}
\tabledata
\bottomrule
\end{tabular}
}
\begin{document}
\DTLloaddb[noheader,keys={RowID,Label}]{myDB}{\jobname.dat}
\PrintDTLTable{myDB}
\end{document}
答案3
解决方法是在中间和底部规则之前设置负垂直空间[-2.75ex]
\documentclass{article}
\usepackage{datatool}
\usepackage{booktabs}
\usepackage{filecontents}
\begin{filecontents*}{foo.dat}
Line1, AAAA \\
Line2, BBBB \\ [-2.75ex]
,
Line4, DDDD \\
Line5, EEEE \\ [-2.75ex]
\end{filecontents*}
\newcommand{\PrintDTLTable}[1]{%
% #1 = database to search
\begin{tabular}{c c}\toprule
Label & Cost \\\cmidrule{1-2}
\DTLforeach{#1}{%
\RowID=RowID,%
\Label=Label%
}{%
\DTLifnullorempty{\RowID}{%
\\%[-\baselineskip]
%\vspace*{-\baselineskip}%
\cmidrule(lr){1-2}%
}{%
\RowID & \Label
}%
}%
\\\bottomrule
\end{tabular}
}%
\begin{document}
\DTLloaddb[noheader,keys={RowID,Label}]{myDB}{foo.dat}
\PrintDTLTable{myDB}
\end{document}