7

Consider a TikZ picture with a circle centred at (0,0) in the TikZ coordinate system. Surrounding the node is a complex shape, such as a curve as shown below. In the standalone package, the border is computed to automatically hug the picture contents.

Is it possible to output upon compilation the final coordinates of (0,0) in the standalone coordinate system?

Minimal working example

The following snippet produces a simplified version of the figure below.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\coordinate (c1) at (0,0);
\coordinate (c2) at (-2, 0.2);
\node[circle,inner sep=2pt,draw=none,fill=red] at (c1) {};
\draw[color=blue,bend left=150] (c1) to (c2);
\end{tikzpicture}
\end{document}

enter image description here

2
  • 7
    The final coordinates will still be (0,0). But you can extract the coordinates of the bounding box and use this to calculate how far this coordinate is from each edge. To get the coordinates of the bounding box, you can do something like\path (current bounding box.south east); \pgfgetlastxy{\boundingboxeast}{\boundingboxsouth} \path (current bounding box.north west); \pgfgetlastxy{\boundingboxwest}{\boundingboxnorth}. Consider providing a compilable minimal working example (MWE) which will make it easier to help you in more detail. Commented Apr 10, 2023 at 17:31
  • @JasperHabicht thank you, I've added a compilable MWE. Would the snippet in your comment output the bounding box coordinates to the log file if inserted in the line above \end{tikzpicture}?
    – Sam
    Commented Apr 10, 2023 at 17:57

1 Answer 1

10

The coordinate of the center of the node (at (0,0)) will stay the same. But you can extract the coordinates of the bounding box (which is identical with the output format if you use the standalone package) and use these coordinates to calculate how far the center of the node is from each edge.

You can use the virtual node current bounding box and its anchors north east and south west in combination with \pgfgetlastxy to get the coordinates of the current bounding box (you probably only need south west though):

\path (current bounding box.north east); 
\pgfgetlastxy{\boundingboxeast}{\boundingboxnorth} 
\path (current bounding box.south west); 
\pgfgetlastxy{\boundingboxwest}{\boundingboxsouth}

The following code will print (current bounding box.south west) is at (-77.49pt,-13.25343pt). to the log:

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}

\coordinate (c1) at (0,0);
\coordinate (c2) at (-2, 0.2);
\node[circle,inner sep=2pt,draw=none,fill=red] at (c1) {};
\draw[color=blue,bend left=150] (c1) to (c2);

\path (current bounding box.south west); 
\pgfgetlastxy{\boundingboxwest}{\boundingboxsouth}
\message{(current bounding box.south west) is at (\boundingboxwest,\boundingboxsouth).}

\end{tikzpicture}
\end{document}

You could create a custom helper macro like this:

\documentclass{standalone}
\usepackage{tikz}

\NewDocumentCommand{\whereiscoordinate}{m}{
    \path (current bounding box.south west); 
    \pgfgetlastxy{\currentboundingboxwest}{\currentboundingboxsouth}
    \path #1; 
    \pgfgetlastxy{\currentcoordinatex}{\currentcoordinatey}
    \pgfmathsetmacro{\currentcoordinatedistancex}
        {-1*\currentboundingboxwest+\currentcoordinatex}
    \pgfmathsetmacro{\currentcoordinatedistancey}
        {-1*\currentboundingboxsouth+\currentcoordinatey}
    \message{Coordinate #1 
        is \currentcoordinatedistancex pt from the left and  
        \currentcoordinatedistancey pt from the bottom
        of the current bounding box. ^^J}
}

\begin{document}
\begin{tikzpicture}

\coordinate (c1) at (0,0);
\coordinate (c2) at (-2, 0.2);
\node[circle,inner sep=2pt,draw=none,fill=red] at (c1) {};
\draw[color=blue,bend left=150] (c1) to (c2);

\whereiscoordinate{(0,0)}
% prints to the log:
% Coordinate (0,0) is 77.49pt from the left and 13.25343pt from the bottom 
% of the current bounding box.

\whereiscoordinate{(-2,0.2)}
% prints to the log:
% Coordinate (-2,0.2) is 20.58452pt from the left and 18.9439pt from the bottom 
% of the current bounding box.

\end{tikzpicture}
\end{document}

You must log in to answer this question.

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