5

How can I get tikz node dimensions such as height and width? I am interested to use these parameters to precisely calculate placement of objects, e.g. using <a.height> in the following example:

\documentclass{article}
\thispagestyle{empty}
\usepackage{graphicx}
\usepackage{tikz}
\begin{document}
\begin{figure}
  \begin{tikzpicture}
    \node(a) {
      \includegraphics[width=0.8\textwidth]{example-image-a}
    };
    \node(b) at (0, <a.height>*3/4){label};
  \end{tikzpicture}
\end{figure}
\end{document}
Viesturs
  • 7,895

1 Answers1

7

Two examples where the desired result is obtained without knowing node dimensions. The second one uses calc tikzlibrary

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
  \begin{tikzpicture}
    \node(a) {
      \includegraphics[width=0.8\textwidth]{example-image-a}
    };
    \path (a.center)--(a.north) node[pos=.75, right] {label without calc};
    \node[left] at ($(a.center)!.75!(a.north)$) {label with calc};
  \end{tikzpicture}
\end{document}

enter image description here

Update:

Let's suppose we want the label label without calc 2cm to the right and 1cm up from its actual position in previous example: \path (a.center)--(a.north) node[pos=.75, right] {label without calc}. We can use positioning library to move the node relative to the corresponding point on path. In this case we can say above right=1cm and 2cm but this will use south west anchor for placement and we want west, then we add anchor=west after above right option:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc, positioning}
\begin{document}
  \begin{tikzpicture}
    \node[inner sep=0pt] (a) {
      \includegraphics[width=0.8\textwidth]{example-image-a}
    };
    \path (a.center)--(a.north) node[draw, pos=.75, above right=1cm and 2cm, anchor=west, red] (b) {label without calc};
    \node[left] (c) at ($(a.center)!.75!(a.north)$) {label with calc};

    %Some auxiliary elements.
    \draw[red,<->] (a.center)--(c.east) node[midway, right] {75\%};
    \draw[red,<->] (c.east)--(a.north) node[midway,right] {25\%};
    \fill[red] (c.east) circle (1pt);
    \draw[red,<->] (c.east)-|(b.west)  node[pos=0.25,below] {2 cm} node[pos=0.75,left] {1 cm};
  \end{tikzpicture}
\end{document}

enter image description here

Ignasi
  • 136,588
  • Can I still nudge e.g. + (2, 1) inside the expression \path (a.center)--(a.north) node[pos=.75, right] – Viesturs Jul 03 '17 at 10:23
  • @Viesturs Where in \path (a.center)--(a.north) node[pos=.75, right] do you want to use +(2,1)? In any case, you can use positioning library an say node[above right=2cm and 1cm of ...] – Ignasi Jul 03 '17 at 13:02
  • I want to nudge the result given by (a.center)--(a.north) node[pos=.75, by + (2, 1). I don't know the correct syntax for that. – Viesturs Jul 03 '17 at 14:00
  • @Viesturs Does the updated answer solve your question? – Ignasi Jul 03 '17 at 14:30
  • Yes, this is what I imagined. – Viesturs Jul 03 '17 at 14:40