使用 Tikz 的流程图:帮助我改进

使用 Tikz 的流程图:帮助我改进

大家好!我对我为论文制作的流程图不满意。有人能帮我改进它吗?另外,我想将路径/线条的文本/标题改为垂直的。请帮忙。谢谢

\documentclass[12pt,letterpaper]{article}
\usepackage[margin=1in]{geometry}

\usepackage[latin1]{inputenc}
\usepackage{tikz,amsmath,amssymb,amsthm,epsfig}
\usetikzlibrary{shapes,arrows}
\begin{document}
\pagestyle{empty}


% Define block styles
\tikzstyle{decision} = [diamond, draw, fill=blue!23, 
    text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt]
\tikzstyle{block1} = [rectangle, draw, fill=blue!20, 
    text width=5em, text centered, rounded corners, minimum height=5em]
\tikzstyle{block2} = [rectangle, draw, fill=blue!10, 
    text width=8em, text centered, minimum height=10em]
\tikzstyle{block3} = [rectangle, draw, fill=purple!30, 
    text width=13em, text centered, minimum height=5em]
\tikzstyle{line} = [draw, thick, -latex']
\tikzstyle{cloud1} = [draw, ellipse,fill=red!18, node distance=4cm,
    minimum height=5em]
\tikzstyle{cloud2} = [draw, ellipse,fill=green!18, node distance=5cm,
    minimum height=5em, text width=7em]
\tikzstyle{block4}= [rectangle, draw, fill=orange!18,   
   text width=12em, text centered, minimum height=8em]

\begin{tikzpicture}[node distance = 3cm, auto]
    % Place nodes
    \node [cloud1] (input) {INPUT};
    \node [cloud2, below of=input, node distance=2.5cm] (mn) {$L(m,1),W(3,n)$ where $m,n \in \mathbb{N}$};
    \node [block1, left of=mn, node distance=6cm] (step1) {STEP 1:};
    \node [block2, left of=step1, node distance=5cm] (vertex) {$V(W(3,n)) = \{v\} \cup \{v_{(i,j)} | i \in \{1,2,3,\ldots,n\} , j \in \{1,2,3\} \}$};
    \node [block2, below of=vertex, node distance=5cm] (edge) {$E(W(3,n))= \{ vv_{(i,j)} | i \in \{1,2,\ldots,n\} , j \in \{1,2,3\}\} \cup \{v_{(i,j)}v_{(i,j+1)} | i \in \{1,2,\ldots,n\} , j \in \{1,2,\}\} \cup \{ v_{(i,1)}v_{(i,3)} | i \in \{1,2,\ldots,n\}\}$};
    \node [block1, right of=edge, node distance=5cm] (step2) {STEP 2:};
    \node [block3, below of=step2, node distance=3cm] (v) {Let $\ell : V(W(3,n)) \longrightarrow \{0,1,2,\ldots\}$ where $\ell(v_0)=0$};
    \node [decision, below of=v, left of=v, node distance=4cm] (geq) {$m\geq n$};
    \node [block3, right of=geq, node distance=7cm] (1) {$\ell(V_{(i,j)})=jm+(i-1), i \in \{1,2,\ldots n\}, j \in \{1,2,3\}$};
    \node [decision, below of=geq] (lessthan) {$m<n$};
    \node [block3, right of=lessthan, node distance=7cm] (2) {$\ell(v_{i,j})=jm+(j-1)(n-m)+(i-1), i \in \{1,2,3,\ldots,n\}$ and $j \in \{1,2,3\}$};
    \node [cloud2, below of=2, right of=2, node distance=4.5cm] (labeled) {Each vertex has label.};
    \node [cloud1, left of=labeled, node distance=5cm] (output) {OUTPUT};
    \node [block4, left of=output, node distance=5cm] (optimal) {Optimal $L(m,1)$-labeling for triangular windmill graph $(W(3,n))$.};


    % Draw edges
    \path [line] (input) --  (mn);
    \path [line] (mn) -- (step1);
    \path [line] (edge) -- (step2);
    \path [line] (step1) -- (vertex);
    \path [line] (vertex) -- (edge);
    \path [line] (step2) -- (v);
    \path [line] (v) -| node {and} (geq);
    \path [line] (geq) -- node {TRUE} (1);
    \path [line] (geq) -- node {FALSE}(lessthan);
    \path [line] (lessthan) -- node {TRUE}(2);
    \path [line] (1) -| node [near start] {TRUE} (labeled);
    \path [line] (2) -| node [near start] {TRUE} (labeled);
    \path [line] (labeled) -- node {TRUE} (output);
    \path [line] (output) -- (optimal);
    \draw [->,thick] (labeled.north) -- ++(0,0)  -- ++(10mm,5mm) |- node [ near start, yshift=1.5em]{FALSE} (step2);

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

有很多种方法可以做到这一点,这是一种可能性。

我对代码进行了一些“现代化”,使用\tikzset而不是 \tikzstyle应该\tikzset或被\tikzstyle用来定义 TikZ 样式吗?)并使用了positioning库(PGF/TikZ 中“right of=”和“right=of”之间的区别)。我还node distance从样式本身中删除了所有 s。

