我尝试使用 TikZ 矩阵库排版表格,例如图形,并希望在其中添加条纹。但是,我还想用另一种颜色来设置最后一列和最后一行的样式。无论 和 的顺序和组合如何,.append style
.prefix style
我.style
都无法从 覆盖条纹颜色every odd row
。
这是一个最小的工作示例:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix [
matrix of nodes,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={draw, anchor=center, minimum size=.75cm},
every odd row/.style={nodes={fill=yellow!10}},
column 6/.style={column sep=5pt},
column 7/.style={nodes={fill=green!10, font=\bfseries}},
row 5/.style={row sep=5pt},
row 6/.style={nodes={fill=green!10, font=\bfseries}}
] {
1 & 1 & 1 & 0 & 1 & 0 & 0 \\
0 & 0 & 1 & 0 & 1 & 1 & 1 \\
0 & 1 & 0 & 1 & 1 & 0 & 1 \\
0 & 1 & 1 & 0 & 0 & 0 & 0 \\
1 & 0 & 1 & 1 & 0 & 1 & 0 \\
0 & 1 & 0 & 0 & 1 & 0 & \\
};
\end{tikzpicture}
\end{document}
答案1
“覆盖”是下列摘录的结果tikz.code.tex
:
\def\tikz@common@matrix@code{%
\let\tikz@options=\pgfutil@empty%
\let\tikz@mode=\pgfutil@empty%
\tikzset{every cell/.try={\the\pgfmatrixcurrentrow}{\the\pgfmatrixcurrentcolumn}}%
\tikzset{column \the\pgfmatrixcurrentcolumn/.try}%
\ifodd\pgfmatrixcurrentcolumn%
\tikzset{every odd column/.try}%
\else%
\tikzset{every even column/.try}%
\fi%
\tikzset{row \the\pgfmatrixcurrentrow/.try}%
\ifodd\pgfmatrixcurrentrow%
\tikzset{every odd row/.try}%
\else%
\tikzset{every even row/.try}%
\fi%
\tikzset{row \the\pgfmatrixcurrentrow\space column \the\pgfmatrixcurrentcolumn/.try}%
\tikz@options%
}%
如您所见,它会按特定顺序浏览列和行选项。具体来说,它会查看行后列。因此,正如您所说,列的样式通常会被行的样式覆盖。
然而,这个问题远非无法解决。相反,我发现在大多数情况下,直接使用计数\pgfmatrixcurrentcolumn
和是最方便的\pgfmatrixcurrentrow
。这允许你实现你喜欢的任何规则。在你的例子中,你的目标可以通过一些代码来实现,这些代码计算一个整数来标记案例,
\pgfmathtruncatemacro{\itest}{ifthenelse(%
\pgfmatrixcurrentcolumn==7||\pgfmatrixcurrentrow==6,0,%
ifthenelse(isodd(\pgfmatrixcurrentrow),1,2))}
然后使用\ifcase
,
\ifcase\itest
\tikzset{fill=green!10, font=\bfseries}%
\or
\tikzset{fill=yellow!10}%
\or
\fi
这种方法非常强大,因为您可以实现更一般的条件,如模条件,或行和列索引的更一般的函数。
以下是完整的代码。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{cond/.code={%
\pgfmathtruncatemacro{\itest}{ifthenelse(%
\pgfmatrixcurrentcolumn==7||\pgfmatrixcurrentrow==6,0,%
ifthenelse(isodd(\pgfmatrixcurrentrow),1,2))}
%\typeout{\the\pgfmatrixcurrentcolumn,\pgfmatrixcurrentrow->\itest}
\ifcase\itest
\tikzset{fill=green!10, font=\bfseries}%
\or
\tikzset{fill=yellow!10}%
\or
\fi
}}
\begin{document}
\begin{tikzpicture}
\matrix [
matrix of nodes,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={draw, anchor=center, minimum size=.75cm,cond},
column 6/.style={column sep=5pt},
row 5/.style={row sep=5pt},
] {
1 & 1 & 1 & 0 & 1 & 0 & 0 \\
0 & 0 & 1 & 0 & 1 & 1 & 1 \\
0 & 1 & 0 & 1 & 1 & 0 & 1 \\
0 & 1 & 1 & 0 & 0 & 0 & 0 \\
1 & 0 & 1 & 1 & 0 & 1 & 0 \\
0 & 1 & 0 & 0 & 1 & 0 & \\
};
\end{tikzpicture}
\end{document}
附录: 关于这个问题,您还可以安装一些重新排序的样式。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\makeatletter
\tikzset{reorder matrix styles/.style={execute at begin matrix={
\def\tikz@common@matrix@code{%
\let\tikz@options=\pgfutil@empty%
\let\tikz@mode=\pgfutil@empty%
\tikzset{every cell/.try={\the\pgfmatrixcurrentrow}{\the\pgfmatrixcurrentcolumn}}%
\ifodd\pgfmatrixcurrentcolumn%
\tikzset{every odd column/.try}%
\else%
\tikzset{every even column/.try}%
\fi%
\ifodd\pgfmatrixcurrentrow%
\tikzset{every odd row/.try}%
\else%
\tikzset{every even row/.try}%
\fi%
\tikzset{column \the\pgfmatrixcurrentcolumn/.try}%
\tikzset{row \the\pgfmatrixcurrentrow/.try}%
\tikzset{row \the\pgfmatrixcurrentrow\space column \the\pgfmatrixcurrentcolumn/.try}%
\tikz@options%
}%
}}}
\makeatother
\begin{document}
\begin{tikzpicture}[reorder matrix styles]
\matrix [
matrix of nodes,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={draw, anchor=center, minimum size=.75cm},
every odd row/.style={nodes={fill=yellow!10}},
column 6/.style={column sep=5pt},
column 7/.style={nodes={fill=green!10, font=\bfseries}},
row 5/.style={row sep=5pt},
row 6/.style={nodes={fill=green!10, font=\bfseries}}
] {
1 & 1 & 1 & 0 & 1 & 0 & 0 \\
0 & 0 & 1 & 0 & 1 & 1 & 1 \\
0 & 1 & 0 & 1 & 1 & 0 & 1 \\
0 & 1 & 1 & 0 & 0 & 0 & 0 \\
1 & 0 & 1 & 1 & 0 & 1 & 0 \\
0 & 1 & 0 & 0 & 1 & 0 & \\
};
\end{tikzpicture}
\end{document}
这会产生与上面相同的输出。但请注意,这有点儿像 hack,你可以想象到崩溃的情况。