38

I want to place a node, that is kind of a note, behind another node. However, I need to put the reference node before the note, so I can place the note in a relative position to the reference node. So, the reference node is draw behind the note. But I want the reverse.

For example, in this figure the note is over the node. However, I want it below of the decision node.

  • So, how can I place the note behind the reference. Or is there a way to put notes to the nodes natively in TikZ?
  • The other thing is, why the text in the note is not centered?

diagram

\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}
4
  • possible duplicate of place a new TikZ node behind others
    – Jake
    Commented Jan 12, 2012 at 3:18
  • 1
    @Jake: I check that one, but it is related to beamer. Although the solution may be shifted from beamer to a standalone document for an expert, the use in a standalone doc is not straight forward. Hence, I asked this question.
    – adn
    Commented Jan 12, 2012 at 4:37
  • Yeah, that makes sense.
    – Jake
    Commented Jan 12, 2012 at 4:43
  • 1
    I won't say anything new here (thus only a comment), but I especially like zlevel/.style concise way of using \pgfonlayer shown by Martin. Example of its use can be found in my answer to the question about drawing electromagnetic tetrahedron.
    – przemoc
    Commented Jan 12, 2012 at 12:41

3 Answers 3

38

A variant of Azoun's solution is to use the backgrounds tikz library. The code is

\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}
3
  • Thanks, the background part works. Btw, do you know why the text is not centered in the note? Or is it a visual effect?
    – adn
    Commented Jan 12, 2012 at 4:50
  • 1
    @adn: see the change in the code of the background node. The text will be centered
    – Frédéric
    Commented Jan 12, 2012 at 6:15
  • @Frédéric text width=2 cm inside \tikzstyle{note} is preferable but if you use text width=6 em in the style or in the node, the text is not centered! (pgf 2.10 cvs). There is a problem with em unit. Commented Jan 12, 2012 at 6:51
14

Use the pgfonlayer environment. Here it is the solution:

\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}
4
  • Thanks, the background part works. Btw, do you know why the text is not centered in the note? Or is it a visual effect?
    – adn
    Commented Jan 12, 2012 at 4:51
  • @adn A workaround : removes font = \footnotesize and usess \footnotesize inside the node {\footnotesize using a note here} Commented Jan 12, 2012 at 5:59
  • @Altermundus: If I have to use the node several times, I have to change it in all the nodes, right? Is not another way?
    – adn
    Commented Jan 12, 2012 at 6:36
  • 1
    @adn You can use cm instead of 'em, for example text width = 2 cm` instead of text width = 5 em. It's the first time I see this problem. Commented Jan 12, 2012 at 7:09
8

This is just a comment to expand on Altermundus' comments as to why text width=5em does not work.

What happens is this. When you set text width=<something> on a node then it stores the <something> literally in a macro (\tikz@text@width). This macro is then used to set the width of the node. The way that TikZ deals with requests like text centered is to put the contents of the node in a minipage. To do that, it has to know the desired width of the text in the node. This is one place where \tikz@text@width is evaluated. The relevant code is:

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

Various things are important to note here:

  1. The font is changed first.
  2. The evaluation takes place inside a group.

The first means that in this case, 5em is interpreted as 5em in the \footnotesize font. I get this to be 42.50061pt (put a \showthe\pgf@x right after the \pgfmathsetlength line). The second means that if TikZ wants to know this width outside this group it either has to store the width somehow or it has to recompute it. TikZ does need to know this width later, and it chooses to recompute it. Relevant code:

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

Tracing the commands, this takes place not only outside the original box, but also outside the group in which the font declaration took place. So this computation doesn't know that the font was changed. For this computation, I get 50.00008pt.

I'd call this a bug.

The workaround is, as Altermundus explains, to use non-font-specific lengths. If you really want 5em (incidentally, should that be 5em in the normal font or in the footnote font?), then you can evaluate it before setting it. Something like:

\pgfmathsetmacro\fiveem{5em}

Then you can say text width=\fiveem and know that if you change font size later then everything will scale as you want.

2
  • 1
    Interesting analysis and enquiry, well done Sherlock ! Commented Jan 12, 2012 at 8:59
  • 1
    Things like this (unexpected behaviour) always get me curious, and I'm beginning to know my way around the TikZ/PGF code. It's all a learning process! Commented Jan 12, 2012 at 9:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .