5

With

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}

\draw (2,2) circle (3cm);

\node at (current bounding box.north)[anchor=north]{north};
\node at (current bounding box.north west)[anchor=north west]{north west};
\node at (current bounding box.north east)[anchor=north east]{north east};

\end{tikzpicture}
\end{document}

one can position a node relative to the bounding box of the picture, using a set of given positions (north, north west, etc.).

enter image description here

I would like to position the text more freely within the current bounding box, e.g., at position (0.45, 0.7), where (0, 0) is the lower left and (1, 1) the top right corner of the current bounding box.

How to achieve this?

1
  • \path (current bouding box.south west) -- (current bounding box.north east) node[pos=0.7] {xxx};
    – Ignasi
    Commented Oct 25, 2015 at 19:22

1 Answer 1

4

You can calculate the coordinate using the calc library. Note that if you place nodes near the edge that sticks outside, the current bounding box node will change. If you don't want the bounding box to be changed you can add a \useboundingbox as in the below example.

enter image description here

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand\relcoord[2]{({$(current bounding box.south west)!#1!(current bounding box.south east)$} |- {$(current bounding box.south west)!#2!(current bounding box.north west)$})}
\begin{document}
\begin{tikzpicture}

\draw (2,2) circle[radius=2.5cm];

\node at (current bounding box.north)[anchor=north]{north};
\node at (current bounding box.north west)[anchor=north west]{north west};
\node at (current bounding box.north east)[anchor=north east]{north east};

\useasboundingbox \relcoord{0}{0} rectangle \relcoord{1}{1};

\draw [use as bounding box,help lines,step=0.5cm] \relcoord{0}{0} grid \relcoord{1}{1};

\fill [red] \relcoord{0.6}{0.7} circle[radius=3pt];
\fill [blue] \relcoord{0.3}{0.4} circle[radius=3pt];

\foreach \x in {0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1} {
 \node at \relcoord{\x}{0.7}[anchor=center] {x};
 \node at \relcoord{0.7}{\x}[anchor=center] {x};
}
\end{tikzpicture}
\end{document}
2
  • I see slight inaccuracies with \foreach \x in {0,...,9} { \node at \relcoord{0.\x}{0.7}[anchor=center] {x}; } \node at \relcoord{1.0}{0.7}[anchor=center] {x}; \foreach \x in {0,...,9} { \node at \relcoord{0.7}{0.\x}[anchor=center] {x}; } \node at \relcoord{0.7}{1.0}[anchor=center] {x}; , in particular for \x=0. Also, the two loops don't intersect exactly. Commented Oct 25, 2015 at 20:46
  • 1
    @NicoSchlömer When you place nodes that has content outside the bounding box the current bounding box will change. In my example, if you add use as bounding box as an option to the \draw that makes the grid, and add the loops after it, it looks better. Commented Oct 25, 2015 at 20:51

You must log in to answer this question.

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