如何制作表格单元格中的背景(垂直线)?

如何制作表格单元格中的背景(垂直线)?

有什么方法可以在单元格背景上制作以下两条水平线?单元格中间应包含蓝色或绿色的水平线。或者第三个选项是两者兼有。

下面是一个简化的表格,我希望应用它,但不知何故我不知道如何...

\documentclass[landscape,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[czech, english]{babel}
%How to make background in tabular cell (vertical lines)?

%USEPACKAGE
\usepackage{tikz}
\usepackage{tcolorbox}
\usepackage{color}
\usepackage{multicol}
\usepackage{multirow}
\usepackage{hhline}

%--------------------
\usepackage{geometry}
\geometry{
    a4paper,
    total={210mm,297mm},
    left=10mm,
    right=10mm,
    top=6mm,
    bottom=2mm,
    bindingoffset=0mm
}

\begin{document}
\pagestyle{empty}
\thispagestyle{empty}
\shorthandoff{-}

\begin{tabular}{|c|c|c|c|}
\hline
\multicolumn{4}{|c|}{Tabular XY} \\
\hline\hline

\multirow{2}{*}{1} & \parbox[c][1cm][c]{2cm}{\centering \scriptsize Lorem ipsum dolor sit amet} & \parbox[c][1cm][c]{1cm}{\centering \scriptsize Lorem ipsum} & Y \\

\hhline{*{1}{|~}*{2}{|-}|-|}

& \parbox[c][1cm][c]{2cm}{\centering \scriptsize Lorem ipsum dolor sit amet} & \parbox[c][1cm][c]{1cm}{\centering \scriptsize Lorem ipsum} & X \\

\hline
\end{tabular}

\end{document}

在此处输入图片描述

答案1

在此处输入图片描述

\documentclass[landscape,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[czech, english]{babel}
%How to make background in tabular cell (vertical lines)?

%USEPACKAGE
\usepackage{tikz}
\usepackage{tcolorbox}
\usepackage{color}
\usepackage{multicol}
\usepackage{multirow}
\usepackage{hhline}

%--------------------
\usepackage{geometry}
\geometry{
    a4paper,
    total={210mm,297mm},
    left=10mm,
    right=10mm,
    top=6mm,
    bottom=2mm,
    bindingoffset=0mm
}

\usepackage{colortbl}
\begin{document}
\pagestyle{empty}
\thispagestyle{empty}
\shorthandoff{-}

\newcommand\R[1]{\mbox{}\hspace{\dimexpr#1-2.5pt}\textcolor{red}{\vrule width 5pt}\hspace{\dimexpr-#1-2.5pt}\mbox{}}
\newcommand\G[1]{\mbox{}\hspace{\dimexpr#1-2.5pt}\textcolor{green}{\vrule width 5pt}\hspace{\dimexpr-#1-2.5pt}\mbox{}}
\newcommand\RG[1]{\mbox{}\hspace{\dimexpr#1-7.5pt}\textcolor{red}{\vrule width 5pt}%
                                                 \hspace{5pt}%
                                                 \textcolor{green}{\vrule width 5pt}%
                                                 \hspace{\dimexpr-#1-7.5pt}\mbox{}}
\begin{tabular}{|c|c|c|c|}
\hline
\multicolumn{4}{|c|}{Tabular XY} \\
\hline\hline

\multirow{2}{*}{1} &\R{1cm} \parbox[c][1cm][c]{2cm}{\centering \scriptsize Lorem ipsum dolor sit amet} & \G{.5cm}\parbox[c][1cm][c]{1cm}{\centering \scriptsize Lorem ipsum} & Y \\

\hhline{*{1}{|~}*{2}{|-}|-|}

& \RG{1cm}\parbox[c][1cm][c]{2cm}{\centering \scriptsize Lorem ipsum dolor sit amet} & \parbox[c][1cm][c]{1cm}{\centering \scriptsize Lorem ipsum} & X \\

\hline
\end{tabular}

\end{document}

答案2

{NiceTabular}这是的解决方案nicematrix。该环境类似于经典环境{tabular}array),但还在单元格、行和列下创建 PGF/Tikz 节点。

然后,就可以在\CodeBeforeTikz 中使用这些节点来绘制您想要的任何规则。

\documentclass{article}
\usepackage[table]{xcolor}
\doublerulesepcolor{white}

\usepackage{nicematrix,tikz}
\usetikzlibrary{calc}

\begin{document}


\begin{NiceTabular}{cm[c]{25mm}m[c]{15mm}c}[vlines,cell-space-limits=3pt]
\CodeBefore
  \begin{tikzpicture} [line width = 2mm]
  \draw [green] (2-|2.5) -- (3-|2.5) ; 
  \draw [blue!50] (2-|3.5) -- (3-|3.5) ; 
  % now, the cell with both rules
  \draw [green] ($(3-|2)!0.4!(3-|3)$) -- ($(4-|2)!0.4!(4-|3)$) ;
  \draw [blue!50] ($(3-|2)!0.6!(3-|3)$) -- ($(4-|2)!0.6!(4-|3)$) ;
  \end{tikzpicture}
\Body
  \Hline
  \Block{1-*}{Tabular XY} \\
  \Hline\Hline
  \Block{2-1}{1} & Lorem ipsum dolor sit amet & Lorem ipsum & Y \\
  \Hline
  & Lorem ipsum dolor sit amet & Lorem ipsum & X \\
  \Hline
\end{NiceTabular}

\end{document}

您需要多次编译(因为 PGF/Tikz 节点)。 上述代码的输出

相关内容