TikZ:了解 calc 库的用法

TikZ:了解 calc 库的用法

对于以下 MWE,我需要将块放置在(1)和(2)(yaw) {C}之间的中点和 之间的中间位置。(output)(integrator)(sum2)

那么,我该如何修正这个语法\node [block] (yaw) at ([yshift=-2cm]$(integrator)+0.5*{(output)-(integrator)}!0.5!(sum2)$) {C};以使其正常工作?

\documentclass{article}
\usepackage{tikz,mathtools,amssymb}
\usetikzlibrary{shapes,arrows,positioning,calc}

\begin{document}

\tikzset{
block/.style = {draw, fill=white, rectangle, minimum height=3em, minimum width=3em},
tmp/.style  = {coordinate}, 
sum/.style= {draw, fill=white, circle, node distance=1cm},
input/.style = {coordinate},
output/.style= {coordinate},
pinstyle/.style = {pin edge={to-,thin,black}
}
}

\begin{tikzpicture}[auto, node distance=2cm,>=latex',align=center]
     \node [sum] (sum2) {};
    \node [block, right = 1cm of sum2](ractuator){$\frac{2}{s+2}$};
    \node [block, right = 1cm of ractuator,] (vdynamics) {$\frac{-0.125(s+0.437)}{(s+1.29)(s+0.193)}$};
    \node [block, right = 1cm of vdynamics,] (integrator) {$\frac{1}{s}$};
    \node [output, right = 1.5cm of integrator] (output) {};
    \node [block] (yaw) at ([yshift=-2cm]$(integrator)+0.5*{(output)-(integrator)}!0.5!(sum2)$) {C};
    %
    \draw [->] (ractuator) -- (vdynamics);
    \draw [->] (vdynamics) -- (integrator);
    \draw [->] (integrator) -- node[name=heading]{$\Psi(s)$} (output);
    \end{tikzpicture}

\end{document}

此外,是否可以在\node [tmp, below = 2cm of ($(output)!0.5!(integrator)$) ] (tmp1) {};不创建辅助节点/坐标的情况下创建新节点?

答案1

我不知道如何理解复杂的表达式,calc但与其试图理解如何编写这样的表达式,我认为使用辅助坐标并解决问题更容易:

\documentclass{article}
\usepackage{tikz,mathtools,amssymb}
\usetikzlibrary{shapes,arrows,positioning,calc}

\begin{document}

\tikzset{
block/.style = {draw, fill=white, rectangle, minimum height=3em, minimum width=3em},
tmp/.style  = {coordinate}, 
sum/.style= {draw, fill=white, circle, node distance=1cm},
input/.style = {coordinate},
output/.style= {coordinate},
pinstyle/.style = {pin edge={to-,thin,black}
}
}

\begin{tikzpicture}[auto, node distance=2cm,>=latex',align=center]
     \node [sum] (sum2) {};
    \node [block, right = 1cm of sum2](ractuator){$\frac{2}{s+2}$};
    \node [block, right = 1cm of ractuator,] (vdynamics) {$\frac{-0.125(s+0.437)}{(s+1.29)(s+0.193)}$};
    \node [block, right = 1cm of vdynamics,] (integrator) {$\frac{1}{s}$};
    \node [output, right = 1.5cm of integrator] (output) {};
    \coordinate (aux) at ($(integrator.east)!.5!(output)$);
    \node [block] (yaw) at ([yshift=-2cm]$(aux)!0.5!(sum2)$) {C};
    \draw (aux) |- (yaw);
    \draw (yaw)-|(sum2);
    %
    \draw [->] (ractuator) -- (vdynamics);
    \draw [->] (vdynamics) -- (integrator);
    \draw [->] (integrator) -- node[name=heading]{$\Psi(s)$} (output);
    \end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

您的方法不起作用,因为您尝试在应该使用{和的地方使用和。您绝对可以在没有辅助坐标的情况下做到这一点,实际上甚至不需要。}($$)calc

\documentclass{article}
\usepackage{tikz,mathtools,amssymb}
\usetikzlibrary{shapes,arrows,positioning,calc}

\begin{document}

\tikzset{
block/.style = {draw, fill=white, rectangle, minimum height=3em, minimum width=3em},
tmp/.style  = {coordinate}, 
sum/.style= {draw, fill=white, circle, node distance=1cm},
input/.style = {coordinate},
output/.style= {coordinate},
pinstyle/.style = {pin edge={to-,thin,black}
}
}

\begin{tikzpicture}[auto, node distance=2cm,>=latex',align=center]
     \node [sum] (sum2) {};
    \node [block, right = 1cm of sum2](ractuator){$\frac{2}{s+2}$};
    \node [block, right = 1cm of ractuator,] (vdynamics) {$\frac{-0.125(s+0.437)}{(s+1.29)(s+0.193)}$};
    \node [block, right = 1cm of vdynamics,] (integrator) {$\frac{1}{s}$};
    \node [output, right = 1.5cm of integrator] (output) {};
    \node [block] (yaw) at 
    ($(0,-2cm)+($(output)!0.5!(integrator)$)!0.5!(sum2)$) {C};
    %
    \draw [->] (ractuator) -- (vdynamics);
    \draw [->] (vdynamics) -- (integrator);
    \draw [->] (integrator) -- node[name=heading]{$\Psi(s)$} (output)
    coordinate[midway] (aux);
    \draw (aux) |- (yaw) -| (sum2);
\end{tikzpicture}

\bigskip

\begin{tikzpicture}[auto, node distance=2cm,>=latex',align=center]
     \node [sum] (sum2) {};
    \node [block, right = 1cm of sum2](ractuator){$\frac{2}{s+2}$};
    \node [block, right = 1cm of ractuator,] (vdynamics) {$\frac{-0.125(s+0.437)}{(s+1.29)(s+0.193)}$};
    \node [block, right = 1cm of vdynamics,] (integrator) {$\frac{1}{s}$};
    \node [output, right = 1.5cm of integrator] (output) {};
    \node [block] (yaw) at 
    ([yshift=-2cm]barycentric cs:output=1,integrator=1,sum2=2) {C};
    %
    \draw [->] (ractuator) -- (vdynamics);
    \draw [->] (vdynamics) -- (integrator);
    \draw [->] (integrator) -- node[name=heading]{$\Psi(s)$} (output)
    coordinate[midway] (aux);
    \draw (aux) |- (yaw) -| (sum2);
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案3

这里,为了简化代码,我将 替换(integrator)(A),替换(output)(B)(sum2)替换为(C)。 替换为 有两点不正确 ($ (A) + 0.5*{ (B)-(A) }!0.5!(C) $)

  • 首先,我认为您不能将{}calc 包用于坐标部分。对我来说,它只适用于标量。所以($ {2+2}*(A) $)会计算,但不会($ 2*{(A)+(B)} $)(或者我错了?)

  • 第二件事是,这个公式似乎与您想要的点不对应。我有点明白您想从 (A) 开始,“移动”到 [AB] 的中间并继续这样做,但您混合了相对 (BA) 和绝对定位 (C)。一个正确的公式应该是($ { (A) + 0.5*{(B)-(A)} }!0.5!(C) $)。但由于 tikz 无法进行计算,因此您必须给出扩展公式:($ .25*(A) + .25*(B) + .5*(C)$)

另一种方法是($ (A) !.5! (B) !.5! (C) $)。在这里,我们取(A)和(B)的中间值,然后取结果和(C)的中间值。

我希望这可以解答您的疑问!

您可以在此处测试这三种解决方案(最后一种具有临时坐标):

\documentclass[tikz,margin=10pt]{standalone}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}[line width=1]

  \draw[black!10] (0,0) grid (4,4);
  \node (A) at (1,1) {A};
  \node (B) at (3,1) {B};
  \node (C) at (2,3) {C};

  %% 1
  \draw[red] ($ (A) !.5! (B) !.5! (C) $) circle (.05);
  %% 2
  \draw[orange] ($ .25*(A) + .25*(B) + .5*(C) $) circle (.1);
  %% 3
  \coordinate (foo) at ($ (A) !.5! (B) $);
  \draw[yellow] ($ (foo) !.5! (C) $) circle (.15);

\end{tikzpicture}
\end{document}

这使

结果

答案4

减少代码的一种方法:

  • 使用 tikz 库chains在链中放置节点并通过宏在它们之间画线join
  • 节点“c”位于节点下方的反馈简单步骤中vdynamics
  • 直接将坐标放入图像中,而不是通过节点
  • 坐标可以包含标签,利用这一点来获取标签$\Psi$
  • 定义节点距离,然后使用它所有节点定位

    \documentclass{article}
    \usepackage{tikz,nccmath,amssymb}
    \usetikzlibrary{arrows,
                    calc, chains,
                    positioning,
                    shapes}
    
    \begin{document}
    
    \tikzset{
      block/.style  = {draw, fill=white, rectangle, minimum size=3em,
                       on chain, join=by ->},
        sum/.style  = {draw, fill=white, circle},
    }
    \makeatletter
    \tikzset{suspend join/.code={\def\tikz@after@path{}}}   % <--- for dicountinue of jon macro
    \makeatother
    
        \begin{tikzpicture}[
        node distance = 0.5cm and 1cm,
          start chain = going right,
                    > = latex']
    \coordinate (in);
    \node [sum,right=of in, on chain] (sum2)    {};
    \node [block]   (ractuator)     {$\mfrac{2}{s+2}$};
    \node [block]   (vdynamics)     {$\mfrac{-0.125(s+0.437)}{(s+1.29)(s+0.193)}$};
    \node [block]   (integrator)    {$\mfrac{1}{s}$};
    \coordinate[right=of integrator] (out)      {};
    \node [block, suspend join,
           below = of vdynamics]  (yaw) {C};
        %
    \draw[->]   (in) -- (sum2);
    \draw[->]   (integrator) -- coordinate[label=$\Psi(s)$] (psi) (out);
    \draw[->]   (psi) |- (yaw);
    \draw[->]   (yaw) -| (sum2);
        \end{tikzpicture}
    \end{document}
    

在此处输入图片描述

离题:在包\mfrac中定义的分数(中等大小的分数)nccmath

相关内容