我希望能够修剪 a 和 的左侧和右侧,\toprule
就像\bottomrule
使用 一样\cmidrule(lr){<column range>}
。我尝试使用 的解决方案非常接近,但不幸的是,它与 a /产生的\cmidrule[\heavyrulewidth]{<column range>}
垂直结果并不完全一致。\toprule
\bottomrule
因此,所需的输出是左侧的表格(使用\MyToprule
和\MyBottomrule
),但右侧的表格具有垂直对齐方式(使用\toprule
和\bottomrule
)。
笔记:
- 这个问题之前被问过如何修剪 \toprule 的长度?,但该帖子被关闭,被视为重复,因为标题并不完全符合要求,或者解决方法已经足够。
- 如果有更简单的方法来获得所需的输出,那将会很有用。
代码:
\documentclass{article}
\usepackage{booktabs}
\newcounter{MyCounter}
\newcommand*{\NumberRow}{\stepcounter{MyCounter}\arabic{MyCounter}.}%
\newcommand*{\MyToprule}[1]{% #1 = 2-<last column>
\cmidrule[\heavyrulewidth]{#1}%
}%
\newcommand*{\MyBottomrule}[1]{% #1 = 2-<last column>
\cmidrule[\heavyrulewidth]{#1}%
}%
\newenvironment{MyTabular}[2][]{%
\setcounter{MyCounter}{0}%
\tabular[#1]{#2}%
}{%
\endtabular
}%
\newcommand*{\TabularContent}{%
& Item Heading & Text Heading \\
\cmidrule(lr){2-2}
\cmidrule(lr){3-3}
\NumberRow & Item 1 & Some Text \\
& Item 1a & Some other Text \\
\NumberRow & Item 2 & Some Text \\
}%
\begin{document}
\begin{MyTabular}{cll}
\MyToprule{2-3}
&\multicolumn{2}{c}{Using MyToprule and MyBottomrule} \\
\TabularContent
\MyBottomrule{2-3}
\end{MyTabular}%
\begin{MyTabular}{cll}
\toprule
&\multicolumn{2}{c}{Using toprule and bottomrule} \\
\TabularContent
\bottomrule
\end{MyTabular}%
\end{document}
答案1
我不确定你为什么要这样布局:顶部和底部规则旨在将表格与上下文区分开来,因此它们也应该涵盖行号。如果数字的作用从标题或其他列上方的标题中清晰可见,则带有行号的列可以没有标题。
如果您想继续这样做,就不要定义\MyToprule
并\MyBottomrule
提出论点,而是留下\cmidrule
对论点的扫描。
MyTabular
请注意,我修复了可选参数处理的定义。
\documentclass{article}
\usepackage{booktabs}
\newcounter{MyCounter}
\newcommand*{\NumberRow}{\stepcounter{MyCounter}\arabic{MyCounter}.}
\newcommand*{\MyToprule}{%
\cmidrule[\heavyrulewidth]%
}
\newcommand*{\MyBottomrule}{%
\cmidrule[\heavyrulewidth]%
}
\newenvironment{MyTabular}[2][c]{% Default is centering
\setcounter{MyCounter}{0}%
\tabular[#1]{#2}%
}{%
\endtabular
}
\newcommand*{\TabularContent}{%
& Item Heading & Text Heading \\
\cmidrule(lr){2-2}
\cmidrule(lr){3-3}
\NumberRow & Item 1 & Some Text \\
& Item 1a & Some other Text \\
\NumberRow & Item 2 & Some Text \\
}
\begin{document}
\begin{MyTabular}{rll}
\MyToprule(lr){2-3} & \multicolumn{2}{c}{Using MyToprule} \\
\TabularContent
\MyBottomrule(lr){2-3}
\end{MyTabular}
\end{document}
(我改变了双列标题以避免它给第二列添加空间。)
更精美的版本,其中自定义环境和正常环境的垂直对齐方式相同(假设的参数为默认值booktabs
)。
\documentclass{article}
\usepackage{booktabs,xparse}
\newcounter{MyCounter}
\newcommand*{\NumberRow}{\stepcounter{MyCounter}\arabic{MyCounter}.}
\newcommand*{\MyToprule}{%
\addlinespace[-\aboverulesep]%
\addlinespace[\heavyrulewidth]
\addlinespace[-2\cmidrulewidth]
\cmidrule[\heavyrulewidth]%
}
\DeclareExpandableDocumentCommand{\MyBottomrule}{d()m}{%
\IfNoValueTF{#1}
{\cmidrule[\heavyrulewidth]{#2}}
{\cmidrule[\heavyrulewidth](#1){#2}}%
\addlinespace[-\belowrulesep]
}
\newenvironment{MyTabular}[2][c]{%
\setcounter{MyCounter}{0}%
\tabular[#1]{#2}%
}{%
\endtabular
}
\newcommand*{\TabularContent}{%
& Item Heading & Text Heading \\
\cmidrule(lr){2-2}
\cmidrule(lr){3-3}
\NumberRow & Item 1 & Some Text \\
& Item 1a & Some other Text \\
\NumberRow & Item 2 & Some Text \\
}
\begin{document}
\setlength{\parindent}{0pt} % just for the example
Top alignment:
\begin{MyTabular}[t]{rll}
\MyToprule(lr){2-3} & \multicolumn{2}{c}{Using MyToprule} \\
\TabularContent
\MyBottomrule(lr){2-3}
\end{MyTabular}\quad
\begin{tabular}[t]{rll}
\toprule & \multicolumn{2}{c}{Using MyToprule} \\
\TabularContent
\bottomrule
\end{tabular}
\bigskip
Center alignment:\medskip
\begin{MyTabular}{rll}
\MyToprule(lr){2-3} & \multicolumn{2}{c}{Using MyToprule} \\
\TabularContent
\MyBottomrule(lr){2-3}
\end{MyTabular}\quad
\begin{tabular}{rll}
\toprule & \multicolumn{2}{c}{Using MyToprule} \\
\TabularContent
\bottomrule
\end{tabular}
\bigskip
Bottom alignment:\medskip
\begin{MyTabular}[b]{rll}
\MyToprule(lr){2-3} & \multicolumn{2}{c}{Using MyToprule} \\
\TabularContent
\MyBottomrule(lr){2-3}
\end{MyTabular}\quad
\begin{tabular}[b]{rll}
\toprule & \multicolumn{2}{c}{Using MyToprule} \\
\TabularContent
\bottomrule
\end{tabular}
\end{document}