我想绘制一些动力系统,tikz
但是当我需要迭代节点名称时,会出现错误,因为当我需要处理锚点时节点名称已损坏。
我找到了多个关于访问节点的帖子foreach
,例如这个但我找不到任何与锚点有关的东西。
是否有特定的方法来保护循环中的节点名称?
\documentclass[tikz,border = 1mm]{standalone}
\usetikzlibrary{patterns,shapes.geometric,decorations.pathmorphing}
\def\bord{0.1cm}
\def\radius{.2cm}
\tikzset{
spring/.style={thick,decorate,decoration={
zigzag,
segment length = 1mm,
amplitude=0.7mm,
pre length=1mm,
post length=1mm}},
mass/.style={circle,minimum width=2*\radius,anchor=center}
}
\begin{document}
\begin{tikzpicture}[x=1cm,y=1cm,line join=round]
% Bord gauche
\fill [pattern = north east lines] (-.3,-2) rectangle (0,.5);
\draw[thick] (0,-2) -- (0,.5);
% Right edge
\def\borddroit{8}
\fill [fill=black!30!white] (\borddroit,-2) rectangle (\borddroit+.3,.5);
\fill [pattern = north east lines] (\borddroit,-2) rectangle (\borddroit+.3,.5);
\draw[ thick] (\borddroit,-2) -- (\borddroit,.5);
% Masses
\foreach \i in {1,2,...,6}{
\node[mass,fill=blue] (m\i) at (\i,0) {};
}
\node[mass] (m0) at (0,0) {};
\node[mass,fill=red] (m7) at (7,0) {};
% Springs
% Working plot
\draw[spring] (m0.center) -- (m1.west);
% Working \foreach without anchor
\foreach \i in {0,...,6}{
\pgfmathsetmacro{\j}{\i+1}
\draw[spring] (m\i) -- (m\j) node[midway, above] (kuu\i) {$k$};
}
% Non working foreach beacause node name is broken
%\foreach \i in {0,...,6}{
% \pgfmathsetmacro{\j}{\i+1}
% \draw[spring] (m\i.east) -- (m\j.west) node[midway, above] (kuu\i) {$k^{\uu}$};
%}
\end{tikzpicture}
\end{document}
答案1
线条
\draw[spring] (m\i) -- (m\j) node[midway, above] (kuu\i) {$k$}; \draw[spring] (m\i.east) -- (m\j.west) node[midway, above] (kuu\i) {$k^{quu}$};
给出相同的结果(除了节点内容,在第二种情况下未定义)。
反而
\foreach \i in {0,...,6}{ \pgfmathsetmacro{\j}{\i+1} \draw[spring] (m\i) -- (m\j) node[midway, above] (kuu\i) {$k$};
你可以写
\foreach \i [count=\j from 1] in {0,...,6}{ \draw[spring] (m\i.east) -- (m\j.west) node[midway, above] (kuu\i) {$k^{\uu}$};
因为没有定义,所以
\uu
它不起作用。用它替换u
会得到\documentclass[tikz,border = 1mm]{standalone} \usetikzlibrary{patterns,shapes.geometric,decorations.pathmorphing} \def\bord{0.1cm} \def\radius{.2cm} \tikzset{ spring/.style={thick,decorate,decoration={ zigzag, segment length = 1mm, amplitude=0.7mm, pre length=1mm, post length=1mm}}, mass/.style={circle,minimum width=2*\radius,anchor=center} } \begin{document} \begin{tikzpicture}[x=1cm,y=1cm,line join=round] % Bord gauche \fill [pattern = north east lines] (-.3,-2) rectangle (0,.5); \draw[thick] (0,-2) -- (0,.5); % Right edge \def\borddroit{8} \fill [fill=black!30!white] (\borddroit,-2) rectangle (\borddroit+.3,.5); \fill [pattern = north east lines] (\borddroit,-2) rectangle (\borddroit+.3,.5); \draw[ thick] (\borddroit,-2) -- (\borddroit,.5); % Masses \foreach \i in {1,2,...,6}{ \node[mass,fill=blue] (m\i) at (\i,0) {}; } \node[mass] (m0) at (0,0) {}; \node[mass,fill=red] (m7) at (7,0) {}; % Springs % Working plot \draw[spring] (m0.center) -- (m1.west); % Working \foreach without anchor \foreach \i [count=\j from 1] in {0,...,6}{ \draw[spring] (m\i.east) -- (m\j.west) node[midway, above] (kuu\i) {$k^{u}$}; } \end{tikzpicture} \end{document}
无关:
上面的代码(只更正了代码中的两行)可以写得稍微短一些,更简洁一些(只用一个\foreach
循环):
\documentclass[tikz,border = 3mm]{standalone}
\usetikzlibrary{decorations.pathmorphing,
patterns, positioning,
quotes,
shapes.geometric}
\def\radius{.2cm}
\begin{document}
\begin{tikzpicture}[
node distance = 5mm and 10mm,
bord/.style = {rectangle, fill=#1, %inner sep=0pt,
pattern = north east lines,
minimum width=5mm, minimum height=25mm,
yshift=15mm,
node contents={}},
mass/.style = {circle, minimum width=2*\radius, fill=#1},
spring/.style = {thick,decorate,decoration={zigzag,
segment length = 1mm,
amplitude=0.7mm,
pre length=1mm, post length=1mm}
},
]
% Bord gauche
\coordinate (m0) at (\radius,0);
\node (m00) [bord=white, below left=5mm and 0mm of m0];
\draw[thick] (m00.north east) -- (m00.south east);
% Working \foreach without anchor
\foreach \i [count=\j from 1] in {0,...,6}
{
\ifnum\j<7
\node[mass=blue] (m\j) at (\j,0) {};
\else
\node[mass=red] (m\j) at (\j,0) {};
\fi
\draw[spring] (m\i) to ["$k^{u}$"] (m\j) ;
}
% Right edge
\node (m8) [bord=gray!30, below right=of m7.center];
\draw[thick] (m8.north west) -- (m8.south west);
\end{tikzpicture}
\end{document}
结果是: