如何在 Tikz 中定义线条行为?

如何在 Tikz 中定义线条行为?

我有以下 LaTex 代码:

% !TEX TS-program = pdflatex
% !TEX encoding = UTF-8 Unicode
\documentclass[14pt, a4paper]{article} % use larger type; default would be 10pt
\usepackage[utf8]{inputenc}
\usepackage[russian]{babel}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}
\usetikzlibrary{shapes.multipart}

\oddsidemargin=-15.4mm
\textwidth=190mm
\headheight=-32.4mm
\textheight=277mm
\tolerance=100
\parindent=0pt
\parskip=8pt
\pagestyle{empty}


\tikzstyle{startstop} = [rectangle, rounded corners, minimum width=3cm, minimum height=1cm,text centered, draw=black, fill=blue!50]

\tikzstyle{io} = [trapezium, trapezium left angle=70, trapezium right angle=110, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=red!50]

\tikzstyle{process} = [rectangle, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=gray!50]

\tikzstyle{decision} = [diamond, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=green!50]

\tikzstyle{loop} = [rectangle split, rectangle split parts=2,draw, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=gray!50]

\tikzstyle{arrow} = [thick,->,>=stealth]



\begin{document}
    {\textbf{Задача 1.}}
    \\
    Схема алгоритма:
    \\
    \begin{center}
        \begin{tikzpicture}[node distance=2cm]
            \node (start) [startstop] {Начало};
            
            \node (input) [io, below of=start] {Ввод n};
            
            \node (ds1) [decision, below of=input, yshift=-0.4cm]{n = 0?};
            
            \node (y-case) [process, right of=ds1, xshift=2cm]{pow = 1};
            
            \node (while) [loop, below of=ds1, yshift=-0.4cm]{ 
                \nodepart{one} While n > 0 
                \nodepart{two} 
                    \begin{tabular}{cc}
                         n = n //10 \\
                         pow = pow +1 \\
                    \end{tabular}
                };
            
            \node (output) [io, below of=while, ]{Вывод pow};
            
            \node (stop) [startstop, below of=output] {Конец программы};
            
            \draw[arrow] (start) -- (input);
            \draw[arrow] (input) -- (ds1);
            \draw[arrow] (ds1) -- node[anchor=south]{Yes}(y-case);
            \draw[arrow] (y-case) |- (while);
            \draw[arrow] (ds1) -- node[anchor=east]{No}(while);
            \draw[arrow] (while) -- (output);
            \draw[arrow] (output) -- (stop);
        \end{tikzpicture}
    \end{center}

\end{document}

它的构建如下: Tikz 程序流程

我如何定义“NO”箭头的行为以使其从菱形块的左角开始(现在它从底角开始)?

答案1

像这样?

在此处输入图片描述

\documentclass[12pt, a4paper]{article} 
\usepackage[russian]{babel}
\usepackage{listings}

\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                chains,       % new
                positioning,  % new
                quotes,       % new
                shapes.geometric,
                shapes.multipart,
                babel
                }

\oddsidemargin=-15.4mm % for this settings is better to use >"geometry" package
\textwidth=190mm
\headheight=-32.4mm
\textheight=277mm
\tolerance=100
\parindent=0pt
\parskip=8pt
\pagestyle{empty}


\makeatletter
\tikzset{FlowChart/.style={  % <--- corrected, new
     base/.style = {draw,
                    minimum width=3cm, minimum height=1cm, align=center,
                    outer sep=0pt},
startstop/.style = {base, rounded corners, fill=blue!30},
  process/.style = {base, fill=orange!30},
 decision/.style = {base, diamond, aspect=1.3, fill=green!30},
       io/.style = {base, trapezium, trapezium stretches body,
                    trapezium left angle=70, trapezium right angle=110,
                    fill=red!30,
                    text width =\pgfkeysvalueof{/pgf/minimum width} - 2*\pgfkeysvalueof{/pgf/inner xsep}
                    },
     loop/.style = {base, rectangle split, rectangle split parts=2,
                    fill=gray!50},
    arrow/.style = {thick,-Triangle},
% suspend
suspend join/.code={\def\tikz@after@path{}}
                            }
        }% end of tikzset
 \makeatother

\begin{document}
    \textbf{Задача 1.}  \\
    Схема алгоритма:    \\
    \begin{center}
    \begin{tikzpicture}[FlowChart,
base/.append style = {on chain, join=by arrow},
    node distance = 5mm and 7mm,
      start chain = A going below
                        ]
% nodes in chain
\node (start)   [startstop] {Начало};
\node (input)   [io]        {Ввод n};
\node (ds1)     [decision]  {n = 0?};
\node (while)   [loop,
                 suspend join]  {\nodepart{one} While n > 0
                                 \nodepart{two} \begin{tabular}{cc}
                                                     n = n //10     \\
                                                     pow = pow +1   \\
                                                \end{tabular}
                                };
\node (output)  [io]        {Вывод pow};
\node (stop)    [startstop] {Конец программы};
% right branch
\node (y-case) [process,right=of ds1,
                suspend join]   {pow = 1};
%
\draw[arrow]    (ds1) to ["Yes"] (y-case);
\draw[arrow]    (y-case) |- (while);
\draw[arrow]    (ds1.west) to ["No" ']  ++ (-1,0) |- (while); % new
        \end{tikzpicture}
    \end{center}
\end{document}

笔记: 上面的 MWE 与您的相比做了以下更改:

  • 对于图片元素样式,使用tikset已弃用的tikzstyle
  • 节点的定位使用positioning库中定义的语法(观察right=of ds1而不是right of=ds1
  • 节点以链式组织,简化代码,使其更简短、更清晰
  • 垂直连接线由宏join中定义的宏绘制chains
  • join主分支中的最后一个节点与决策节点右侧节点之间的线被样式代码打断suspend join

相关内容