修剪表格中的垂直单元格线,以清晰显示带有换行符的单元格

修剪表格中的垂直单元格线,以清晰显示带有换行符的单元格

我需要在有换行符的单元格前面添加垂直线,以明确大单元格的内容对应哪一行。但是,这些线不能跨越整个单元格高度,因为如果两个连续的单元格有换行符,线就会接触。

这是 MWE

\documentclass{article}

\usepackage{array}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{xtab}
\usepackage[table,xcdraw]{xcolor}

% to make specific-length columns
\newcommand{\PreserveBackslash}[1]{\let\temp=\\#1\let\\=\temp}
\newcolumntype{L}[1]{>{\PreserveBackslash\raggedright}m{#1}}
\newcolumntype{C}[1]{>{\PreserveBackslash\centering}m{#1}}
\newcolumntype{R}[1]{>{\PreserveBackslash\raggedleft}m{#1}}

% example:  C{0.5\textwidth}


\begin{document}


\begin{table}[]
\footnotesize
\begin{tabular}{@{}lL{0.2\textwidth}llrlllllllllll@{}}
\toprule
\textbf{Stuff} & \textbf{Other stuff} & 4.B & 4.D & \multicolumn{1}{l}{5} & 5.A & 5.B & 5.D & 5.E & 5.F \\ \midrule
This text & short text &  &  & \cellcolor[HTML]{DC7878}15 &  & \multicolumn{1}{r}{\cellcolor[HTML]{EDA9A9}7} &  &  &   \\
That text & Some longer text that splits &  &  & \cellcolor[HTML]{F1BDBD}3 &  &  &  &  &    \\
\rowcolor[HTML]{FFF2CC} 
Red text & Even loooonger text that splits, maybe even more than once &  &  & \cellcolor[HTML]{F1BDBD}3 &  &  &  &  &   \\
\rowcolor[HTML]{FFF2CC} 
Blue text & Some long text once again that splits &  &  & \cellcolor[HTML]{F1BDBD}3 &  &  &  &  &   \\
Up text & blah blah blah, again text here &  &  & \multicolumn{1}{l}{} &  &  & \multicolumn{1}{r}{\cellcolor[HTML]{F3C7C7}1} &  & \multicolumn{1}{r}{\cellcolor[HTML]{F3C7C7}1} \\
Down text & text. &  &  & \multicolumn{1}{l}{} &  &  & \multicolumn{1}{r}{\cellcolor[HTML]{F3C7C7}1} &  & \multicolumn{1}{r}{\cellcolor[HTML]{F3C7C7}1} \\
\bottomrule
\end{tabular}
\end{table}

\end{document}

由此得出下表:

表格1

我目前的猜测是用来multicol添加线条分隔,但如上所述,这会连接如下所示的连续垂直线,而这并不是我想要的。

表 2

具有多列行的表格的代码:

\begin{table}[]
\footnotesize
\begin{tabular}{@{}lL{0.2\textwidth}llrlllllllllll@{}}
\toprule
\textbf{Stuff} & \textbf{Other stuff} & 4.B & 4.D & \multicolumn{1}{l}{5} & 5.A & 5.B & 5.D & 5.E & 5.F \\ \midrule
This text & short text &  &  & \cellcolor[HTML]{DC7878}15 &  & \multicolumn{1}{r}{\cellcolor[HTML]{EDA9A9}7} &  &  &    \\
That text & \multicolumn{1}{|L{0.2\textwidth}}{Some longer text that splits} &  &  & \cellcolor[HTML]{F1BDBD}3 &  &  &  &  &  \\
\rowcolor[HTML]{FFF2CC} 
Red text & \multicolumn{1}{|L{0.2\textwidth}}{Even loooonger text that splits, maybe even more than once} &  &  & \cellcolor[HTML]{F1BDBD}3 &  &  &  &  &   \\
\rowcolor[HTML]{FFF2CC} 
Blue text & Some text &  &  & \cellcolor[HTML]{F1BDBD}3 &  &  &  &  &    \\
Up text & \multicolumn{1}{|L{0.2\textwidth}}{blah blah blah, again text here} &  &  & \multicolumn{1}{l}{} &  &  &  \multicolumn{1}{r}{\cellcolor[HTML]{F3C7C7}1} &  & \multicolumn{1}{r}{\cellcolor[HTML]{F3C7C7}1} \\
Down text & text. &  &  & \multicolumn{1}{l}{} &  &  & \multicolumn{1}{r}{\cellcolor[HTML]{F3C7C7}1} &  & \multicolumn{1}{r}{\cellcolor[HTML]{F3C7C7}1} \\
\bottomrule
\end{tabular}
\end{table}

怎样才能使垂直线更短?

免责声明:我知道垂直线是魔鬼(因此,有了书页标签)。如果您有更好的想法来澄清行分隔,同时在页面长度的表格中设置一些具有特定背景颜色的行,我很乐意提出建议...

答案1

借助{NiceTabular}nicematrix您可以随时使用 中的 TikZ\CodeAfter和 创建的 PGF/TikZ 节点绘制您想要的任何规则nicematrix

\documentclass{article}
\usepackage{nicematrix,tikz}
\usepackage{booktabs}

% to make specific-length columns
\newcommand{\PreserveBackslash}[1]{\let\temp=\\#1\let\\=\temp}
\newcolumntype{L}[1]{>{\PreserveBackslash\raggedright}m{#1}}
\newcolumntype{C}[1]{>{\PreserveBackslash\centering}m{#1}}
\newcolumntype{R}[1]{>{\PreserveBackslash\raggedleft}m{#1}}

\begin{document}

\begin{table}
\footnotesize
\begin{NiceTabular}{@{}lL{0.2\textwidth}llrlllll@{}}[color-inside]
\toprule
\textbf{Stuff} & \textbf{Other stuff} & 4.B & 4.D & \multicolumn{1}{l}{5} & 5.A & 5.B & 5.D & 5.E & 5.F \\ \midrule
This text & short text &  &  & \cellcolor[HTML]{DC7878}15 &  & \multicolumn{1}{r}{\cellcolor[HTML]{EDA9A9}7} &  &  &   \\
That text & Some longer text that splits &  &  & \cellcolor[HTML]{F1BDBD}3 &  &  &  &  &    \\
\rowcolor[HTML]{FFF2CC} 
Red text & Even loooonger text that splits, maybe even more than once &  &  & \cellcolor[HTML]{F1BDBD}3 &  &  &  &  &   \\
\rowcolor[HTML]{FFF2CC} 
Blue text & Some long text once again that splits &  &  & \cellcolor[HTML]{F1BDBD}3 &  &  &  &  &   \\
Up text & blah blah blah, again text here &  &  & \multicolumn{1}{l}{} &  &  & \multicolumn{1}{r}{\cellcolor[HTML]{F3C7C7}1} &  & \multicolumn{1}{r}{\cellcolor[HTML]{F3C7C7}1} \\
Down text & text. &  &  & \multicolumn{1}{l}{} &  &  & \multicolumn{1}{r}{\cellcolor[HTML]{F3C7C7}1} &  & \multicolumn{1}{r}{\cellcolor[HTML]{F3C7C7}1} \\
\bottomrule
\CodeAfter
  \begin{tikzpicture} [shorten < = 2pt, shorten > = 2pt]
    \draw (3-|2) -- (4-|2) ; 
    \draw (4-|2) -- (5-|2) ;
    \draw (5-|2) -- (6-|2) ;
    \draw (6-|2) -- (7-|2) ;
  \end{tikzpicture}
\end{NiceTabular}
\end{table}

\end{document}

您需要多次编译。

上述代码的输出

nicematrix但是,由于没有按照 的顺序应用表格中颜色的说明,因此存在一个小问题colortbl。使用 中的说明\CodeBefore则不会出现问题。

\documentclass{article}
\usepackage{nicematrix,tikz}
\usepackage{booktabs}

\begin{document}

\begin{table}
\footnotesize
% nicematrix provides the column m[l]{...}
\begin{NiceTabular}{@{}lm[l]{0.2\textwidth}llrlllll@{}}
\CodeBefore
  \rowcolor[HTML]{FFF2CC}{4,5}
  \cellcolor[HTML]{EDA9A9}{2-7}
  \cellcolor[HTML]{DC7878}{2-5}
  \cellcolor[HTML]{F3C7C7}{3-5,4-5,5-5,6-8,7-8,6-10,7-10}
\Body
\toprule
\textbf{Stuff} & \textbf{Other stuff} & 4.B & 4.D & \multicolumn{1}{l}{5} & 5.A & 5.B & 5.D & 5.E & 5.F \\ \midrule
This text & short text &  &  & 15 &  &  \multicolumn{1}{r}{7} &  &  &   \\
That text & Some longer text that splits &  &  & 3 &  &  &  &  &    \\
Red text & Even loooonger text that splits, maybe even more than once &  &  & 3 &  &  &  &  &   \\
Blue text & Some long text once again that splits &  &  & 3 &  &  &  &  &   \\
Up text & blah blah blah, again text here &  &  & &  &  & \multicolumn{1}{r}{1} &  & \multicolumn{1}{r}{1} \\
Down text & text. &  &  & &  &  & \multicolumn{1}{r}{1} &  & \multicolumn{1}{r}{1}\\
\bottomrule
\CodeAfter
  \begin{tikzpicture} [shorten < = 2pt, shorten > = 2pt]
    \draw (3-|2) -- (4-|2) ; 
    \draw (4-|2) -- (5-|2) ;
    \draw (5-|2) -- (6-|2) ;
    \draw (6-|2) -- (7-|2) ;
  \end{tikzpicture}
\end{NiceTabular}
\end{table}

\end{document}

第二段代码的输出

相关内容