2

I have a tikzpicture environment containing multiple nodes. For the MWE example below, I have two nodes - an image and another node, placed to the left relative to the image.

If I use a simple centering command, the entire tikzpicture is horizontally centered across the page. However, what I would like to do is to have the image node horizontally centered across the page. How could I achieve that?

\documentclass[12pt, a4paper]{article}

\usepackage[a4paper, total={17cm, 25cm}]{geometry}
\usepackage{tikz}

\begin{document}

This line shows the width of the text across the page as a visual guideline how the image is centered horizontally on the page.

\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\node[anchor = south west, inner sep=0pt] (image) at (0,0)
    {\includegraphics[width=.6\textwidth]{example-image-a}};
\begin{scope}[x = {(image.south east)},y = {(image.north west)}]
\node [anchor=east] at (-0.05,0.5) {annotation};
\end{scope}
\end{tikzpicture}
\end{figure}

\end{document}

The output is:

enter image description here

How should I change the code, so that the x coordinate of image.center is horizontally centered on the page? Thank you in advance.

1 Answer 1

3

I' do it with \useasboundingbox command. Something like this:

\documentclass[12pt, a4paper]{article}

\usepackage[a4paper, total={17cm, 25cm}]{geometry}
\usepackage{tikz}

\begin{document}

This line shows the width of the text across the page as a visual guideline how the image is centered horizontally on the page.

\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\node (image) at (0,0) {\includegraphics[width=.6\textwidth]{example-image-a}};
\useasboundingbox (image.south east) rectangle (image.north west); % <-- to center wrt this rectangle
\node at (image.west) [left] {annotation};
\end{tikzpicture}
\end{figure}

\end{document}

enter image description here

2
  • Thank you so much! It works perfectly. Commented Jun 25, 2021 at 10:41
  • @jopeto Please upvote and accept this answer to let the community know that you found what you needed. Commented Jun 26, 2021 at 10:30

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.