从回答开始这个问题,我修改了代码,以便有 4 种不同类型的单元格(而不是 2 种)。代码如下:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\tikz[%
every cell 0/.style={draw=black
, opacity=.5
, minimum height=0.1em
, outer sep=0pt},
every cell 1/.style={draw=black
, fill=gray
, opacity=.5
, minimum height=0.1em
%, width=0.1em,
%, depth=0.1em
, outer sep=0pt},
every cell 2/.style={draw=red
, fill=gray
, minimum height=0.1em
%, width=0.1em,
%, depth=0.1em
, outer sep=0pt},
every cell 3/.style={draw=black
, fill=gray
, minimum height=0.1em
%, width=0.1em,
%, depth=0.1em
, outer sep=0pt}
]
\foreach \row [count=\y] in {%
{0,0,0,0,0,1,1,1,0,0,0},%
{0,0,0,1,1,1,0,1,1,0,0},%
{0,0,0,1,0,0,0,0,1,0,0},%
{0,0,0,2,0,0,0,1,1,0,0},%
{0,0,0,3,0,0,0,1,0,0,0},%
{0,0,1,1,0,0,0,1,0,0,0},%
{0,0,1,1,0,0,0,1,1,1,1},%
{0,0,1,1,0,0,0,0,0,0,0},%
{1,1,1,1,0,0,0,0,0,0,0}}%
\foreach \cell [count=\x] in \row
\path [every cell, every cell \cell]
(\x,-\y) rectangle ++(1,1);
\end{document}
这是输出(缺少两个外部边):
我想将对应于 2 的单元格放在前景中。实际上,只有在设置了不透明度参数的地方(即类型为 0,2 而不是 3 的单元格),红色边框才可见。特别是,我希望看到红色矩形,而无需为其他单元格设置不透明度。 这里类似的问题。
答案1
一个想法。创建以单元格值命名的图层,例如:
\pgfdeclarelayer{layer0}
\pgfdeclarelayer{layer1}
\pgfdeclarelayer{layer2}
\pgfdeclarelayer{layer3}
并按所需的顺序设置它们,即:
\pgfsetlayers{layer0,layer1,main,layer3,layer2}
绘制图层中的每个单元格layer\cell
。这是实现这个想法的完整代码(请注意,我添加了very thick
单元格 2 的样式):
\documentclass{standalone}
\usepackage{tikz}
\pgfdeclarelayer{layer0}
\pgfdeclarelayer{layer1}
\pgfdeclarelayer{layer2}
\pgfdeclarelayer{layer3}
\pgfsetlayers{layer0,layer1,main,layer3,layer2}
\begin{document}
\tikz[%
every cell 0/.style={draw=black
, opacity=.5
, minimum height=0.1em
, outer sep=0pt},
every cell 1/.style={draw=black
, fill=gray
, opacity=.5
, minimum height=0.1em
, outer sep=0pt},
every cell 2/.style={draw=red, very thick,
, fill=gray
, minimum height=0.1em
, outer sep=0pt},
every cell 3/.style={draw=black
, fill=gray
, minimum height=0.1em
%, width=0.1em,
%, depth=0.1em
, outer sep=0pt}
] {
\foreach \row [count=\y] in {%
{0,0,0,0,0,1,1,1,0,0,0},%
{0,0,0,1,1,1,0,1,1,0,0},%
{0,0,0,1,0,0,0,0,1,0,0},%
{0,0,0,2,0,0,0,1,1,0,0},%
{0,0,0,3,0,0,0,1,0,0,0},%
{0,0,1,1,0,0,0,1,0,0,0},%
{0,0,1,1,0,0,0,1,1,1,1},%
{0,0,1,1,0,0,0,0,0,0,0},%
{1,1,1,1,0,0,0,0,0,0,0},}%
\foreach \cell [count=\x] in \row {
\begin{pgfonlayer}{layer\cell}
\path [every cell, every cell \cell]
(\x,-\y) rectangle ++(1,1);
\end{pgfonlayer}
}
}
\end{document}
结果如下: