在 latex 中绘制图表

在 latex 中绘制图表

我的问题是,当我用短语替换字母时,编译时会出现错误

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\usepackage[a4paper,margin=1.5cm]{geometry}
\usepackage{amsmath,amsfonts}
\begin{document}

%\newcommand{\boite}[3]{\draw (#1,#2) rectangle (#1+12,#2+3);
%\node at (#1+6,#2+1.5) {#3};}

\newcommand{\rect}[3]{\node[draw,minimum height=1cm,minimum width=4cm] (#3) at (#1,#2) {$#3$};}
\newcommand{\prect}[3]{\node[draw,minimum height=1cm,minimum width=1.8cm] (#3) at (#1,#2) {$#3$};}
\newcommand{\diam}[3]{\node[draw,diamond,minimum height=1.5cm,minimum width=4cm] (#3) at (#1,#2) {$#3$};}


\begin{tikzpicture}[x=-.375cm,y=-.375cm]
  \foreach \x/\y/\k in {0/0/A, 0/5/B, 0/10/C, 0/15/D, 0/20/E, 0/30/Q, 16/20/F, 16/25/G, 16/30/M, 16/35/N, 16/40/O, 32/25/H, 32/30/I, 32/40/L } {
    \rect{\x}{\y}{\k}
  }
  \foreach \x/\y/\k in {0/25/P, 0/35/R} {
    \diam{\x}{\y}{\k}
  }
  \foreach \x/\y/\k in {35.1/35/J, 28.9/35/K} {
    \prect{\x}{\y}{\k}
  }
  \node[draw,minimum height=1cm,minimum width=4cm] (End) at (0,45) {End};
  \foreach \m/\n in {A/B, B/C, C/D, D/E, E/P, E/F, P/Q, Q/R, F/G, G/H, H/I, J/L, K/L, M/N} {
    \draw[thick,>=latex,->] (\m)--(\n);
  }
  \foreach \m/\n in {I/J, R/End, N/O} {
    \draw[thick,>=latex,->] (\m) -- (\n) node[midway,above,sloped]{Yes};
  }
  \foreach \m/\n in {I/K} {
    \draw[thick,>=latex,->] (\m) -- (\n) node[midway,above,sloped]{No};
  }
  \draw[red,thick,>=latex,->] (R.west) --++ (2,0) node[midway,above]{No} --++ (0,-30) -- (B);
  \draw[thick,>=latex,->] (N.east) --++ (-2,0) node[midway,above]{No} --++ (0,-10) -- (G.east);
  \draw[thick,>=latex,->] (O.south) -- (16,43) -- (8,43) -- (8,25) --(8,25)--(P.west);
\end{tikzpicture}

\end{document}

答案1

我认为您绘制流程图的方法无法产生清晰的流程图结构。使用chains包及其宏join,定义节点样式而不是定义新命令(在我看来)可以得到更好的结果,并且不会出现节点名称及其内容的问题。

作为起点可以提供以下内容(不完整的 MWE,我无法完成它,因为我不知道现在的流程图是什么):

\documentclass{article}
\usepackage[a4paper,margin=1.5cm]{geometry}
\usepackage{amsmath,amsfonts}

\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                chains,
                quotes,
                shapes.geometric}

\begin{document}
\begin{tikzpicture}[%x=-.375cm,y=-.375cm,
                auto,
node distance = 10mm and 20mm,
  start chain = A going below,
   arr/.style = {-{Stealth[scale=2]}},
     D/.style = {draw,diamond,aspect=2, minimum width=4cm},
     N/.style = {draw,minimum height=1cm,minimum width=4cm}
                    ]
\foreach \i/\j/\k in {N/A/text A, N/B/text B, N/C/text C, N/D/text D, N/E/text E,
                      D/P/text P, N/Q/text Q, D/R/text R}
\node (\j)  [\i, on chain, join=by arr]  {\k};

\node (end)[N, below=22mm of R] {End};
%
\draw[arr]  (R.west) to["No" ']  ++ (-0.7,0) |- (B);
\draw[arr]  (R)      to["Yes" ]  (end);
%% middle branch
\node (F)   [N,  left=of E] {text F};
\node (G)   [N, below=of F] {text G};
\node (M)   [N, below=of G] {text M};
\chainin (M);
%
\draw[arr]  (E) -- (F);
\draw[arr]  (F) -- (G);
\foreach \i/\j/\k in {D/N/text N, N/O/text O}
\node (\j)   [\i, on chain, join=by arr] {\k};
%
\draw[arr]  (N.east) to["No" ']  ++ (0.7,0) |- (G);
\path   (N) to ["Yes"] (O);
%% left branch )(left to you)
\node   (H) [N,  left=of G] {text H};
\draw[arr]  (G) -- (H);
% etc
\end{tikzpicture}
\end{document}

正如您所看到的,与重现以下图像的代码部分相比,建议的流程图代码也短得多:

在此处输入图片描述

相关内容