我有一个可以生成以下内容的 TikZ 代码:
使用类似于以下代码片段的内容。可以从问题的答案中获得与此代码片段相对应的完整代码tikz-双块矩阵:
\pgfmathparse{\y-\x<1?"draw":""}
\node[\pgfmathresult](mynode-\x-\y) at (\y,-\x) {$\times$};
但我希望为这些选定的节点设置以下主题。此示例的代码可在另一个问题的答案中找到tikz-标记矩阵中的多个块并使用矩阵数组定义,而不是使用 TikZ 绘制所有内容:
我如何使用 TikZ 做到这一点?这意味着基本上没有边框线,而是将正方形的背景更改为特定的颜色...
更新:以下代码和输出是独立的,并显示了我想要添加背景颜色变化的现有 Tikz 图。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,fit,matrix,arrows,automata,positioning}
\begin{document}
\begin{tikzpicture}
[zeros/.style={},transform shape]
\foreach \y in {1,...,4}{
\foreach \x in {1,...,5}{
\ifnum\x>\numexpr\y+1
\node[zeros] (mynode-\x-\y) at (\y,-\x) {0};
\else
\pgfmathparse{\y-\x<1?"draw":""}
\node[\pgfmathresult](mynode-\x-\y) at (\y,-\x) {$\times$};
\ifnum\x<5
\ifnum\x<\y\draw[<-] (mynode-\x-\y) -- +(0,-0.7);\fi
\ifnum\x=\y\draw[<-] (mynode-\x-\y) -- +(0,-0.7);\fi
\ifnum\y<4\draw[<-] (mynode-\x-\y) -- +(0.7,-0.7);\fi
\fi
\fi
}
}
\node[fit=(mynode-1-1)(mynode-5-4),draw,thick,red,label=90:{Conquer diagonal}]{};
\begin{scope}[xshift=5.2cm]
\foreach \x[remember=\x as \lastx] in {1,...,5}{
\node (myvecs1-\x) at (0,-\x) {$\times$};
\node (myvecs2-\x) at (1,-\x) {$\times$};
\node (myvecs3-\x) at (2,-\x) {$\times$};
\node (myvecs4-\x) at (3,-\x) {$\times$};
\node (myvecs5-\x) at (4,-\x) {$\times$};
\node (myvecsn-\x) at (5,-\x) {$\times$};
\ifnum\x>1
\draw[->] (myvecs1-\x) -- (myvecs1-\lastx);
\draw[->] (myvecs2-\x) -- (myvecs2-\lastx);
\draw[->] (myvecs3-\x) -- (myvecs3-\lastx);
\draw[->] (myvecs4-\x) -- (myvecs4-\lastx);
\draw[->] (myvecs5-\x) -- (myvecs5-\lastx);
\draw[->] (myvecsn-\x) -- (myvecsn-\lastx);
\fi
}
\node[fit=(myvecs1-1)(myvecsn-5),draw,thick,green,label=90:{Apply as deep as possible}] {};
\end{scope}
\end{tikzpicture}
\end{document}
输出结果为:
答案1
你可以这样做:
代码
\documentclass[tikz]{standalone}
\usetikzlibrary{fit}
\begin{document}
\begin{tikzpicture}
[ zeros/.style={},
transform shape
]
\foreach \y in {1,...,4}
{ \foreach \x in {1,...,5}{
\ifnum\x>\numexpr\y+1
\node[zeros] (mynode-\x-\y) at (\y,-\x) {0};
\else
\node(mynode-\x-\y) at (\y,-\x) {$\times$};
\ifnum\x<5
\ifnum\x=\y\fill[red,opacity=0.1] (mynode-\x-\y.south west) rectangle (mynode-\x-\y.north east);\fi
\fi
\ifnum\y=\numexpr\x-1\fill[opacity=0.1] (mynode-\x-\y.south west) rectangle (mynode-\x-\y.north east);\fi
\fi
}
}
\node[fit=(mynode-1-1)(mynode-5-4),draw,thick,red,label=90:{Conquer diagonal}]{};
\end{tikzpicture}
\end{document}