我的论文中有一张图,其中我包含一张图片,图片上有一堆标签,用于标识图片的不同方面,有些标签很长,有些标签很短。问题是其中一些标签很长,导致整个图像偏移,如下所示。 我想让图像本身(不包括我添加的标签)居中对齐。我该怎么做?
这是一个 MWE,与我在文档中所做的基本相同。
\documentclass[letterpaper]{article}
\usepackage{subcaption}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{tikz}
\usepackage{graphicx}
\begin{document}
\begin{figure}
\centering
\begin{subfigure}[b]{0.9\textwidth}
\begin{tikzpicture}
\node[anchor=south west,inner sep=0](image) at (0,0) {\includegraphics[width=0.35\textwidth]{example-image-a}};
\begin{scope}[x={(image.south east)},y={(image.north west)}]
\node[anchor=east,align=right,draw=none] at (-0.10,0.80) {A loooooooooong label};
\node[anchor=west,align=left ,draw=none] at ( 1.10,0.05) {Short Label};
\draw[black,->,thick] (-0.10,0.80) -- (0.40,0.70); %Outlet expansion line
\draw[black,->,thick] ( 1.10,0.05) -- (0.30,0.10); %Seeding array line
\end{scope}
\end{tikzpicture}
\caption{\label{subfig1}}
\end{subfigure}
\label{fig1}
\caption{An off-center figure}
\end{figure}
\end{document}
答案1
只需在相关的地方使用\rlap
和\llap
(\centering
当然也要使图像本身居中):
\documentclass[letterpaper]{article}
\usepackage{subcaption}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{tikz}
\usepackage{graphicx}
\begin{document}
\begin{figure}
\centering
\begin{subfigure}[b]{0.9\textwidth}
\centering
\begin{tikzpicture}
\node[anchor=south west,inner sep=0](image) at (0,0) {\includegraphics[width=0.35\textwidth]{example-image-a}};
\begin{scope}[x={(image.south east)},y={(image.north west)}]
\node[anchor=east,align=right,draw=none] at (-0.10,0.80) {\llap{A loooooooooong label}};
\node[anchor=west,align=left ,draw=none] at ( 1.10,0.05) {\rlap{Short Label}};
\draw[black,->,thick] (-0.10,0.80) -- (0.40,0.70); %Outlet expansion line
\draw[black,->,thick] ( 1.10,0.05) -- (0.30,0.10); %Seeding array line
\end{scope}
\end{tikzpicture}
\caption{\label{subfig1}}
\end{subfigure}
\label{fig1}
\caption{An off-center figure}
\end{figure}
\end{document}
答案2
\centering
在 的开头添加subfigure
,然后\useasboundingbox (image.south east) rectangle (image.north west);
在tikzpicture
之前添加scope
。这样 的边界框tikzpicture
仅由图像的大小定义。
顺便说一下,pgfplots
加载tikz
,并tikz
加载graphicx
。因此,您实际上不必在 之后加载后两个\usepackage{pgfplots}
。此外,建议使用 的特定版本compat
,而不是compat=newest
。这样做的原因是,如果pgfplots
更新,即使代码没有改变,您的图的外观也可能会改变。
我刚刚意识到一件事:如果标签位于图像上方或下方,这种方法效果就不太好。可以使用以下方法手动修复此问题:\useasboundingbox ([yshift=-1cm]image.south east) rectangle ([yshift=1cm]image.north west);
修改1cm
和调整-1cm
为有用的长度,具体取决于标签位置。从这个意义上说,Bernard 的答案更好。
\documentclass[letterpaper]{article}
\usepackage{subcaption}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\begin{document}
\begin{figure}
\centering
\begin{subfigure}[b]{0.9\textwidth}
\centering
\begin{tikzpicture}
\node[anchor=south west,inner sep=0](image) at (0,0) {\includegraphics[width=0.35\textwidth]{example-image-a}};
\useasboundingbox (image.south east) rectangle (image.north west);
\begin{scope}[x={(image.south east)},y={(image.north west)}]
\node[anchor=east,align=right,draw=none] at (-0.10,0.80) {A loooooooooong label};
\node[anchor=west,align=left ,draw=none] at ( 1.10,0.05) {Short Label};
\draw[black,->,thick] (-0.10,0.80) -- (0.40,0.70); %Outlet expansion line
\draw[black,->,thick] ( 1.10,0.05) -- (0.30,0.10); %Seeding array line
\end{scope}
\end{tikzpicture}
\caption{\label{subfig1}}
\end{subfigure}
\label{fig1}
\caption{An off-center figure}
\end{figure}
\end{document}