TikZ - 灵活的箭头定位

TikZ - 灵活的箭头定位

以下 MWE 正在转换为所需的tikz图片,但代码对于调整不是很有用。特别是通过使用毫米信息定义坐标,一些箭头的定位非常糟糕。虚线框也是如此。有人知道更智能的解决方案来定义这些坐标吗?

% document class
\documentclass{scrreprt}
\usepackage[utf8]{inputenc}     

% layout (geometry, single page, margins)
\usepackage[a4paper,left=100pt,right=100pt,top=70pt,bottom=70pt,includeheadfoot]{geometry}

% colorized fonts and frames
\usepackage[table]{xcolor}  
\definecolor{anti-flashwhite}{rgb}{0.95, 0.95, 0.96}

% flow chart diagrams
\usepackage{tikz}
\usetikzlibrary{arrows.meta,babel,positioning,graphs}
\usepackage[european,siunitx]{circuitikz}

\tikzstyle{block}       = [draw, fill=anti-flashwhite, rectangle, minimum height=10mm, minimum width=10mm]
\tikzstyle{sum}         = [draw, fill=anti-flashwhite, circle,inner sep=1mm, node distance=20mm]
\tikzstyle{input}       = [coordinate]
\tikzstyle{output}      = [coordinate]
\tikzstyle{branch}      = [circle,inner sep=0pt,minimum size=1mm,fill=black,draw=black]
\tikzstyle{pinstyle}    = [pin edge={<-,thin,black},pin distance=10mm]
\tikzstyle{skip loop}   = [to path={-- ++(0,#1) -| (\tikztotarget)}]
\tikzstyle{hv path}     = [style={to  path={-|  (\tikztotarget)}}]
\tikzstyle{vh path}     = [style={to  path={|-  (\tikztotarget)}}]


\begin{document}

\begin{center}
\begin{tikzpicture}[auto, node distance=20mm,>=latex']
    % inputs
    \node [input]   (inputx)                                                        {};
    \node [input]   (inputy)        [below of =inputx, node distance=40mm]          {};

    % sums
    \node [sum]     (sumx1)         [right of=inputx, node distance=15mm]           {};
    \node [sum]     (sumy1)         [right of=inputy, node distance=15mm]           {};

    % controllers
    \node [block]   (reglerx)       [right of=sumx1]                                {Regler $X$};
    \node [block]   (reglery)       [right of=sumy1]                                {Regler $Y$};

    % magnets
    \node [block]   (magnet1)       [right of=reglerx, node distance=40mm]          {Magnet 1};
    \node [block]   (magnet2)       [below of=magnet1]                              {Magnet 2};
    \node [block]   (magnet3)       [right of=reglery, node distance=40mm]          {Magnet 3};

    % sums
    \node [sum]     (sumx2)         [right of=magnet1]                              {};
    \node [sum]     (sumy2)         [right of=magnet3]                              {};

    % pendulums      
    \node [block]   (pendulumx)     [right of=sumx2]                                {Pendel $X$};
    \node [block]   (pendulumy)     [right of=sumy2]                                {Pendel $Y$};

    % branches      
    \node [branch]  (valuex)        [right of=pendulumx, node distance=15mm]        {};
    \node [branch]  (valuey)        [right of=pendulumy, node distance=15mm]        {}; 

    \node [branch]  (dotx)          [above of=valuex]                               {};
    \node [branch]  (doty)          [below of=valuey]                               {};

    \node [branch]  (nonlinearx)    at (55mm,30mm)                                  {};
    \node [branch]  (nonlineary)    at (58mm,-70mm)                                 {};

    % outputs
    \node [output]  (outputx)       [right of=valuex, node distance=10mm]           {};
    \node [output]  (outputy)       [right of=valuey, node distance=10mm]           {};


    % differentiator
    \node [block]   (diffx)         [above of=magnet1]      {$\frac{d x}{d t}$};
    \node [block]   (diffy)         [below of=magnet3]      {$\frac{d y}{d t}$};

    % arrows    
    \draw [->]      (inputx) --     node {$x_{soll}$}   (sumx1);
    \draw [->]      (inputy) --     node {$y_{soll}$}   (sumy1);

    \draw [->]      (sumx1)         --                  (reglerx);
    \draw [->]      (sumy1)         --                  (reglery);

    \draw [->]      (reglerx)       --                  (magnet1);
    \draw [->]      (reglerx.east)  --                  (magnet2.west);
    \draw [->]      (reglerx.east)  --                  (magnet3.west);
    \draw [->]      (reglery.east)  --                  (magnet1.west);     
    \draw [->]      (reglery.east)  --                  (magnet2.west);
    \draw [->]      (reglery)       --                  (magnet3);

    \draw [->]      (magnet1.east)  --                  (sumx2);
    \draw [->]      (magnet1.east)  --                  (sumy2);
    \draw [->]      (magnet2.east)  --                  (sumx2);
    \draw [->]      (magnet2.east)  --                  (sumy2);
    \draw [->]      (magnet3.east)  --                  (sumx2);
    \draw [->]      (magnet3.east)  --                  (sumy2);

    \draw [->]      (sumx2)         --                  (pendulumx);
    \draw [->]      (sumy2)         --                  (pendulumy);

    \draw           (pendulumx)     --                  (valuex);
    \draw           (pendulumy)     --                  (valuey);

    \draw [->]      (valuex)        --      node {$x$}  (outputx);
    \draw [->]      (valuey)        --      node {$y$}  (outputy);

    \draw           (valuex)        --                  (dotx);
    \draw           (valuey)        --                  (doty); 

    \draw [->]      (dotx)          --                  (diffx);
    \draw [->]      (doty)          --                  (diffy);

    \graph{(diffx)  ->[hv path]                         (reglerx)};
    \graph{(diffy)  ->[hv path]                         (reglery)};

    \coordinate (x1) at (65.5mm,3mm);
    \coordinate (y1) at (65.5mm,-3mm);
    \coordinate (x2) at (65.6mm,-17mm);
    \coordinate (y2) at (65.7mm,-23mm);
    \coordinate (x3) at (65.6mm,-37mm);
    \coordinate (y3) at (65.7mm,-43mm);


    \graph{(nonlinearx) ->[vh path, color=red]          (x1)};
    \graph{(nonlinearx) ->[vh path, color=red]          (x2)};
    \graph{(nonlinearx) ->[vh path, color=red]          (x3)};

    \graph{(nonlineary) ->[vh path, color=red]          (y1)};
    \graph{(nonlineary) ->[vh path, color=red]          (y2)};
    \graph{(nonlineary) ->[vh path, color=red]          (y3)};    

    % feedback loops
    \path (dotx)    edge  [->,skip  loop=10mm]          (sumx1);
    \path (doty)    edge  [->,skip  loop=-10mm]         (sumy1);

    % labels
    \coordinate [label=$\dot{x}$] (dotxsign) at         (32mm,6mm);
    \coordinate [label=$\dot{y}$] (dotysign) at         (32mm,-52mm);   

    % dashed frame
    \draw (52mm,35mm)       [dashed,thick, color=blue]  --  (132mm,35mm);
    \draw (132mm,35mm)      [dashed,thick, color=blue]  --  (132mm,-75mm);
    \draw (132mm,-75mm)     [dashed,thick, color=blue]  --  (52mm,-75mm);
    \draw (52mm,-75mm)      [dashed,thick, color=blue]  --  (52mm,35mm);

    % controller communication
    \coordinate (x4) at (32mm,-5mm);
    \coordinate (x5) at (38mm,-5mm);
    \coordinate (y4) at (32mm,-35mm);
    \coordinate (y5) at (38mm,-35mm);

    \draw [->, ultra thick,dotted]      (x4)            --          (y4);   
    \draw [->, ultra thick,dotted]      (y5)            --          (x5);   

\end{tikzpicture}
\end{center}

\end{document}

答案1

您可以使用 和 来定义您的目标xshiftyshift您的蓝色框架更容易绘制为rectangle

% arara: pdflatex

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\usepackage[utf8]{inputenc}     
\definecolor{myColor1}{named}{red}
\definecolor{myColor2}{named}{blue}
\definecolor{anti-flashwhite}{rgb}{0.95, 0.95, 0.96}
\usepackage[european,siunitx]{circuitikz}
\newcommand{\od}[3][]{\frac{\dif^{#1}#2}{\dif#3^{#1}}}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[%
,auto
,>=latex'
,block/.style={text height=1.5ex,text depth=.25ex,draw, fill=anti-flashwhite, rectangle, minimum height=10mm, minimum width=10mm}
,sum/.style={draw, fill=anti-flashwhite, circle,inner sep=1mm, node distance=20mm}
,input/.style={coordinate}
,output/.style={coordinate}
,branch/.style={circle,inner sep=0pt,minimum size=1mm,fill=black,draw=black}
,skip loop/.style={to path={-- ++(0,#1) -| (\tikztotarget)}}
,frame/.style={dashed,thick,myColor2}
]
\draw
    % inputs
    node [input]   (inputx)                                                        {}
    node [input]   (inputy)        [below = 40mm of inputx]          {}
    % sums
    node [sum]     (sumx1)         [right = 15mm of inputx]           {}
    node [sum]     (sumy1)         [right = 15mm of inputy]           {}
    % controllers
    node [block]   (reglerx)       [right = of sumx1]                                {Regler $X$}
    node [block]   (reglery)       [right = of sumy1]                                {Regler $Y$}
    % magnets
    node [block]   (magnet1)       [right = 30mm of reglerx]          {Magnet 1}
    node [block]   (magnet2)       [below = of magnet1]                              {Magnet 2}
    node [block]   (magnet3)       [right = 30mm of reglery]          {Magnet 3}
    % sums
    node [sum]     (sumx2)         [right = of magnet1]                              {}
    node [sum]     (sumy2)         [right = of magnet3]                              {}
    % pendulums      
    node [block]   (pendulumx)     [right = of sumx2]                                {Pendel $X$}
    node [block]   (pendulumy)     [right = of sumy2]                                {Pendel $Y$}
    % branches      
    node [branch]  (valuex)        [right = of pendulumx]        {}
    node [branch]  (valuey)        [right = of pendulumy]        {}
    node [branch]  (dotx)          [above = 19mm of valuex]                               {}
    node [branch]  (doty)          [below = 19mm of valuey]                               {}
    node [branch]  (nonlinearx)    [above = 24.5mm of reglerx, xshift=20mm]                                  {}
    node [branch]  (nonlineary)    [below = 24.5mm of reglery, xshift=25mm]                                 {}
    % outputs
    node [output]  (outputx)       [right = 15mm of valuex]           {}
    node [output]  (outputy)       [right = 15mm of valuey]           {}
    % differentiator
    node [block]   (diffx)         [above = of magnet1]      {$\od{x}{t}$}
    node [block]   (diffy)         [below = of magnet3]      {$\od{y}{t}$};

    % arrows    
\path 
(inputx) edge[->] node {$x_\text{soll}$} (sumx1)
(sumx1) edge[->] (reglerx)
(reglerx) edge[->] (magnet1)
(magnet1)  edge[->] (sumx2)
(sumx2) edge[->] (pendulumx)
(pendulumx) edge[->] (valuex)
(valuex) edge[->] node {$x$} (outputx);

\path 
(inputy) edge[->] node {$y_\text{soll}$} (sumy1)
(sumy1) edge[->] (reglery)
(reglery) edge[->] (magnet3)
(magnet3)  edge[->] (sumy2)
(sumy2) edge[->] (pendulumy)
(pendulumy) edge[->] (valuey)
(valuey) edge[->] node {$y$} (outputy);

    \draw [->]      (reglerx.east)  --                  (magnet2.west);
    \draw [->]      (reglerx.east)  --                  (magnet3.west);
    \draw [->]      (reglery.east)  --                  (magnet1.west);     
    \draw [->]      (reglery.east)  --                  (magnet2.west);

    \draw [->]      (magnet1.east)  --                  (sumy2);
    \draw [->]      (magnet2.east)  --                  (sumx2);
    \draw [->]      (magnet2.east)  --                  (sumy2);
    \draw [->]      (magnet3.east)  --                  (sumx2);

    \draw           (valuex)        --                  (dotx);
    \draw           (valuey)        --                  (doty); 

    \draw [->]      (dotx)          --                  (diffx);
    \draw [->]      (doty)          --                  (diffy);

    \draw[->] (diffx)  -| node[near end] {$\dot{x}$} (reglerx);
    \draw[->] (diffy)  -| node[near end] {$\dot{y}$} (reglery);

    \draw[->, color=myColor1] (nonlinearx) |- ([yshift=2.5mm]magnet1.west);
    \draw[->, color=myColor1] (nonlinearx) |- ([yshift=2.5mm]magnet2.west);
    \draw[->, color=myColor1] (nonlinearx) |- ([yshift=2.5mm]magnet3.west);

    \draw[->, color=myColor1] (nonlineary) |- ([yshift=-2.5mm]magnet1.west);
    \draw[->, color=myColor1] (nonlineary) |- ([yshift=-2.5mm]magnet2.west);
    \draw[->, color=myColor1] (nonlineary) |- ([yshift=-2.5mm]magnet3.west);
    % feedback loops
    \path (dotx) edge[->,skip loop=10mm] (sumx1);
    \path (doty)    edge  [->,skip  loop=-10mm]         (sumy1);
    % dashed frame
    \draw [frame] ([yshift=10,xshift=-10]nonlinearx) rectangle ([yshift=-38.5,xshift=10]doty);
    % controller communication
    \draw[->, ultra thick,dotted] ([xshift=-2.5mm]reglerx.south) -- ([xshift=-2.5mm]reglery.north);
    \draw[->, ultra thick,dotted] ([xshift=2.5mm]reglery.north) -- ([xshift=2.5mm]reglerx.south);
\end{tikzpicture}
\end{figure}
\end{document}

在此处输入图片描述

相关内容