英文:
Disconnected vertical lines with Latex table using booktabs package
问题
我想要在我的表格中断开垂直线,就像下面的示例中一样。
我的尝试得到的垂直线就像下面显示的第二个表格一样,我如何才能重现示例中的线条呢?
这是生成我的表格的代码:
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{multirow}
\usepackage{array}
\usepackage{makecell}
\usepackage[table,xcdraw]{xcolor}
\usepackage{booktabs}
\usepackage{arydshln}
\begin{document}
\begin{table}[h]
\begin{center}
\setlength{\tabcolsep}{8pt}
\renewcommand{\arraystretch}{1.0}
\begin{tabular}{c|cccc}
\toprule
classes & abs\_rel & a1 & a2 & a3 \\ \midrule
Road & 0.138 & 0.851 & 0.917 & 0.946 \\
Vehicle & 0.135 & 0.855 & 0.920 & 0.948 \\
Lane & 0.135 & 0.855 & 0.920 & 0.947 \\
\bottomrule
\end{tabular}
\end{center}
\end{table}
\end{document}
【注意】:我已经省略了不需要翻译的代码部分。
英文:
I want the vertical lines in my table disconnected like in the example below.
My attempt is getting me vertical lines like in the second table shown below, how can I reproduce the lines like in the example?
This is my code to generate my table
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{multirow}
\usepackage{array}
\usepackage{makecell}
\usepackage[table,xcdraw]{xcolor}
\usepackage{booktabs}
\usepackage{arydshln}
\begin{document}
\begin{table}[h]
\begin{center}
\setlength{\tabcolsep}{8pt}
\renewcommand{\arraystretch}{1.0}
\begin{tabular}{c|cccc}
\toprule
classes & abs\_rel & a1 & a2 & a3 \\ \midrule
Road & 0.138 & 0.851 & 0.917 & 0.946 \\ %\hline
Vehicle & 0.135 & 0.855 & 0.920 & 0.948 \\ %\hline
Lane & 0.135 & 0.855 & 0.920 & 0.947 \\ %\hline
\bottomrule
\end{tabular}
\end{center}
\end{table}
\end{document}
答案1
得分: 2
你可以在不加载arydshln
包的情况下复制booktabs
与垂直线的错误用法。这个包可以修复booktabs
与垂直线的一些不兼容性。
然而,请仔细考虑一下,是否真的想让你的表格看起来像是编码错误。它会看起来好像你不懂 LaTeX。
\documentclass{article}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{multirow}
\usepackage{array}
\usepackage{makecell}
\usepackage[table,xcdraw]{xcolor}
\usepackage{booktabs}
%\usepackage{arydshln}
\begin{document}
\begin{table}[h]
\begin{center}
\setlength{\tabcolsep}{8pt}
\renewcommand{\arraystretch}{1.0}
\begin{tabular}{c|cccc}
\toprule
classes & abs\_rel & a1 & a2 & a3 \\ \midrule
Road & 0.138 & 0.851 & 0.917 & 0.946 \\ %\hline
Vehicle & 0.135 & 0.855 & 0.920 & 0.948 \\ %\hline
Lane & 0.135 & 0.855 & 0.920 & 0.947 \\ %\hline
\bottomrule
\end{tabular}
\end{center}
\end{table}
\end{document}
英文:
You can replicate the incorrect usage of booktabs with vertical lines if you refrain form loading the arydshln
package. This package would fix some of the incompatibilities of booktabs with vertical lines.
However please consider carefully if you really want your table to look like you made an error in coding it. It will look as if you don't know your latex.
\documentclass{article}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{multirow}
\usepackage{array}
\usepackage{makecell}
\usepackage[table,xcdraw]{xcolor}
\usepackage{booktabs}
%\usepackage{arydshln}
\begin{document}
\begin{table}[h]
\begin{center}
\setlength{\tabcolsep}{8pt}
\renewcommand{\arraystretch}{1.0}
\begin{tabular}{c|cccc}
\toprule
classes & abs\_rel & a1 & a2 & a3 \\ \midrule
Road & 0.138 & 0.851 & 0.917 & 0.946 \\ %\hline
Vehicle & 0.135 & 0.855 & 0.920 & 0.948 \\ %\hline
Lane & 0.135 & 0.855 & 0.920 & 0.947 \\ %\hline
\bottomrule
\end{tabular}
\end{center}
\end{table}
\end{document}
答案2
得分: 1
这里是一个完整的答案。如果你的目标是在表格中保持相同的间隔,那就使用 booktabs
。
然而,即使不加载 booktabs
,也可以创建自定义间隔,只要它们在 tabular
开始创建新行之前添加。实际上,间隔成为规则的一部分,而不是行/单元格的一部分。这是通过TeX原语 \noalign{}
实现的;它只能在行之间使用!例如,可以在行之间放置以下行
% ...
\hline \noalign{\vskip 3pt}
% ...
就会在\hline
下方得到3pt的间隔,打破垂直条。由于你已经添加了makecell
包,\Xhline{<<rule size>>}
也是可用的。结合 \noalign{\vskip <<size>>}
,你可以得到具有自定义规则的自定义间隔。
这里有一个例子,我创建了一个方便的宏:\Gap
和带参数的 \XGap{<<size>>}
。
\documentclass{article}
\usepackage{makecell}
\NewExpandableDocumentCommand\XGap{m}{\noalign{\vskip #1}}
\NewExpandableDocumentCommand\Gap{}{\XGap{3pt}}
\begin{document}
\begin{table}[h]
\begin{center}
\setlength{\tabcolsep}{8pt}
\renewcommand{\arraystretch}{1.0}
\begin{tabular}{c | *4{c}}
\Xhline{0.8pt} \XGap{1pt}
classes & abs\_rel & a1 & a2 & a3 \\
\Gap \hline \Gap
Road & 0.138 & 0.851 & 0.917 & 0.946 \\
Vehicle & 0.135 & 0.855 & 0.920 & 0.948 \\
Lane & 0.135 & 0.855 & 0.920 & 0.947 \\
\XGap{1pt} \Xhline{0.8pt}
\end{tabular}
\end{center}
\end{table}
\end{document}
这些宏可以叠加,在每行之后重复使用,并与负跳跃一起使用,以实现一些特殊效果。例如,考虑这个表格
\begin{table}[h]
\begin{center}
\setlength{\tabcolsep}{8pt}
\renewcommand{\arraystretch}{1.0}
\begin{tabular}{c | *4{c}}
\Xhline{0.8pt} \XGap{1pt}
classes & abs\_rel & a1 & a2 & a3 \\\Gap
\hline \Gap
Road & 0.138 & 0.851 & 0.917 & 0.946 \\\Gap
Vehicle & 0.135 & 0.855 & 0.920 & 0.948 \\\Gap
Lane & 0.135 & 0.855 & 0.920 & 0.947 \\\Gap\XGap{-2pt}
\Xhline{0.8pt}
\end{tabular}
\end{center}
\end{table}
英文:
Here's an answer just for completeness. If your goal is to have the same gaps across the table, stick to booktabs
.
However, custom gaps can be created without loading booktabs
as long as they are added before tabular
starts creating a new row. Essentially gaps become a part of a rule, not a part of rows/cells. It is achieved by a TeX primitive \noalign{}
; it can only be used between rows! For instance, one can place the following line between rows
% ...
\hline \noalign{\vskip 3pt}
% ...
and get the 3pt skip below the \hline
which breaks the vertical bar. Since you have added makecell
package, \Xhline{<<rule size>>}
is also available. Combined with \noalign{\vskip <<size>>}
and you can have custom gaps with custom rules.
Here's the example where I made a convenient macros: \Gap
and parametrised \XGap{<<size>>
.
\documentclass{article}
\usepackage{makecell}
\NewExpandableDocumentCommand\XGap{m}{\noalign{\vskip #1}}
\NewExpandableDocumentCommand\Gap{}{\XGap{3pt}}
\begin{document}
\begin{table}[h]
\begin{center}
\setlength{\tabcolsep}{8pt}
\renewcommand{\arraystretch}{1.0}
\begin{tabular}{c | *4{c}}
\Xhline{0.8pt} \XGap{1pt}
classes & abs\_rel & a1 & a2 & a3 \\
\Gap \hline \Gap
Road & 0.138 & 0.851 & 0.917 & 0.946 \\
Vehicle & 0.135 & 0.855 & 0.920 & 0.948 \\
Lane & 0.135 & 0.855 & 0.920 & 0.947 \\
\XGap{1pt} \Xhline{0.8pt}
\end{tabular}
\end{center}
\end{table}
\end{document}
The macros can be stack, repeated after each row and used with negative skips to achieve some special effects. For instance, consider this table
\begin{table}[h]
\begin{center}
\setlength{\tabcolsep}{8pt}
\renewcommand{\arraystretch}{1.0}
\begin{tabular}{c | *4{c}}
\Xhline{0.8pt} \XGap{1pt}
classes & abs\_rel & a1 & a2 & a3 \\\Gap
\hline \Gap
Road & 0.138 & 0.851 & 0.917 & 0.946 \\\Gap
Vehicle & 0.135 & 0.855 & 0.920 & 0.948 \\\Gap
Lane & 0.135 & 0.855 & 0.920 & 0.947 \\\Gap\XGap{-2pt}
\Xhline{0.8pt}
\end{tabular}
\end{center}
\end{table}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论