Tikz foreach 清單中的 \ldots

Tikz foreach 清單中的 \ldots

我修改了給出的解決方案CFR在這篇文章中使用 Tikz 的路徑圖

我的程式碼如下所示:

\documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta,calc,positioning} 
\begin{document}
 \begin{tikzpicture}
     [
    rect/.style={draw, text centered},
     >={Stealth[]}
     ]
     \node (input) {Input};
     \node [rect, right=of input] (gen) {Generator};
     \node [above right=of gen] (C1) {$Candidate_1$};
     \node [below right=of gen] (Cn) {$Candidate_n$};
     \node [rect, below right=of C1] (eval) {Evaluator};
     \node [right=of eval] (output) {Optimal Output};
     \foreach \i [count=\ino] in {Candidate_2,Candidate\ldots}
     {
      \node at ($(C1)!\ino/3!(Cn)$) (\i) {$\i$};
      \draw [->] (gen) -- (\i);
      \draw [->] (\i) -- (eval);
      }
      \foreach \i/\j in {input/gen,gen/C1,gen/Cn,C1/eval,Cn/eval,eval/output} \draw [->] (\i) -- (\j);
    \end{tikzpicture}
\end{document}

問題出在這一行

\foreach \i [count=\ino] in {Candidate_2,Candidate\ldots}

這使得編譯失敗並出現錯誤:

! Missing \endcsname inserted.                      
<to be read again>        
                   \protect                         
l.20       }              

因為我找不到在該環境中出現點的解決方案。

我基本上想證明你可以有從 1, 2, ..., 到 n 的多個「候選人」。但我怎麼能在那裡有“候選人......”呢?

答案1

您不能使用不可擴展的標記來命名座標;您可以使用計數器\ino(經過適當裝飾以避免與其他座標混淆)。

我還用 修復了格式\mathrm

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,calc,positioning}

\begin{document}

\begin{tikzpicture}[
  rect/.style={draw, text centered},
  >={Stealth[]},
]
\node (input) {Input};
\node [rect, right=of input] (gen) {Generator};
\node [above right=of gen] (C1) {$\mathrm{Candidate}_1$};
\node [below right=of gen] (Cn) {$\mathrm{Candidate}_n$};
\node [rect, below right=of C1] (eval) {Evaluator};
\node [right=of eval] (output) {Optimal Output};
\foreach \i [count=\ino] in {\mathrm{Candidate}_2,\mathrm{Candidate}\ldots}
  {
   \node at ($(C1)!\ino/3!(Cn)$) (c\ino) {$\i$};
   \draw [->] (gen) -- (c\ino);
   \draw [->] (c\ino) -- (eval);
  }
\foreach \i/\j in {input/gen,gen/C1,gen/Cn,C1/eval,Cn/eval,eval/output} \draw [->] (\i) -- (\j);
\end{tikzpicture}

\end{document}

在此輸入影像描述

答案2

我向您建議一個替代解決方案,即tikz matrix.

我用\vdots它代替是Canditate...因為它對我來說看起來更乾淨。

\documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta,calc,positioning, matrix} 

\begin{document}

 \begin{tikzpicture}
     [
    rect/.style={draw, text centered},
     >={Stealth[]}
     ]
      \matrix[%
        matrix of nodes,
        nodes={inner sep=4pt},
        inner sep=0pt,
        column sep=30pt,
        row sep= 10pt,
        ] (C) {%
        & & $\mathrm{Candidate}_{1}$ \\ 
        & & $\mathrm{Candidate}_{2}$ \\[-15pt]
        Input & |[rect]| Generator & & |[rect]| Evaluator & Optimal Output \\[-15pt]
        & & $\vdots$ \\
        & & $\mathrm{Candidate}_{n}$ \\ 
      };
              \foreach \i  in {1,2,5}
      {%
      \draw [->] (C-3-2) -- (C-\i-3);
      \draw [->] (C-\i-3) -- (C-3-4);
      }
      \draw [->] (C-3-1) -- (C-3-2);
      \draw [->] (C-3-4) -- (C-3-5);         
\end{tikzpicture}
\end{document}

在此輸入影像描述

PS = 如果您絕對想要\mathrm{Candidate}\ldots,只需將其替換為\vdots並添加到:4,中。\foreach\foreach \i in {1,2,4,5}

相關內容