我的代码中是否有错误,或者我发现了一个错误?如果有,\foreach
语句似乎不会增加,但它可以与手动给定的初始值()一起使用。\lastn
initally (value)
\def\lastn
这是我所期望的……
…这就是我得到的……
这是我的 MWE:
\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[every node/.style={draw}]
\node (A) at (0,0) {A};
\node (B) at (2,0) {B};
\node (C) at (4,0) {C};
\node (D) at (6,0) {D};
% DOESN'T WORK
% \foreach \n [remember=\n as \lastn (initially A)] in {B,C,D} {
% \draw [->] (\lastn) -- (\n);
% }
% WORKS
\def\lastn{A}
\foreach \n [remember=\n as \lastn] in {B,C,D} {
\draw [->] (\lastn) -- (\n);
}
\end{tikzpicture}
\end{document}
答案1
它适用于\newforeach
:
\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usepackage{loops}
\begin{document}
\begin{tikzpicture}[every node/.style=draw]
\node (A) at (0,0) {A};
\node (B) at (2,0) {B};
\node (C) at (4,0) {C};
\node (D) at (6,0) {D};
\newforeach \n [remember=\n as \lastn initially A] in {B,C,D}
\draw [->] (\lastn) -- (\n);
\end{tikzpicture}
\end{document}
您也可以尝试
\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usepackage{loops}
\begin{document}
\begin{tikzpicture}
\node at (0,1) [circle,draw,red] {
\foxloop [remember=#1 as \x initially A] {B,C,D,E,F,G,H}{%
$\overrightarrow{\x#1}$\iflastfox.\else,\space\fi
}
};
\end{tikzpicture}
\end{document}