LaTeX 表格非分割单元格,带对角线分割颜色

LaTeX 表格非分割单元格,带对角线分割颜色

这是一种奇怪的情况,但是我有一个表格单元格,我需要文本正常位于单元格中(不拆分)并且单元格颜色沿对角线拆分。

我知道如何使用slashbox来拆分单元格,但似乎没有办法在不拆分文本的情况下拆分颜色。基本上,我需要它看起来像这样:

在此处输入图片描述

任何建议将不胜感激。

答案1

由于您没有添加 MWE,我只能假设这是您所需要的:

\documentclass{article}
\usepackage{makecell}
\usepackage{tikz}
\usetikzlibrary{matrix}
% code (slightly modified) from https://tex.stackexchange.com/a/343392/101651
\tikzset{
diagonal fill/.style 2 args={fill=#2, path picture={%
\fill[#1] (path picture bounding box.south west) -|
                         (path picture bounding box.north east) -- cycle;}},
reversed diagonal fill/.style 2 args={fill=#2, path picture={
\fill[#1] (path picture bounding box.north west) |- 
                         (path picture bounding box.south east) -- cycle;}}
}


\begin{document}
\begin{table}
\centering
\begin{tikzpicture}
\matrix[matrix of nodes,
    row sep=-\pgflinewidth,
    column sep=-\pgflinewidth,
    column 2/.style={nodes={text width=4.5cm}},
    row 1/.style={nodes={minimum height=7ex}},
    align=center,
    text centered,
    nodes={text width=2cm,
        text height=1.5ex,
        text depth=.25ex,
        minimum height=4ex,
        }] (mymatr) {
|[reversed diagonal fill={green}{red}]|\makecell{Let's\\recap} & \makecell{Van Duck's \\rules}\\
1& Read the manuals\\
2& Look at the log\\
3& Search on \TeX.SE\\
4& |[diagonal fill={yellow}{orange}]|\color{blue}\bfseries Always add an MWE\\
};
\draw[thick] (mymatr-1-1.north west) -- (mymatr-1-2.north east);
\draw (mymatr-1-1.south west) -- (mymatr-1-2.south east);
\draw[thick] (mymatr-5-1.south west) -- (mymatr-5-2.south east);
\end{tikzpicture}
\end{table}
\end{document}

在此处输入图片描述

致谢 ferahfezadiagonal fill风格reversed diagonal fill

答案2

使用{NiceTabular}of nicematrix。该环境类似于{tabular}(of nicematrix),但在单元格、行和列下创建 PGF/Tikz 节点。

因此,可以使用 Tikz 在数组的单元格下绘制任何你想要的内容。

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

\begin{document}

\renewcommand{\arraystretch}{1.4}
\begin{NiceTabular}{ccc}
\CodeBefore
  \begin{tikzpicture}
  \fill [red!15] (1-|1) -- (2-|1) -- (2-|2) -- cycle ;
  \fill [blue!15] (1-|1) -- (1-|2) -- (2-|2) -- cycle ;
  \end{tikzpicture}
\Body
Some text & other text & another text \\
Some text & other text & another text \\
Some text & other text & another text \\
\end{NiceTabular}

\bigskip
\begin{NiceTabular}{ccc}[hvlines]
\CodeBefore
  \begin{tikzpicture}
  \fill [red!15] (1-|1) -- (2-|1) -- (2-|2) -- cycle ;
  \fill [blue!15] (1-|1) -- (1-|2) -- (2-|2) -- cycle ;
  \end{tikzpicture}
\Body
Some text & other text & another text \\
Some text & other text & another text \\
Some text & other text & another text \\
\end{NiceTabular}


\end{document}

您需要多次编译(因为nicematrix在后台使用 PGF/Tikz 节点)。

上述代码的输出

相关内容