Tikzpicture 到 Tikz 的转换

Tikzpicture 到 Tikz 的转换

我有以下绘制 tikzpicture 的代码。

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}

\usepgflibrary{shapes.arrows}

\tikzstyle{every node}=[align=center,text=black, ultra thick]


% All the Labels on the Edges
\draw (11,-0.65) node {$\frac{1}{4}.x$};
\draw (11,-1.35) node {(1)};

\draw (2.25,-0.65) node {4.x};
\draw (2.25,-1.35) node {(8)};


\draw (6.45,-7.75) node {1};
\draw (6.45,-8.35) node {(2)};


\draw (10.15,-5.5) node {$\frac{1}{4}.x$};
\draw (10.85,-5.5) node {(4)};

\draw (12.15,-4.75) node {$\frac{1}{x}$};
\draw (12.85,-4.75) node {(4)};

% All the Nodes
\draw (0,0) node[minimum height=3.5 cm,minimum width=2.4cm,draw,ultra thick, text width=2cm,font=\large] {A \\300.x};
\draw (4.5,0.5) node[minimum height=2cm,minimum width=1 cm,draw, ultra thick, text width=2cm,font=\large] {$Fetch_L$\\150};
\draw (4.5,-2.5) node[minimum height=2cm,minimum width=1 cm,draw,ultra thick, text width=2cm,font=\large] {$CINT_L$\\130};
\draw (8.5,-1.0) node[minimum height=2cm,minimum width=1 cm,draw,ultra thick, text width=2cm,font=\large] {$DINT_L$\\100};
\draw (0,-5.0) node[minimum height=2cm,minimum width=1 cm,draw,ultra thick, text width=2cm,font=\large] {$BINTS-Split$\\40};
\draw (8.5,-5.0) node[minimum height=2cm,minimum width=1 cm,draw,ultra thick, text width=2cm,font=\large] {$Md_C$\\200};
\draw (4.5,-8.0) node[minimum height=2cm,minimum width=1 cm,draw,ultra thick, text width=2cm,font=\large] {$IDg_C$\\260};
\draw (8.5,-8.0) node[minimum height=2cm,minimum width=1 cm,draw,ultra thick, text width=2cm,font=\large] {$USCA$\\40};
\draw (12.5,-6.5) node[minimum height=2cm,minimum width=1 cm,draw,ultra thick, text width=2cm,font=\large] {$C-Join$\\30};
\draw (12.5,-3.0) node[minimum height=2cm,minimum width=1 cm,draw,ultra thick, text width=2cm,font=\large] {$CMix$\\680.x};

% All the Arrows
\draw [->, ultra thick] (1.25,0.5)  -- (3.35,0.5);
\draw [->, ultra thick] (1.25,-0.95)  -- (4.35,-0.95) -- (4.35,-1.45);
\draw [->, ultra thick] (5.65,0.50)  -- (8.50,0.5) -- (8.5,0.05);
\draw [->, ultra thick] (5.65,-2.50)  -- (8.50,-2.5) -- (8.5,-2.05);
\draw [->, ultra thick] (9.65,-1)  -- (12.50,-1) -- (12.5,-2.0);
\draw [->, ultra thick] (0.0,-1.75)  -- (0.0,-4);
\draw [->, ultra thick] (1.15,-5)  -- (7.35,-5);
\draw [->, ultra thick] (5.65,-8)  -- (7.35,-8);
\draw [->, ultra thick] (0,-6)  -- (0,-8) -- (3.35,-8);
\draw [->, ultra thick] (8.65,-7)  -- (8.65,-6);
\draw [->, ultra thick] (9.65,-5)  -- (10.5,-5) -- (10.5,-6.5) -- (11.35, -6.5);
\draw [->, ultra thick] (12.5,-5.5)  -- (12.5,-4);

\end{tikzpicture}
\end{document}

输出如下所示:

输出

我的问题是,我不想对值进行硬编码,以便更轻松地更改某些条目(如果需要)。我尝试参考图库中给出的示例。但是,这些示例并没有给我提供像上面输出中给出的节点的准确位置。因此,我的问题是:是否可以不使用硬编码值,但仍能获得这样的输出?

答案1

样式

  • every node:你已经用过了
  • normal:对于大多数节点
  • big:针对一个大节点
  • -|-to path必须与 liketo[-|-]或一起使用的edge[-|-]

节点

我已经为后面几行引用的所有大节点命名。

也可以指定线上的节点(无论是---|还是)。我提供了几个示例来to说明edge如何工作。

没有改变了节点的位置,尽管如此应该以较少的硬编码方式完成。再次,我参考了positioning库。以下是几个示例:

\node (A) {A};
\node[left=of A] {B};
\node[right=of A.north,anchor=north, minimum height=3cm] (C) {C};
\node[below=5cm of C] (D) {far away};

代码

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\usetikzlibrary{shapes.arrows}
\tikzset{
    every node/.style={
        align=center,
        text=black,
        ultra thick,
    },
    normal/.style={
        every node,
        minimum height=2cm,
        minimum width=1cm,
        draw,
        text width=2cm,
        font=\large,
    },
    big/.style={
        normal,
        minimum height=3.5cm,
        minimum width=2.4cm,
    },
    -|-/.style={
        to path={
            let \p1=(\tikztostart),
                \p2=(\tikztotarget),
                \n1={(\x1+\x2)/2}
             in
            (\tikztostart) -- (\n1,\y1) -- (\n1,\y2) \tikztonodes -- (\tikztotarget)
        }
    },
}

\begin{document}
\begin{tikzpicture}
\node[big]    (A)      at (0,0)      {A \\300.x};
\node[normal] (Fetch)  at (4.5,0.5)  {$Fetch_L$\\150};
\node[normal] (CINT)   at (4.5,-2.5) {$CINT_L$\\130};
\node[normal] (DINT)   at (8.5,-1.0) {$DINT_L$\\100};
\node[normal] (BINTS)  at (0,-5.0)   {$BINTS-Split$\\40};
\node[normal] (FStar)  at (4.5,-5.0) {$FStar_C$\\300};
\node[normal] (Md)     at (8.5,-5.0) {$Md_C$\\200};
\node[normal] (IDg)    at (4.5,-8.0) {$IDg_C$\\260};
\node[normal] (USCA)   at (8.5,-8.0) {$USCA$\\40};
\node[normal] (C-Join) at (12.5,-6.5){$C-Join$\\30};
\node[normal] (CMix)   at (12.5,-3.0){$CMix$\\680.x};

\begin{scope}[every path/.style={ultra thick, ->}]
\draw (A.east |- Fetch.west) -- node[above]{4.x} node[below] {(8)} (Fetch.west);
\draw (A.320) -| (CINT);
\draw (Fetch) -| (DINT);
\draw (CINT) -| (DINT);
\draw[pos=.25] (DINT) -| node[above]{$\frac{1}{4}.x$} node[below] {(1)} (CMix);
\draw (A)  -- (BINTS);
\draw (BINTS) edge (FStar)
      (FStar) edge (Md); % two on one
\draw (BINTS) |- (IDg);
\draw (IDg)  edge (USCA)
      (USCA) edge (Md);
\draw[] (Md) to[-|-] (C-Join);
\draw[] (C-Join)  -- (CMix);
\end{scope}
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

相关内容