这是我的代码:
\documentclass{standalone}% For the example only, any class will do
\usepackage{tikz}
\usetikzlibrary{positioning}% To get more advances positioning options
\usetikzlibrary{arrows}% To get more arrow heads
\usetikzlibrary{shapes,trees,hobby,calc}
\begin{document}
\tikzstyle{block}=[rectangle, draw=black, rounded corners, ultra thick,
text centered, anchor=north, text=white, text width=1cm, text height=.3cm]
\tikzstyle{bblock}=[block, fill=blue!50]
\begin{tikzpicture}
%nodes
% \pos = position
% \lb = label
% \t = text
\foreach \pos / \lb / \t in {
(0,-1)/S1/$ $,
(0,1)/S2/$ $,
%
(3,-2)/T1/$B$,
(3,1)/T2/$ $,
%
(6,-3)/U1/$ $,
(6,0)/U2/$B_{DP}$,
(6,2)/U3/$ $,
%
(9,-2.5)/V1/$ $,
(9,-1)/V2/$ $,
(9,1.5)/V3/$ $
}
{
\node[bblock] (\lb) at \pos {\t};
}
%arrows
\foreach \f/\t/\b in {
S1/T1/0,
S2/T2/0,
S2/T1/0,
S1/T2/0,
T1/U1/0,
T1/U2/0,
T2/U3/0,
U1/V1/0,
U2/V2/0,
U2/V3/0,
U3/V3/0
}
{
\draw[->, >=stealth', thick] (\f) edge[bend right=\b] (\t);
}
%in arrows
\foreach \t in {
S1,
S2
}
{
\draw[->, >=stealth', thick] ($ (\t) + (-1.5,.4) $) -- (\t);
\draw[->, >=stealth', thick] ($ (\t) + (-1.5,-.4) $) -- (\t);
}
%out arrows
\foreach \t in {
V1,
V2,
V3
}
{
\draw[->, >=stealth', thick] (\t) -- ($ (\t) + (1.5,.4) $);
\draw[->, >=stealth', thick] (\t) -- ($ (\t) + (1.5,-.4) $);
}
\end{tikzpicture}
\end{document}
每个 foreach 循环的最后几行都返回“没有名为 X 的已知形状”,其中 X 是列表中最后出现的形状。如果我添加一些其他形状,它们就会被正确识别。
有谁知道为什么 TikZ 会这样以及如何解决这个问题?
答案1
这是因为括号写在下一行,从而产生了虚假空格。只需在行末进行注释即可。
\documentclass{standalone}% For the example only, any class will do
\usepackage{tikz}
\usetikzlibrary{positioning}% To get more advances positioning options
\usetikzlibrary{arrows}% To get more arrow heads
\usetikzlibrary{shapes,trees,hobby,calc}
\begin{document}
\tikzstyle{block}=[rectangle, draw=black, rounded corners, ultra thick,
text centered, anchor=north, text=white, text width=1cm, text height=.3cm]
\tikzstyle{bblock}=[block, fill=blue!50]
\begin{tikzpicture}
%blob
\path[draw,green,fill=red,thick,use Hobby shortcut,closed=true, opacity=0.2]
(-.5,.3) .. (1.5,2.5) .. (2.8,3.6) .. (4,3) .. (4.2,0) .. (4,-3)
.. (2.8,-3.6) .. (1.5,-2.5) .. (-.5,-.3)
;
\path[draw,red,fill=green,thick,use Hobby shortcut,closed=true, opacity=0.2]
(12.5,.3) .. (10.5,2.5) .. (9.2,3.6) .. (8,3) .. (7.8,0) .. (8,-3)
.. (9.2,-3.6) .. (10.5,-2.5) .. (12.5,-.3)
;
\node[text=red] at (1.8,0) {\Huge S};
\node[text=green] at (10.2,0) {\Huge T};
%nodes
% \pos = position
% \lb = label
% \t = text
\foreach \pos / \lb / \t in {
(0,-1)/S1/$ $,
(0,1)/S2/$ $,
%
(3,-2)/T1/$B$,
(3,1)/T2/$ $,
%
(6,-3)/U1/$ $,
(6,0)/U2/$B_{DP}$,
(6,2)/U3/$ $,
%
(9,-2.5)/V1/$ $,
(9,-1)/V2/$ $,
(9,1.5)/V3/$ $
}
{
\node[bblock] (\lb) at \pos {\t};
}
%arrows
\foreach \f/\t/\b in {
S1/T1/0,
S2/T2/0,
S2/T1/0,
S1/T2/0,
T1/U1/0,
T1/U2/0,
T2/U3/0,
U1/V1/0,
U2/V2/0,
U2/V3/0,
U3/V3/0
}
{
\draw[->, >=stealth', thick] (\f) edge[bend right=\b] (\t);
}
%in arrows
\foreach \t in {%
S1,
S2% comment to avoid spurious space
}
{
\draw[->, >=stealth', thick] ($ (\t) + (-1.5,.4) $) -- (\t);
\draw[->, >=stealth', thick] ($ (\t) + (-1.5,-.4) $) -- (\t);
}
%out arrows
\foreach \t in {
V1,
V2,
V3% comment to avoid spurious space
}
{
\draw[->, >=stealth', thick] (\t) -- ($ (\t) + (1.5,.4) $);
\draw[->, >=stealth', thick] (\t) -- ($ (\t) + (1.5,-.4) $);
}
\end{tikzpicture}
\end{document}
答案2
以下是如何使用\xintFor
(来自包xinttools
): 它修剪项目的空格(前导和尾随),因此不会出现@AndréC 分析的那种问题。
另一方面,当 n-nuple 包含本身也是 n-uple 的元素时,n-uples 列表的语法会变得更加复杂。这里是 3-uples,其第一个组件是配对。我们必须将后者隐藏在括号中。
\xintFor
没有\foreach
诸如1, 2, ..., 10
语法之类的良好功能。
它不使用组,也不使用宏来保存值。
它还经过特别设计,可用于生成表格中的行。
\documentclass{standalone}% For the example only, any class will do
\usepackage{tikz}
\usetikzlibrary{positioning}% To get more advances positioning options
\usetikzlibrary{arrows}% To get more arrow heads
\usetikzlibrary{shapes,trees,hobby,calc}
\usepackage{xinttools}% \xintFor trims spaces...
\begin{document}
\tikzstyle{block}=[rectangle, draw=black, rounded corners, ultra thick,
text centered, anchor=north, text=white, text width=1cm, text height=.3cm]
\tikzstyle{bblock}=[block, fill=blue!50]
\begin{tikzpicture}
%blob
\path[draw,green,fill=red,thick,use Hobby shortcut,closed=true, opacity=0.2]
(-.5,.3) .. (1.5,2.5) .. (2.8,3.6) .. (4,3) .. (4.2,0) .. (4,-3)
.. (2.8,-3.6) .. (1.5,-2.5) .. (-.5,-.3)
;
\path[draw,red,fill=green,thick,use Hobby shortcut,closed=true, opacity=0.2]
(12.5,.3) .. (10.5,2.5) .. (9.2,3.6) .. (8,3) .. (7.8,0) .. (8,-3)
.. (9.2,-3.6) .. (10.5,-2.5) .. (12.5,-.3)
;
\node[text=red] at (1.8,0) {\Huge S};
\node[text=green] at (10.2,0) {\Huge T};
%nodes
% #1 (= \pos) = position
% #2 (= \lb) = label
% #3 (= \t) = text
%\foreach \pos / \lb / \t in {
% (0,-1)/S1/$ $,
\xintForthree #1#2#3 in {% list of 3-tuples
% we *must* hide the parentheses and commas used inside individual
% components of the 3-tuple
( {(0,-1)} , S1 , $ $ ),
( {(0,1)} , S2 , $ $ ),
%
( {(3,-2)} , T1 , $B$ ),
( {(3,1)} , T2 , $ $ ),
%
({(6,-3)}, U1, $ $ ),
({(6,0)} , U2, $B_{DP}$ ),
({(6,2)} , U3, $ $ ),
%
({(9,-2.5)}, V1, $ $ ),
({(9,-1)} , V2, $ $ ),
({(9,1.5)} , V3, $ $ )
}
\do
{
% \node[bblock] (\lb) at \pos {\t};
\node[bblock] (#2) at #1 {#3};
% for checking if spaces go trimmed
% \typeout{\detokenize{#1--#2--#3++}}%
}
%arrows
%\foreach \f/\t/\b in {
\xintForthree #1 #2 #3 in {
(S1, T1, 0),
(S2, T2, 0),
(S2, T1, 0),
(S1, T2, 0),
(T1, U1, 0),
(T1, U2, 0),
(T2, U3, 0),
(U1, V1, 0),
(U2, V2, 0),
(U2, V3, 0),
(U3, V3, 0)
}
\do
{
\draw[->, >=stealth', thick] (#1) edge[bend right=#3] (#2);
}
%in arrows (no problem with end of line space)
\xintFor #1 in {%
S1,
S2
}
\do
{
\draw[->, >=stealth', thick] ($ (#1) + (-1.5,.4) $) -- (#1);
\draw[->, >=stealth', thick] ($ (#1) + (-1.5,-.4) $) -- (#1);
}
%out arrows (no problem with end of line space)
\xintFor #1 in {
V1,
V2,
V3
}
\do
{
\draw[->, >=stealth', thick] (#1) -- ($ (#1) + (1.5,.4) $);
\draw[->, >=stealth', thick] (#1) -- ($ (#1) + (1.5,-.4) $);
}
\end{tikzpicture}
\end{document}
生产
可以\xintForthree
使用#1#2#3
或#1 #2 #3
。但没有#1/#2/#3
语法可以在某个时候添加...