1

I'm trying to get a figure made with tikz to be centered exactly on the figure's own internal x-axis. I don't want to use \centering, as at least with its default behaviour, \centering finds its own center line based on the actual width of the tikzpicture as a whole. What I'm looking for is a way to center it on a chosen point, completely disregarding the actual width of the figure.

MWE of \centering's behaviour:

\documentclass{article}

\usepackage{tikz}

\begin{document}

    \begin{figure}\centering
        \begin{tikzpicture}
            \draw (-3,0) -- (2,0);
            \path (0,2) node [shape=circle,draw]  {};
        \end{tikzpicture}
        \caption{This figure should be centered on x=0, marked by the circle. It isn't.}
    \end{figure}

\end{document}

enter image description here

In short, I'm looking for a way to take this exact figure, off-center line included, and place it inside the figure float in such a way that the circle at x=0 is what's centered above the caption (with the line off-center). Is such a thing possible? What does it involve?

  • If you add \path (3,0); to your picture, it is.... These are relative coordinates and TikZ computes the bounding box on the basis of the things you draw in the picture. The coordinates have no absolute meaning (unless you work with absolute page coordinates). – user121799 Apr 10 at 18:58
  • @marmot Oh, so the workaround is just to make an undrawn \path that alters the bounding box size? Makes sense, and seems to solve my problem just fine! – Sjiveru Apr 10 at 19:03
  • 1
    I wouldn't call it a workaround, but yes. Of course you could ask TikZ to make the bounding box symmetric around (0,0). – user121799 Apr 10 at 19:04
1

The coordinates of a (non-overlay) tikzpicture have no absolute meaning because TikZ computes the bounding box on the basis of the things you draw in the picture. Of course, you can readjust the bounding box in such a way that the origin becomes the horizontal center of the bounding box. Either by hand, e.g. by adding \path (3,0); in this case, or write a style for it, e.g.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{make origin horizontal center of bounding box/.style={%
execute at end picture={%
\path let \p1=(current bounding box.west),\p2=(current bounding box.east)
in ({-max(-1*\x1,\x2)},\y1) ({max(-1*\x1,\x2)},\y1);
}}}
\begin{document}

    \begin{figure}\centering
        \begin{tikzpicture}
            \draw (-3,0) -- (2,0);
            \path (0,2) node [shape=circle,draw]  {};
        \end{tikzpicture}
        \caption{This figure should be centered on x=0, marked by the circle. It
        isn't because the bounding box of a \textbackslash\texttt{tikzpicture}
        is computed on the basis of its contents.}
    \end{figure}

    \begin{figure}\centering
        \begin{tikzpicture}[make origin horizontal center of bounding box]
            \draw (-3,0) -- (2,0);
            \path (0,2) node [shape=circle,draw]  {};
        \end{tikzpicture}
        \caption{This figure should be centered on x=0, marked by the circle. It
        is now with the help of \texttt{make origin horizontal center of bounding box}.}
    \end{figure}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.