使用代码
\documentclass[tikz,convert={size=640}]{standalone}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
\usetikzlibrary{quotes}
\begin{document}
\begin{tikzpicture}[
rect/.style={rectangle},
sum/.style={draw,circle,minimum width=0.2cm,minimum height=0.2cm},
dot/.style={fill,circle,inner sep=1pt,outer sep=0pt},
pics/lowpass/.style args={#1 and #2}{
code={
\node[sum,"-45:-"] (-in) {};
\node[rect,right=0.3cm of -in] (-gain) {#1};
\node[sum,right=0.3cm of -gain] (-sum) {};
\node[dot,right=1.25cm of -sum] (-out) {};
\node[rect,below=0.5cm of $(-sum)!0.5!(-out)$,
"left:#2"{font=\scriptsize,yshift=-0.25cm}] (-int) {$z^{-1}$};
\draw[-latex] (-in) -- (-gain);
\draw[-latex] (-gain) -- (-sum);
\draw (-sum) -- (-out);
\draw[-latex] (-out) |- (-int);
\draw[-latex] (-int) -| (-in);
\draw[-latex] (-sum |- -int) node[xshift=-1.5pt,dot] {} -- (-sum) ;
}
}
]
\pic (lowpass1) at (0,0) {lowpass=$k_e$ and $x_1(k)$};
\pic[right=1cm of lowpass1-out] (lowpass2) {lowpass=$k_c$ and $x_2(k)$};
\end{tikzpicture}
\end{document}
我制作了这张图片
根据绝对和相对位置,下部中心点会偏移 1.5pt。如何避免此行为?当我使用相对定位时,参考点是什么\pic
?
答案1
首先,这可以被认为是主观的,但我发现定位相当令人困惑。如果可以的话,我会对所有节点使用绝对定位,它能提供一定的一致性。
无论如何,这是您的代码的稍微修改后的版本。
输出
代码
\documentclass[tikz, margin=10pt]{standalone}
\usetikzlibrary{positioning,calc,quotes}
\tikzset{
rect/.style={rectangle},
sum/.style={draw,circle,minimum width=0.2cm,minimum height=0.2cm},
dot/.style={fill,circle,inner sep=1pt,outer sep=0pt},
pics/lowpass/.style args={#1 and #2}{
code={
\node[sum,"-45:-"] (-in) {};
\node[rect,right=0.3cm of -in] (-gain) {#1};
\node[sum,right=0.3cm of -gain] (-sum) {};
\node[dot,right=1.25cm of -sum] (-out) {};
\node[rect,below=0.5cm of $(-sum)!0.5!(-out)$] (-int) {$z^{-1}$};
\node[dot, below=0.55cm of -sum,label={below:\scriptsize #2}] {};
\draw[-latex] (-in) -- (-gain);
\draw[-latex] (-gain) -- (-sum);
\draw (-sum) -- (-out);
\draw[-latex] (-out) |- (-int);
\draw[-latex] (-int) -| (-in);
\draw[-latex] (-sum |- -int) -- (-sum);
}
}
}
\begin{document}
\begin{tikzpicture}
\pic (lowpass1) at (0,0) {lowpass=$k_e$ and $x_1(k)$};
\pic[right=1cm of lowpass1-out] (lowpass2) {lowpass=$k_c$ and $x_2(k)$};
\end{tikzpicture}
\end{document}
答案2
这似乎是定位库和相互作用中的一个错误\pic
。一个可能的解决方法是在定义中使用每个节点的中心锚点\pic
:
\documentclass[tikz,convert={size=640}]{standalone}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
\usetikzlibrary{quotes}
\begin{document}
\begin{tikzpicture}[
rect/.style={rectangle},
sum/.style={draw,circle,minimum width=0.2cm,minimum height=0.2cm},
dot/.style={fill,circle,inner sep=1pt,outer sep=0pt},
pics/lowpass/.style args={#1 and #2}{
code={
\begin{scope}[every node/.append style={anchor=center}]
\node[sum,"-45:-"] (-in) {};
\node[rect,right=0.3cm of -in] (-gain) {#1};
\node[sum,right=0.3cm of -gain] (-sum) {};
\node[dot,right=1.25cm of -sum] (-out) {};
\node[rect,below=0.5cm of $(-sum)!0.5!(-out)$,
"left:#2"{font=\scriptsize,yshift=-0.25cm}] (-int) {$z^{-1}$};
\draw[-latex] (-in) -- (-gain);
\draw[-latex] (-gain) -- (-sum);
\draw (-sum) -- (-out);
\draw[-latex] (-out) |- (-int);
\draw[-latex] (-int) -| (-in);
\draw[-latex] (-sum |- -int) node[dot] {} -- (-sum) ;
\end{scope}
}
}
]
\pic (lowpass1) at (0,0) {lowpass=$k_e$ and $x_1(k)$};
\pic[right=1cm of lowpass1-out] (lowpass2) {lowpass=$k_c$ and $x_2(k)$};
\end{tikzpicture}
\end{document}
这会产生所需的行为: