Tikzpicture 流程图:如何避免线条相互交叉

Tikzpicture 流程图:如何避免线条相互交叉

我正在尝试绘制图表;我试图避免线条交叉,但徒劳无功。下图说明了我得到的结果:

在此处输入图片描述

如果可能的话,我想避免线条和边缘互相交叉。

\documentclass[11pt]{book}
\usepackage[showframe,   % in real document remove this option!
            paperwidth=16cm, paperheight=24cm]{geometry}

\usepackage[T1]{fontenc}
\usepackage[french]{babel}

\usepackage{tikz}
\usetikzlibrary{arrows.meta, calc, chains, positioning, babel}

\begin{document}
    \begin{figure}[ht]
          \centering
            \begin{tikzpicture}[
            > = Straight Barb,
                node distance = 13mm and 15mm,
                start chain = going below,
                box/.style = {draw, minimum height=3ex, align=center,
                       on chain}
                          ]
            \begin{scope}[nodes={box,minimum width=4em,join=by ->}]
                \node (A) {Code source};
                \node (B) {Analyse lexicale};
                \node (C) {Analyse syntaxique};
                \node (D) {Analyse sémantique};
                \node (E) {Génération du code intermédiaire};
                \node (F) {Optimization de code};
                \node (G) {Génération du code machine};
            \end{scope}

                % second chain  
                \node (a) [box, right=of $(A)!0.5!(B)$] {Suites de chaînes \\ de caractères};
                \draw[->]   (A |- a) -- (a);
                \node (b) [box, right=of $(B)!0.5!(C)$] {Lexèmes};
                \draw[->]   (A |- b) -- (b);
                \node (c) [box, right=of $(C)!0.5!(D)$] {Arbre de dérivation};
                \draw[->]   (A |- c) -- (c);
                \node (d) [box, right=of $(D)!0.5!(E)$] {Arbre de dérivation};
                \draw[->]   (A |- d) -- (d);
                \node (e) [box, right=of $(E)!0.5!(F)$] {Code intermédiaire};
                \draw[->]   (A |- e) -- (e);
                \node (f) [box, right=of $(F)!0.5!(G)$] {Code binaire};
                \draw[->]   (A |- f) -- (f);

                % outer comments
                \node (auxL)    [box, left=of $(A)!0.5!(G)$]   
                                        {Gestionnaire de la \\ table des symboles}; %%%%%%%%%%%%
                \node (auxR)    [box,right=of $(a.west)!0.5!(f.west)$]   
                                        {Gestionnaire \\ des erreurs}; %%%%%%%%%%%%
                \foreach \i in {A,B,...,G}
                \draw[semithick, cyan]  (auxL) -- (\i.west) 
                                    (\i.east) -- + (2,0) -- (auxR);
       \end{tikzpicture}
       \caption{Processus de compilation d'un programme informatique} \label{fig:processCompilation}
    \end{figure}
\end{document}

答案1

这里有一个使用条件和声明一些常量的选项,以便在初始配置之外进行对齐

结果:

在此处输入图片描述

梅威瑟:

\documentclass[11pt]{book}
\usepackage[showframe,   % in real document remove this option!
paperwidth=16cm, paperheight=24cm]{geometry}

\usepackage[T1]{fontenc}
\usepackage[french]{babel}

\usepackage{tikz}
\usetikzlibrary{arrows.meta, calc, chains, positioning, babel}

\begin{document}
    \begin{figure}[ht]
        \centering
        \begin{tikzpicture}[
            > = Straight Barb,
            node distance = 13mm and 15mm,
            start chain = going below,
            scale=0.9,%--added
            transform shape,%--added
            box/.style = {
                draw,
                minimum height=3ex,
                align=center,
                on chain
            }
        ]
        \begin{scope}[nodes={box,minimum width=4em,join=by ->}]
            \node (A) {Code source};
            \node (B) {Analyse lexicale};
            \node (C) {Analyse syntaxique};
            \node (D) {Analyse sémantique};
            \node (E) {Génération du code intermédiaire};
            \node (F) {Optimization de code};
            \node (G) {Génération du code machine};
        \end{scope}
        \def\Rdispl{2em}%added, to control some displacement    
        % second chain  
        \node (a) [box, right=\Rdispl of $(A)!0.5!(B)$] {Suites de chaînes \\ de caractères};
        \draw[->]   (A |- a) -- (a);
        \node (b) [box, right=\Rdispl of $(B)!0.5!(C)$] {Lexèmes};
        \draw[->]   (A |- b) -- (b);
        \node (c) [box, right=\Rdispl of $(C)!0.5!(D)$] {Arbre de dérivation};
        \draw[->]   (A |- c) -- (c);
        \node (d) [box, right=\Rdispl of $(D)!0.5!(E)$] {Arbre de dérivation};
        \draw[->]   (A |- d) -- (d);
        \node (e) [box, right=\Rdispl of $(E)!0.5!(F)$] {Code intermédiaire};
        \draw[->]   (A |- e) -- (e);
        \node (f) [box, right=\Rdispl of $(F)!0.5!(G)$] {Code binaire};
        \draw[->]   (A |- f) -- (f);
            
        % outer comments
        \node (auxL)[box,left=\Rdispl of D]{Gestionnaire de la \\ table des symboles}; 
        \node (auxR)[box,right=2.5*\Rdispl of D]{Gestionnaire \\ des erreurs};
        \foreach \i [count=\j from -3]in {A,B,...,G}{
            \ifnum \j=0 {\draw[semithick, cyan]  (auxL)--(\i) (auxR)--(\i);} \fi
            \ifnum \j>0 {\draw[semithick, cyan]  ($(auxL.-90)+(-0.5em*\j+1em,0)$) |-(\i) ($(auxR.-90)+(0.5em*\j-1em,0)$) |-(\i);} \fi
            \ifnum \j<0 {\draw[semithick, cyan]  ($(auxR.90)+(-0.5em*\j-1em,0)$) |-(\i) ($(auxL.90)+(0.5em*\j+1em,0)$)  |-(\i);} \fi
        }
            
        \end{tikzpicture}
        \caption{Processus de compilation d'un programme informatique} \label{fig:processCompilation}
    \end{figure}
\end{document}

相关内容