我在 tikz 中格式化矩阵时遇到了一个(可能很简单)问题。在第一步中,我通常使用矩阵环境中的节点填充矩阵。
完成矩阵后,我使用 fit 函数绘制一个矩形形状,以便用颜色标记矩阵中的某些列(我的意思是标记节点的特定排列)。为此,我使用行中的第一个节点作为锚点,并在矩阵末尾使用第二个节点。对于锚点,我仅使用矩阵内节点的名称。
但是,当我在绘制节点之后绘制阴影矩形时,矩形被放置在节点的“前面”,因此隐藏了后面的所有文本。由于我使用节点名称作为锚点,因此无法将绘制矩形的代码放在矩阵代码之前。
我搜索了 PGF 手册,似乎有类似“路径后面”和“路径前面”的内容,但这似乎只在路径命令中有效。
当然,我可以在开始时只画一个矩形,但每次更改矩阵时,我都必须调整锚点。使用节点作为锚点可以实现完美对齐。
您可以测试以下代码来了解我想要实现的目标:
\documentclass{article}
\usepackage{tikz,amsmath, amssymb,bm,color}
\usepackage[margin=0cm,nohead]{geometry}
\usepackage[active,tightpage]{preview}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{calc}
\usetikzlibrary{fit,matrix}
\usetikzlibrary{shadings}
\begin{tikzpicture} [auto, remember picture, block/.style ={ rectangle,
draw, fill=white, text centered, minimum height=15mm, text width=12em,
fill=green}]
\matrix (table) [column sep=.8cm,row sep=.5cm, ampersand replacement=\&,
nodes in empty cells, in front of path]
{
% row 1
\node [block] (11) {\textbf{Kapitelstruktur der Arbeit}};\&
\node [block] (12) {Theorie}; \&
\node [block] (13) {\\Empirie}; \&
\node [block] (14) {\textbf{Resultat}}; \\
% row 2
\node [block] (kap1) {Kapitel 1\\Problembeschreibung und Zielsetzung};
\& \node [block] (22) {Prior theoretical knowledge};
\& \node [block] (23) {Prior theoretical knowledge};
\& \node [block] (24) {Prior theoretical knowledge};
\\
% row 3
\node [block] (kap2) {Kapitel 2\\Grundlagen und Definitionen}; \&
\node [block] (step1) {Prior theoretical knowledge};
\& \node [block] (33) {Prior theoretical knowledge} ;
\& \node [block] (34) {Prior theoretical knowledge};\\
};
%%%% THIS SHOULD APPEAR BEHIND THE NODES OF THE MATRIX%%%%
\node[fit=(12)(33), draw, minimum height=1cm, fill=black, fill opacity=.8,
text=white]{should be behind nodes};
%%%% THIS SHOULD APPEAR BEHIND THE NODES OF THE MATRIX%%%%
\end{tikzpicture}
\end{document}
在此先非常感谢您的帮助。
答案1
您可以用backgrounds
它来做这件事。
\documentclass[border=3.14mm,x11names,dvipsnames,svgnames]{standalone}
\usepackage{tikz,amsmath, amssymb,bm,color}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{calc}
\usetikzlibrary{fit,matrix}
\usetikzlibrary{shadings}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}[auto, remember picture, block/.style ={ rectangle,
draw, fill=white, text centered, minimum height=15mm, text width=12em,
fill=green}]
\matrix (table) [column sep=.8cm,row sep=.5cm, ampersand replacement=\&,
nodes in empty cells, in front of path]
{
% row 1
\node [block] (11) {\textbf{Kapitelstruktur der Arbeit}};\&
\node [block] (12) {Theorie}; \&
\node [block] (13) {\\Empirie}; \&
\node [block] (14) {\textbf{Resultat}}; \\
% row 2
\node [block] (kap1) {Kapitel 1\\Problembeschreibung und Zielsetzung};
\& \node [block] (22) {Prior theoretical knowledge};
\& \node [block] (23) {Prior theoretical knowledge};
\& \node [block] (24) {Prior theoretical knowledge};
\\
% row 3
\node [block] (kap2) {Kapitel 2\\Grundlagen und Definitionen}; \&
\node [block] (step1) {Prior theoretical knowledge};
\& \node [block] (33) {Prior theoretical knowledge} ;
\& \node [block] (34) {Prior theoretical knowledge};\\
};
%%%% THIS SHOULD APPEAR BEHIND THE NODES OF THE MATRIX%%%%
\begin{scope}[on background layer]
\node[fit=(12)(33), draw, minimum height=1cm, fill=black, fill opacity=.8,
text=white]{should be behind nodes};
\end{scope}
%%%% THIS SHOULD APPEAR BEHIND THE NODES OF THE MATRIX%%%%
\end{tikzpicture}
\end{document}