在页面上水平居中 tikz 节点

在页面上水平居中 tikz 节点

我有一个tikzpicture包含多个节点的环境。对于下面的 MWE 示例,我有两个节点 - 一个图像和另一个节点,相对于图像放置在左侧。

如果我使用一个简单的centering命令,整个页面tikzpicture就会水平居中。但是,我想要做的是让图像节点水平居中。我该如何实现呢?

\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}

输出为:

在此处输入图片描述

我应该如何更改代码,以便 x 坐标image.center在页面上水平居中?提前谢谢您。

答案1

我使用命令来完成\useasboundingbox。如下所示:

\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}

在此处输入图片描述

相关内容