颜色完全是个人喜好,我从彩色酿酒师,如果您愿意的话,可以改回原来的。

我将输入节点移到了mn节点下方,这样可以节省大量垂直空间。不过,也可以将其放在上方,这样仍然适合页面。

您在图像中看到的框架表示文本块,它是由showframe我添加到geometry包中的选项生成的,您想在文档中删除该选项。

代码输出

\documentclass[12pt,letterpaper]{article}
% added showframe option to indicate size of text block
\usepackage[margin=1in,showframe]{geometry}
\usepackage[latin1]{inputenc}
\usepackage{tikz,amsmath,amssymb,amsthm}
% the arrows library is deprecated in favor of arrows.meta
% the positioning library is recommended for relative positioning
\usetikzlibrary{shapes,arrows.meta,positioning}

% colors are a matter of personal preference, these are from
% http://colorbrewer2.org/#type=qualitative&scheme=Set2&n=7
\definecolor{clr1}{RGB}{102,194,165}
\definecolor{clr2}{RGB}{252,141,98}
\definecolor{clr3}{RGB}{141,160,203}
\definecolor{clr4}{RGB}{231,138,195}
\definecolor{clr5}{RGB}{166,216,84}
\definecolor{clr6}{RGB}{255,217,47}
\definecolor{clr7}{RGB}{229,196,148}

% Define block styles
% \tikzstyle is considered deprecated in favor of the more 
% general \tikzset I believe
% I removed all the node distance settings from the various styles
\tikzset{
  decision/.style={diamond, draw, fill=clr1, 
    text width=4.5em, text badly centered, inner sep=0pt},
  block1/.style={rectangle, draw, fill=clr2, 
    text width=5em, text centered, rounded corners, minimum height=5em},
  block2/.style={rectangle, draw, fill=clr3, 
    text width=10em, text centered, minimum height=6em},
  block3/.style={rectangle, draw, fill=clr4, 
    text width=13em, text centered, minimum height=5em},
  line/.style={draw, thick, -Latex},
  cloud1/.style={draw, ellipse,fill=clr5, 
    minimum height=5em},
  cloud2/.style={draw, ellipse,fill=clr6, 
    minimum height=5em, text width=7em},
  block4/.style={rectangle, draw, fill=clr7,   
   text width=12em, text centered, minimum height=8em}
}

\begin{document}
\pagestyle{empty}
\begin{tikzpicture}[node distance = 7mm, auto]
    % Place nodes
    \node [cloud1]                         (input)    {INPUT};
    % in order to save space, this is placed above the input
    % but there is still room to have it the other way around, if that is preferred
    \node [cloud2, above=of input]         (mn)       {$L(m,1),W(3,n)$ where $m,n \in \mathbb{N}$};
    \node [block1, left=1cm of mn]         (step1)    {STEP 1:};
    \node [block2, left=2.3cm of step1]    (vertex)   {$V(W(3,n)) = \{v\} \cup \{v_{(i,j)} |
                                                       i \in \{1,2,3,\ldots,n\} , j \in \{1,2,3\} \}$};
    \node [block2, below=of vertex]        (edge)     {$E(W(3,n))= \{ vv_{(i,j)} | i \in \{1,2,\ldots,n\} ,
                                                       j \in \{1,2,3\}\} \cup \{v_{(i,j)}v_{(i,j+1)} | 
                                                       i \in \{1,2,\ldots,n\} , 
                                                       j \in \{1,2,\}\} \cup \{ v_{(i,1)}v_{(i,3)} |
                                                       i \in \{1,2,\ldots,n\}\}$};
    % note this is placed at (edge-|step1) for horizontal alignment,
    % and to avoid repeating node distances
    \node [block1, at={(edge-|step1)}]     (step2)    {STEP 2:};
    \node [block3, below=1.3cm of step2]   (v)        {Let $\ell : V(W(3,n)) \longrightarrow \{0,1,2,\ldots\}$ where $\ell(v_0)=0$};
    \node [decision,
           below left=1cm and 2.5cm of v]  (geq)      {$m\geq n$};
    \node [block3, at={(geq-| v)}]         (1)        {$\ell(V_{(i,j)})=jm+(i-1), i \in \{1,2,\ldots n\}, j \in \{1,2,3\}$};
    \node [decision, below=of geq]         (lessthan) {$m<n$};
    \node [block3, at={(lessthan -| 1)}]   (2)        {$\ell(v_{i,j})=jm+(j-1)(n-m)+(i-1), i \in \{1,2,3,\ldots,n\}$ and $j \in \{1,2,3\}$};
    \node [cloud2,
           right=-5mm of 2, yshift=-3.5cm] (labeled)  {Each vertex has label.};
    \node [cloud1, left=2cm of labeled]    (output)   {OUTPUT};
    \node [block4, left=of output]         (optimal)  {Optimal $L(m,1)$-labeling for triangular windmill graph $(W(3,n))$.};


    % Draw edges
    \path [line] (input) --  (mn);
    \path [line] (mn) -- (step1);
    \path [line] (edge) -- (step2);
    \path [line] (step1) -- (vertex);
    \path [line] (vertex) -- (edge);
    \path [line] (step2) -- (step2 |- v.north);
    % added [pos=0.25] for the node
    % in a -| path, midway/pos=0.5 is at the corner, so 0.25 is halfway to the corner
    \path [line] (v) -| node [pos=0.25] {and} (geq);
    \path [line] (geq) -- node {TRUE} (1);
    \path [line] (geq) -- node  {FALSE} (lessthan);
    \path [line] (lessthan) -- node {TRUE}(2);
    \path [line] (1) -| node [near start] {TRUE} (labeled);
    \path [line] (2) -| node [near start] {TRUE} (labeled);
    \path [line] (labeled) -- node {TRUE} (output);
    \path [line] (output) -- (optimal);
    % added sloped, above to the node options to rotate it
    \path [line] (labeled.north) -- ++(10mm,5mm) |- node [pos=0.3,sloped,above] {FALSE} (step2);

    % place node relative to the current bounding box
    \node [above=2ex, font=\bfseries\Large] at (current bounding box.north) {FLOWCHART};
\end{tikzpicture}
\end{document}

答案2

在我的论文中放了一些流程图后,我想说你做得很好,现在你处于摆弄阶段:手动调整以进行优化。

我要改变的一件事是线路Each vertex has label -- FALSE,因为它目前从入口点(北)出发。虽然东边看起来会更好,但它会让你的流程图稍微宽一点。

您可以将最后一个框向左移动一点,以TRUE减少前面的拥挤OUTPUT。如果您还将上面的两个粉色框也OUTPUT向左移动,它看起来会更平衡,并且/或者您可以Each vertex has label向左移动以重新获得因从东边移出边缘而失去的一些宽度FALSE

相关内容