使用节点创建 tikzpicture

使用节点创建 tikzpicture

有人能帮我用节点创建一个 tikzpicture 我想要的图片和我目前拥有的代码如下所示。有两件事让我很纠结:首先,如何让两个箭头从最后一个矩形中出来?其次,如何在圆圈和第一个矩形之间添加那个 switch 元素? 在此处输入图片描述

% Tikz block diagram for the product processor
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{decorations.shapes}
\usetikzlibrary{calc}



% Definition of styles for blocks and multiplier
\tikzstyle{block} =  [rectangle, draw=black, thick, text width=2em,align=center,
minimum height=2em, node distance=8em]
\tikzstyle{mult}=[draw=black, thick, circle, node distance = 2cm] % multiplier


%-----------------------------------------------------------------------
\begin{document}
\begin{tikzpicture}[font=\sffamily, >=latex]
  \node [align=center,color=black] at (-2,-1) (snlabel) {$c$};
  \node[circle, draw,node distance=5em, right of=snlabel, color=black] (circlelabel1){$\times$}; 
  \node[block, right of=circlelabel1, color=black, node distance=5em] (sumlabel)  {text1}; 
  \node[block, right of=sumlabel, color=black, node distance=7em] (detlabel)  {$d=e$\\$f=g$}; 
  
 
  \draw[->, color=black] (snlabel) -- node [above] {}(circlelabel1); 
  \draw[->, color=black] (circlelabel1) -- node [above] {} (sumlabel); 
  \draw[->, color=black] (sumlabel) -- node [above] {\color{black} $b$} (detlabel); 




  % add whitespace to bottom and top
  \node at (4.5,-3.5) {\hspace*{1em}};  
  \node at (4.5,-.4) {\hspace*{1em}}; 

\end{tikzpicture}

\end{document}

答案1

我强烈建议使用该库,而不是positioning使用。另外,请注意,不应再使用来定义样式。right=of Aright of=Atikzset\tikzstyle

在此处输入图片描述

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning, arrows.meta}

\tikzset{
    block/.style={draw, text width=4em, align=center, minimum height=2em},
    mult/.style={draw, circle, minimum size=6mm, path picture={\draw(path picture bounding box.south east)--(path picture bounding box.north west)
        (path picture bounding box.south west)--(path picture bounding box.north east);}}
}

\begin{document}

\begin{tikzpicture}[node distance=1.5cm, thick]
\coordinate(A) at (0,0);
\node[mult, right=of A](B){};
\coordinate[right=of B](C){};
\node[block, right=of C](D){text1};
\node[below=1cm of D](M){$d$};
\node[block, right=of D](E){$d=e$\\[2ex]$f=g$};
\node[right=1cm of E.25](F){$h_1$};
\node[right=1cm of E.-25](G){$h_2$};
\draw[->] (A)--node[above]{$c$}(B);
\draw (B)--(C);
\draw[->] (M)--(D);
\draw[->] (D)--node[above]{$b$}(E);
\draw[->] (E.25)--(F);
\draw[->] (E.-25)--(G);
\draw[<->] (D)--([xshift=8mm]C)--([shift={(.2,.5)}]C);
\draw[<-] ([xshift=3mm]C) arc(180:90:.5)node[above]{$t=0$};
\end{tikzpicture}

\end{document}

答案2

这是实现此目的的一种方法。

许多事情应该有所不同。我仅强调几点:

  • 为独立版添加边框,这样至少在开发过程中看起来会更好
  • 不需要在底部添加空白
  • 这里不需要大多数包
  • arrows.meta提供许多不错的箭头提示
  • block我认为风格不是一个好的选择
  • default color是黑色的,所以你不必指定它(如果是这样,那就不用按照你的方式)
  • 你可能想要使用inner sep功能块
  • arr如果您要添加线条颜色(如蓝色),样式将非常有用;只需在开始时这样做,如下所示:arr/.style={->,blue},
  • 我使用极坐标(表示与节点默认形状的交点)来放置传出信号线
  • 如果你anchor给出了这些节点,它们就会从正确的位置开始
  • 读最后一条\draw语句:移动到 sumlabel 的中心,新位置在 y 方向上为 -1.5 厘米,在那里放置一个文本,从那里向 sumlabel 画一条线

结果

% Tikz block diagram for the product processor
\documentclass[border=3mm]{standalone} % <<<
\usepackage{tikz}
%\usepackage{pgfplots}
%\usetikzlibrary{decorations.shapes}
%\usetikzlibrary{calc}

\usetikzlibrary{arrows.meta}



% Definition of styles for blocks and multiplier
%\tikzstyle{block} =  [rectangle, draw=black, thick, text width=2em,align=center,
%minimum height=2em, node distance=8em]
\tikzstyle{mult}=[draw=black, thick, circle, node distance = 2cm] % multiplier


%-----------------------------------------------------------------------
\begin{document}
\begin{tikzpicture}[
    font=\sffamily, 
    >={Latex},
    arr/.style={->},
  ]
  % ~~~ nodes ~~~~~~~~~~~~~~~~~~~~~~~~~
  \node[align=center,
        %color=black
                ]       at (-2,-1) (snlabel) {$c$};
  \node[circle, 
        draw,
        node distance=5em, 
        right of=snlabel, 
        color=black]       (circlelabel1){$\times$}; 
  \node[%block,
        draw, 
        right of=circlelabel1, 
        %color=black, 
        inner sep=2mm,
        node distance=5em]  (sumlabel)  {text1}; 
  \node[%block, 
        draw,
        right of=sumlabel, 
        %color=black, 
        node distance=7em,
        inner sep=5mm,
        align=center] (detlabel)  {$d = e$ \\\\ $f = g$}; 
  
  % ~~~ connectors ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  \draw[arr] (snlabel)      -- node [above] {}   (circlelabel1); 
  \draw[arr] (circlelabel1) -- node [above] {}    (sumlabel); 
  \draw[arr] (sumlabel)     -- node [above] {$b$} (detlabel); 
  
  \draw[arr] (detlabel.35)  -- +(1,0) node[anchor=west]{$h_1$};
  \draw[arr] (detlabel.-35) -- +(1,0) node[anchor=west]{$h_2$};
  
  \draw[arr] (sumlabel) ++(0,-1.5) node[anchor=north]{$d$} -- (sumlabel);


%  % add whitespace to bottom and top
%  \node at (4.5,-3.5) {\hspace*{1em}};  
%  \node at (4.5,-.4) {\hspace*{1em}}; 

\end{tikzpicture}

\end{document}

PS:一些清理工作

清理干净

% Tikz block diagram for the product processor
\documentclass[border=3mm]{standalone} % <<<
\usepackage{tikz}
\usetikzlibrary{arrows.meta}

%-----------------------------------------------------------------------
\begin{document}
 \begin{tikzpicture}[
    font=\sffamily, 
    >={Latex},
    arr/.style={->},
    blockS/.style={ draw,       
                    inner sep=2mm,
                    node distance=5em},
    blockB/.style={ draw,        
                    inner sep=5mm,
                    node distance=7em,
                    align=center},
    mult/.style={   circle, 
                    draw,
                    node distance=5em},
  ]
  % ~~~ nodes ~~~~~~~~~~~~~~~~~~~~~~~~~
  \node                      at (-2,-1) (snlabel)       {$c$};
  \node[mult,  right of=snlabel]        (circlelabel1)  {$\times$}; 
  \node[blockS,right of=circlelabel1]   (sumlabel)      {text1}; 
  \node[blockB,right of=sumlabel]       (detlabel)      {$d = e$ \\\\ $f = g$}; 
  
  % ~~~ connectors ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  \draw[arr] (snlabel)      -- node [above] {}   (circlelabel1); 
  \draw[arr] (circlelabel1) -- node [above] {}    (sumlabel); 
  \draw[arr] (sumlabel)     -- node [above] {$b$} (detlabel); 
  
  \draw[arr] (detlabel.25)  -- +(1,0) node[anchor=west]{$h_1$};
  \draw[arr] (detlabel.-25) -- +(1,0) node[anchor=west]{$h_2$};
  
  \draw[arr] (sumlabel) ++(0,-1.5) node[anchor=north]{$d$} -- (sumlabel);

 \end{tikzpicture}

\end{document}

相关内容