如何在 tikzpicture 中旋转标签?

如何在 tikzpicture 中旋转标签?

我想沿着线垂直写上节点的标签。名称是模拟循环。我的代码是:

\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}

\tikzstyle{startstop}=[rectangle, rounded corners, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=red!30]
\tikzstyle{process}=[rectangle, rounded corners, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=blue!30]
\tikzstyle{decision}=[rectangle, rounded corners, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=green!30]
\tikzstyle{arrow}=[thick,->,>=stealth]

\begin{center}

\begin{tikzpicture}[node distance=1.7cm]
\centering
\node (start) [startstop] {Initilization of model};
\node (pro1) [process, below of=start] {Load input data to workspace};
\node (prop2)[process, below of=pro1] {Calculate time of next sample iteration};
\node (prop3) [process, below of=prop2] {Calculate outputs};
\node (prop4) [process, below of=prop3] {Update discrete states};
\node (prop5) [process, below of=prop4] {Calculate derivatives};
\node (prop6) [process, below of=prop5] {Calculate output state variables};
\node (dec1) [decision, below of=prop6] {Calculate derivatives};
\node (prop7) [process, below of=dec1] {Store the final iteration output in workspace};
\node (prop8) [process, below of=prop7] {using the stored output as\\
initial condition in main model simulation};
\node (stop) [startstop, below of=prop8] {End Simulation};
\draw[arrow] (start) -- (pro1);
\draw[arrow] (pro1) -- (prop2);
\draw[arrow] (prop2) -- (prop3);
\draw[arrow] (prop3) -- (prop4);
\draw[arrow] (prop4) -- (prop5);
\draw[arrow] (prop5) -- (prop6);
\draw[arrow] (prop6) -- (dec1);
\draw[arrow] (dec1) -- (prop7);
\draw[arrow] (dec1) -- +(4,0) |- node[near start] {\hspace{3cm}Iterative loop} (prop4);
\draw[arrow] (prop7) -- (prop8);
\draw[arrow] (prop8) -- +(-7,0) |- node[near start]{\hspace{-4cm} Simulation loop} (prop2);
\draw[arrow] (prop8) -- (stop);

\end{tikzpicture}

\end{center}

答案1

添加sloped,above到选项中,并从节点文本中node删除。对于“迭代循环”节点,也从中删除,然后将添加到节点选项中:\hspace\hspaceright

\draw[arrow] (dec1) -- +(4,0) |- node[near start,right] {Iterative loop} (prop4);
\draw[arrow] (prop8) -- +(-7,0) |- node[near start,sloped,above]{Simulation loop} (prop2);

其他一些评论:

  • 您似乎尝试在其中一个节点中添加换行符 ( \\)。默认情况下,这不起作用,但如果您在样式中替换text centered为,则可以使用在节点中进行换行 (align=centerprocess\\TikZ 节点中的手动/自动换行和文本对齐)。

  • 该语法below of=<othernode>被视为已弃用(PGF/TikZ 中“right of=”和“right=of”之间的区别)。相反,您应该加载positioning库并使用below=of <othernode>。执行此操作时,将node distance在以下两者之间计算边缘节点而不是中心点,因此node distance需要较小的。

    如果您希望计算节点中心之间的节点间距离,请将该on grid选项添加到节点样式中。

  • \tikzstyle{foo}=[..]被视为已弃用\tikzset{foo/.style={..}}。单个 中可以有多个样式定义\tikzset,如下例所示。

在此处输入图片描述

\documentclass[border=5mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows, positioning}

\tikzset{
  startstop/.style={rectangle, rounded corners, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=red!30},
  process/.style={rectangle, rounded corners, minimum width=3cm, minimum height=1cm, align=center, draw=black, fill=blue!30},
  decision/.style={rectangle, rounded corners, minimum width=3cm, minimum height=1cm, align=center, draw=black, fill=green!30},
  arrow/.style={thick,->,>=stealth}
}

\begin{document}

\begin{tikzpicture}[node distance=0.7cm]
\node (start) [startstop] {Initilization of model};
\node (pro1)  [process, below=of start] {Load input data to workspace};
\node (prop2) [process, below=of pro1] {Calculate time of next sample iteration};
\node (prop3) [process, below=of prop2] {Calculate outputs};
\node (prop4) [process, below=of prop3] {Update discrete states};
\node (prop5) [process, below=of prop4] {Calculate derivatives};
\node (prop6) [process, below=of prop5] {Calculate output state variables};
\node (dec1)  [decision, below=of prop6] {Calculate derivatives};
\node (prop7) [process, below=of dec1] {Store the final iteration output in workspace};
\node (prop8) [process,  below=of prop7] {using the stored output as\\
initial condition in main model simulation};
\node (stop) [startstop, below=of prop8] {End Simulation};
\draw[arrow] (start) -- (pro1);
\draw[arrow] (pro1) -- (prop2);
\draw[arrow] (prop2) -- (prop3);
\draw[arrow] (prop3) -- (prop4);
\draw[arrow] (prop4) -- (prop5);
\draw[arrow] (prop5) -- (prop6);
\draw[arrow] (prop6) -- (dec1);
\draw[arrow] (dec1) -- (prop7);
\draw[arrow] (dec1) -- +(4,0) |- node[near start,right] {Iterative loop} (prop4);
\draw[arrow] (prop7) -- (prop8);
\draw[arrow] (prop8) -- +(-7,0) |- node[near start,sloped,above]{Simulation loop} (prop2);
\draw[arrow] (prop8) -- (stop);

\end{tikzpicture}

\end{document}

答案2

在此处输入图片描述

\begin{tikzpicture}[node distance=1.7cm]
\centering
\node (start) [startstop] {Initilization of model};
\node (pro1) [process, below of=start] {Load input data to workspace};
\node (prop2)[process, below of=pro1] {Calculate time of next sample iteration};
\node (prop3) [process, below of=prop2] {Calculate outputs};
\node (prop4) [process, below of=prop3] {Update discrete states};
\node (prop5) [process, below of=prop4] {Calculate derivatives};
\node (prop6) [process, below of=prop5] {Calculate output state variables};
\node (dec1) [decision, below of=prop6] {Calculate derivatives};
\node (prop7) [process, below of=dec1] {Store the final iteration output in workspace};
\node (prop8) [process, below of=prop7] {using the stored output as};
\node (stop) [startstop, below of=prop8] {End Simulation};
\draw[arrow] (start) -- (pro1);
\draw[arrow] (pro1) -- (prop2);
\draw[arrow] (prop2) -- (prop3);
\draw[arrow] (prop3) -- (prop4);
\draw[arrow] (prop4) -- (prop5);
\draw[arrow] (prop5) -- (prop6);
\draw[arrow] (prop6) -- (dec1);
\draw[arrow] (dec1) -- (prop7);
\draw[arrow] (dec1) -- +(4,0) |- node[near start] {\hspace{3cm}Iterative loop} (prop4);
\draw[arrow] (prop7) -- (prop8);
\draw[arrow] (prop8) -- +(-7,0) |- node[
                                       rotate=90,%<-------------------add
                                       near start, 
                                       yshift=2mm
                                       ]
                                       {Simulation loop} (prop2);
\draw[arrow] (prop8) -- (stop);

\end{tikzpicture}

相关内容