在 TikZ 图中将一个节点放在另一个节点后面

在 TikZ 图中将一个节点放在另一个节点后面

我想将一个节点(一种注释)放在另一个节点后面。但是,我需要将参考节点放在注释前面,这样我才能将注释放置在相对于参考节点的位置。因此,参考节点绘制在注释后面。但我想要相反的结果。

例如,此图中注释位于节点上方。但是,我希望它位于决策节点下方。

  • 那么,我该如何将注释放在参考后面。或者有没有办法在 TikZ 中将注释放在节点上?
  • 另外还有,为什么笔记中的文字没有居中?

图表

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes,arrows,shadows,positioning}

\begin{document}
\tikzstyle{decision} = [diamond, draw, fill=blue!5, text width=6em, text badly centered, node distance=2.5cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, fill=blue!5,
    text width=7em, text centered, rounded corners, minimum height=4em]
\tikzstyle{note} = [rectangle, dashed, draw, fill=white, font=\footnotesize,
    text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, very thick, color=black!50, -latex']
\begin{tikzpicture}[scale=2, node distance = 2cm, auto]
  \node [block] (blk) {A block here};
  \node [decision, below of=blk] (if) {Is something?};
  \node [note,below right=-5mm of if, anchor=north west] (note1) {using a note here};
  \path [line] (blk) -- (if);
\end{tikzpicture}
\end{document}

答案1

Azoun 的解决方案的一个变体是使用backgroundstikz 库。代码如下

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes,arrows,shadows,positioning}
\usetikzlibrary{backgrounds}

\begin{document}

\tikzstyle{decision} = [diamond, draw, fill=blue!5, text width=6em, text badly centered, node distance=2.5cm, inner sep=0pt]

\tikzstyle{block} = [rectangle, draw, fill=blue!5,
    text width=7em, text centered, rounded corners, minimum height=4em]

\tikzstyle{note} = [rectangle, dashed, draw, fill=white, font=\footnotesize,
    text width=5em, text centered, rounded corners, minimum height=4em]

\tikzstyle{line} = [draw, very thick, color=black!50, -latex']

\begin{tikzpicture}[scale=2, node distance = 2cm, auto]
  \node [block] (blk) {A block here};
  \node [decision, below of=blk] (if) {Is something?};

  \begin{scope}[on background layer]
    \node [note,below right=-5mm of if, anchor=north west, text width=2cm] (note1) {using a note here};
  %you may also use align = center instead of text width = ...
  \end{scope}

  \path [line] (blk) -- (if);
\end{tikzpicture}
\end{document}

答案2

使用 pgfonlayer 环境。这是解决方案:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes,arrows,shadows,positioning}

\begin{document}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\begin{tikzpicture}[auto,
                    scale           = 2,
                    node distance   = 2cm,
                    decision/.style = {draw,
                                       diamond,
                                       text badly centered,
                                       fill          = blue!5,
                                       text width    = 6em,
                                       node distance = 2.5cm,
                                       inner sep     = 0pt},
                    block/.style    = {draw,
                                       rectangle,
                                       text centered,
                                       rounded corners,
                                       fill           = blue!5,
                                       text width     = 7em,
                                       minimum height = 4em},
                    note/.style     = {draw,
                                       rectangle,
                                       dashed,
                                       text centered,
                                       rounded corners,
                                       fill           = white,
                                       font           = \footnotesize,
                                       text width     = 5em,
                                       minimum height = 4em},
                    line/.style     = {draw,
                                       very thick, 
                                       -latex',
                                       color = black!50}]

\node[block] (blk) {A block here};
\node[decision,
      below of = blk] (if) {Is something?};

\begin{pgfonlayer}{background}
\node[note,
      below right = -5mm of if,
      anchor      = north west] (note1) {using a note here};
\end{pgfonlayer}

\path[line] (blk) -- (if);
\end{tikzpicture}
\end{document}

答案3

这只是为了扩展 Altermundus 的评论以解释为什么text width=5em不起作用。

发生了什么事情。当你设置text width=<something>一个节点时,它会存储<something> 字面上地在宏 ( \tikz@text@width) 中。然后使用此宏设置节点的宽度。TikZ 处理类似请求的方式text centered是将节点的内容放在 中minipage。为此,它必须知道节点中文本的所需宽度。这是\tikz@text@width评估 的一个地方。相关代码是:

      \tikz@textfont%  
      \ifx\tikz@text@width\pgfutil@empty%
      \else%
        \begingroup%
            \pgfmathsetlength{\pgf@x}{\tikz@text@width}%
          \pgfutil@minipage[t]{\pgf@x}\leavevmode\hbox{}%

这里有几点需要注意:

  1. 字体已改变第一的
  2. 评估在小组内部进行。

第一个意思是在这种情况下,5em被解释为5em字体\footnotesize。我理解为42.50061pt\showthe\pgf@x在行后放置一个\pgfmathsetlength)。第二个意思是如果 TikZ 想知道这个宽度外部这个组要么必须以某种方式存储宽度,要么必须重新计算它。TikZ 稍后确实需要知道这个宽度,因此它选择重新计算它。相关代码:

\ifx\tikz@text@width\pgfutil@empty%
\else%
  \pgfmathsetlength{\pgf@x}{\tikz@text@width}%
  \wd\pgfnodeparttextbox=\pgf@x%

跟踪命令,这不仅发生在原始框之外,还发生在字体声明所在的组之外。因此,此计算不知道字体已更改。对于此计算,我得到了50.00008pt

我认为这是个错误。

解决方法是,正如 Altermundus 所解释的那样,使用非字体特定的长度。如果你真的想要5em(顺便说一句,应该是5em普通字体还是脚注字体?),那么你可以在设置之前对其进行评估。例如:

\pgfmathsetmacro\fiveem{5em}

然后您可以说text width=\fiveem并且知道如果您稍后更改字体大小,那么一切都会按照您想要的缩放比例。

相关内容