foreach
我正在使用绘制具有不同颜色的节点链tikz
。现在我想添加一个分支,也使用foreach
。但是,在使用 构建的链中启动分支foreach
并不像我预期的那样工作:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{chains}
\usetikzlibrary{scopes}
\begin{document}
\begin{tikzpicture}[node distance=2mm,
every node/.style={shape=rectangle,minimum size=1cm},
]
{ [start chain=values going below]
\foreach \m in {0,...,8}
{
\node[on chain] {\m};
\ifnum\m=3
{
{ [start branch=stuff going right]
\node[on chain] {A};
}
}
\fi
}
}
\end{tikzpicture}
\end{document}
所有节点都按顺序排列在彼此下方0 1 2 3 A 4 5 6 7 8
。 似乎[start branch=stuff going right]
没有将分支向右延伸的效果。 当我用另一个替换手动插入 A 节点时,也会发生同样的情况foreach
。
当我手动构建链条时,一切都绘制正确,就像
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{chains}
\usetikzlibrary{scopes}
\begin{document}
\begin{tikzpicture}[node distance=2mm,
every node/.style={shape=rectangle,minimum size=1cm},
]
{ [start chain=values going below]
\node[on chain] {1};
\node[on chain] {2};
\node[on chain] {3};
{ [start branch=stuff going right]
\node[on chain] {A};
}
\node[on chain] {4};
}
\end{tikzpicture}
\end{document}
我的代码或对的理解有误吗foreach
?
有解决方法吗?(我需要或等效evaluate
的选项foreach
,请参阅我上面链接的问题)
编辑:我也尝试在里面使用 amatrix
和 a foreach
,但是foreach
关闭}
会干扰matrix
代码。
完成了!以下代码使用链(包括分支)、颜色计算以及使用垂直线放置物品来生成如下所示的结果。
\documentclass{standalone}
\usepackage[rgb]{xcolor} % tikz doesn't support Hsb
\usepackage{tikz}
\usetikzlibrary{chains}
\usetikzlibrary{scopes} % needed by the chains lib
\begin{document}
\begin{tikzpicture}
{ [start chain=values going below,node distance=2mm,
every node/.style={shape=rectangle,minimum size=1cm}]
\foreach \n [evaluate=\n as \value using 1-\n*0.125] in {0,...,8}
{
\definecolor{tmpca}{Hsb}{0,0,\value}
\node[on chain,fill=tmpca] {};
\ifnum\n=3
{
\begin{scope}[start branch=saturations going right]
\foreach \m [evaluate=\m as \saturation using \m*0.125] in {1,...,8}
{
\definecolor{tmpcb}{Hsb}{0,\saturation,\value}
\node[on chain,fill=tmpcb] {};
}
\end{scope}
}
\fi
}
}
\draw (values-1.south west) rectangle (values-1.north east);
\node[anchor=south,rotate=90] at (values-5.west) {value scale (light/dark contrast)};
\node[anchor=south west] at (values/saturations-3.north west) {chroma scale (saturation contrast)};
\draw (values/saturations-3.south west) node[anchor=north west] (chroma-label) {gray to full chroma};
\draw (chroma-label)[line width=1pt,->] -- (chroma-label.east -| values/saturations-end.west);
\draw (chroma-label.south west) node [anchor=north west] {values of each color remain equal to gray};
\node[anchor=south east,text width=8cm]
at (values-end.south -| values/saturations-end.east)
{This is a Ti\emph{k}Z implementation of a figure found in \textsc{Puhalla, M.}:
\textit{Perceiving hierarchy through intrinsic color structure}.
Visual Communication 7 (2008), Nr. 2, pp 199--228};
\end{tikzpicture}
\end{document}
我不喜欢红色方块的灰色印象,它们似乎与左边的灰色方块不匹配。我已将问题的这一方面转移到https://graphicdesign.stackexchange.com/questions/2204/gray-to-color-conversion。
答案1
该库引入了较长的scopes
简写。显然,这种简写并不完美:如果使用常规符号,则示例有效:{ [options] <...> }
\begin{scope}[options] <...> \end{scope}
\begin{scope}
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{chains}
\begin{document}
\begin{tikzpicture}[node distance=2mm,
every node/.style={shape=rectangle,minimum size=1cm},
,start chain=values going below]
\foreach \m in {0,...,6}
{
\node[on chain] {\m};
\ifnum\m=3
\begin{scope}[start branch=stuff going right]
\node[on chain] {A};
\end{scope}
\fi
}
\end{tikzpicture}
\end{document}