大家好,我遇到了以图片左侧的 C 形线为中心的问题。正确的做法是什么?
\documentclass[11pt,a4paper,oneside]{article}
\usepackage{fullpage}
\usepackage[italian]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{shapes.multipart, calc}
\usetikzlibrary{shapes,arrows}
\tikzstyle{decision} = [diamond, draw, fill=red!20,
text width=5em, text badly centered, node distance=3cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, fill=blue!20,
text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, -latex']
\begin{document}
\begin{figure}[hb]
\centering
\begin{tikzpicture}[node distance = 2.5cm, auto]
\node[block, label={360:$O(1)$}] (init) {inizializza};
\node[decision, below of=init, label={360:$O(1)$}] (cond) {condizione};
\node[block, below of=cond, label={360:$O(f(n))$}] (corpo) {corpo del ciclo};
\node[block, below of=corpo,label={360:$O(1)$}] (reinit) {re-inizializza};
\path[line] (init) -- (cond);
\path[line] (cond) -- (corpo);
\path[line] (corpo) -- (reinit);
\path[line] (reinit.west) -- ++(-0.5,0) -- node[midway]{$g(n)$ volte} ++(0,1) |- (cond.west);
\end{tikzpicture}
\caption{Schematizzazione del costrutto \texttt{FOR}.}
\label{fig:ciclofor}
\end{figure}
\end{document}
答案1
您必须将 分成|-
两条线段。以下是其中一种方法:
\documentclass[11pt,a4paper,oneside]{article}
\usepackage{fullpage}
\usepackage[italian]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{shapes.multipart, calc}
\usetikzlibrary{shapes,arrows}
\tikzstyle{decision} = [diamond, draw, fill=red!20,
text width=5em, text badly centered, node distance=3cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, fill=blue!20,
text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, -latex']
\begin{document}
\begin{figure}[hb]
\centering
\begin{tikzpicture}[node distance = 2.5cm, auto]
\node[block, label={360:$O(1)$}] (init) {inizializza};
\node[decision, below of=init, label={360:$O(1)$}] (cond) {condizione};
\node[block, below of=cond, label={360:$O(f(n))$}] (corpo) {corpo del ciclo};
\node[block, below of=corpo,label={360:$O(1)$}] (reinit) {re-inizializza};
\path[line] (init) -- (cond);
\path[line] (cond) -- (corpo);
\path[line] (corpo) -- (reinit);
\path[line] (reinit.west) -- ++(-0.5,0) coordinate(A) -- node[midway]{$g(n)$ volte} (A|-cond) -- (cond.west);
\end{tikzpicture}
\caption{Schematizzazione del costrutto \texttt{FOR}.}
\label{fig:ciclofor}
\end{figure}
\end{document}
答案2
实际上,您根本不需要拆分|-
。根据 tikz 手册(第 17.8 节),当沿着这样的路径放置节点时,midway
位置 ( pos=.5
) 指的是弯曲处。也就是说,如果您想将标签放置在第一半(垂直)的中间,您需要pos=.25
。同样,pos=.75
是路径后半部分的中间。
\documentclass[11pt,a4paper,oneside]{article}
\usepackage{fullpage}
\usepackage[italian]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{shapes.multipart, calc}
\usetikzlibrary{shapes,arrows}
\tikzstyle{decision} = [diamond, draw, fill=red!20,
text width=5em, text badly centered, node distance=3cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, fill=blue!20,
text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, -latex']
\begin{document}
\begin{figure}[hb]
\centering
\begin{tikzpicture}[node distance = 2.5cm, auto]
\node[block, label={360:$O(1)$}] (init) {inizializza};
\node[decision, below of=init, label={360:$O(1)$}] (cond) {condizione};
\node[block, below of=cond, label={360:$O(f(n))$}] (corpo) {corpo del ciclo};
\node[block, below of=corpo,label={360:$O(1)$}] (reinit) {re-inizializza};
\path[line] (init) -- (cond);
\path[line] (cond) -- (corpo);
\path[line] (corpo) -- (reinit);
\path[line] (reinit.west) -- ++(-0.5,0) |- node[pos=.25]{$g(n)$ volte} (cond.west);
\end{tikzpicture}
\caption{Schematizzazione del costrutto \texttt{FOR}.}
\label{fig:ciclofor}
\end{figure}
\end{document}
答案3
我赞同 Emma 的回答 :-),但是,他的代码在这种特殊情况下的简单流程图可以通过使用包变得chains
更加简洁:
\documentclass[11pt,a4paper,oneside]{article}
\usepackage{fullpage}
\usepackage[italian]{babel}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows,
calc, chains,
shapes,
}
\begin{document}
\begin{figure}[hb]
\centering
\begin{tikzpicture}[
node distance = 1cm,
start chain = A going below,
base/.style = {draw,
text width=5em, align=flush center,
on chain=A, join=by line},
decision/.style = {base, diamond, fill=red!20,
aspect=1.2, inner sep=0pt},
block/.style = {base, rectangle, rounded corners,
fill=blue!20, minimum height=4em},
line/.style = {-latex'},
]
\node (init) [block, label=0:$O(1)$] {inizializza};
\node (cond) [decision, label=0:$O(1)$] {condizione};
\node (corpo) [block, label=0:$O(f(n))$] {corpo del ciclo};
\node (reinit)[block, label=0:$O(1)$] {re- inizializza};
%
\draw[line] (reinit.west) -- ++(-0.5,0) |- node[pos=0.25,left] {$g(n)$ volte} (cond) ;% borowed/stolen from Emma answer
\end{tikzpicture}
\caption{Schematizzazione del costrutto \texttt{FOR}.}
\label{fig:ciclofor}
\end{figure}
\end{document}
在上面的 MWE 中,我还tikzsyle
用本地节点集样式替换了已弃用的样式。其中针对通用功能定义了样式base
。结果类似:
答案4
因为你得不到答案,所以这是一个快速而又简单的方法。
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{shapes.multipart, calc}
\usetikzlibrary{shapes,arrows}
\tikzstyle{decision} = [diamond, draw, fill=red!20,
text width=5em, text badly centered, node distance=3cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, fill=blue!20,
text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, -latex']
\begin{document}
\begin{figure}[hb]
\centering
\begin{tikzpicture}[node distance = 2.5cm, auto]
\node[block, label={360:$O(1)$}] (init) {inizializza};
\node[decision, below of=init, label={360:$O(1)$}] (cond) {condizione};
\node[block, below of=cond, label={360:$O(f(n))$}] (corpo) {corpo del ciclo};
\node[block, below of=corpo,label={360:$O(1)$}] (reinit) {re-inizializza};
\node[coordinate] (lreinit) [left=15pt of reinit] {};
\node[coordinate] (lcond) [left=15pt of cond] {};
\path[line] (init) -- (cond);
\path[line] (cond) -- (corpo);
\path[line] (corpo) -- (reinit);
% \path[line] (reinit.west) -- ++(-0.5,0) -- node[midway]{$g(n)$ volte} ++(0,1) |- (cond.west);
\draw[->,red] (reinit) -- (lreinit) -- node[midway, left]{$g(n)$ volte} (lcond) -- (cond) ;
\end{tikzpicture}
\caption{Schematizzazione del costrutto \texttt{FOR}.}
\label{fig:ciclofor}
\end{figure}
\end{document}
给予: