我定期需要/想要稍微“推动”图形或表格的位置,使其稍微偏离中心(向左移动 xx em,或向右移动 xx em)。使用 \hspace{xxem} 很容易,如以下 MWE 所示。但是,虽然图形和/或表格本身按预期移动,但标题仍然严格保持在页面的中心。我可以通过(比如)在标题的左端或右端填充尽可能多的 ~ 来强制将其向左或向右移动,但我想知道是否有一种更优雅/更强大的方法来获取整个图形或表格(包括标题),并将其全部移动到中心的左侧或右侧?
首先十分感谢...
\documentclass[12pt]{article}
% set up basic graphics
\usepackage{epsfig,float}
\begin{document}
It is easy to `nudge' an image a bit to the left (or right) of center - simply use hspace --
but this doesn't also move the caption:
\begin{figure}[H]
\begin{center}
\hspace{-5em}\includegraphics[scale=1,keepaspectratio=true]{test.eps}
\caption{move to left of centre...}
\end{center}
\end{figure}
And, same for moving a table left- or right-of-centre -- hspace moves the table, but not the
caption:
\begin{table}[H]
\caption{left-of-center}
\centering
\hspace{-5em}\begin{tabular}{c c c c}
\hline\hline
Case & Method\#1 & Method\#2 & Method\#3 \\ [0.5ex]
\hline
1 & 50 & 837 & 970 \\
2 & 47 & 877 & 230 \\
3 & 31 & 25 & 415 \\
4 & 35 & 144 & 2356 \\
5 & 45 & 300 & 556 \\ [1ex]
\hline
\end{tabular}
\end{table}
\end{document}
答案1
更新后续问题:处理长标题。
如果标题过长,则会溢出边距。为了使标题保持在文本宽度之内,并与图像/表格居中,其宽度应减小为图像/标题的宽度。
为此,longCap
定义了一种新的标题格式(),该格式将图例对齐,并且其宽度限制为图形/表格的宽度,因此位于中央。
对于短标题,新定义的标题格式(shortCap
)将使标题保持图形/表格居中。
\documentclass[12pt]{article}
% set up basic graphics
\usepackage{float}
\usepackage{graphicx}
\usepackage{caption}% added <<
\usepackage{ifthen}% added <<
\newlength{\nudge}
\newlength{\Cwidth}
\DeclareCaptionFormat{longCap}% long caption > justified
{\ifthenelse{\lengthtest\nudge>0pt}{\setlength{\Cwidth}{\dimexpr\linewidth-\nudge}}{\setlength{\Cwidth}{\dimexpr\linewidth+\nudge}}
\hspace*{\nudge}\begin{minipage}{\the\Cwidth} #1#2#3\end{minipage}
}
\DeclareCaptionFormat{shortCap}% short caption > centered
{\ifthenelse{\lengthtest\nudge>0pt}{\setlength{\Cwidth}{\dimexpr\linewidth-\nudge}}{\setlength{\Cwidth}{\dimexpr\linewidth+\nudge}}
\hspace*{\nudge}\begin{minipage}{\the\Cwidth} \centering #1#2#3\end{minipage}
}
\usepackage{showframe} % ONLY to show thw margins <<<<<<<<<<<<<<<
\begin{document}
It is easy to `nudge' an image a bit to the left (or right) of center - simply use hspace --
\section{Long caption => justified}
\setlength{\nudge}{-6em}
\begin{figure}[H]
\begin{center}
\hspace{\nudge}
\includegraphics[scale=1,keepaspectratio=true]{example-image}
\captionsetup{format=longCap}
\caption{move to left of centre... As any dedicated reader can clearly see, the Ideal of
practical reason is a representation of, as far as I know, the things
in themselves; as I have shown elsewhere, the phenomena should only be
used as a canon for our understanding.}
\end{center}
\end{figure}
\newpage
And, same for moving a table left- or right-of-centre -- hspace moves the table, but not the
caption:
\setlength{\nudge}{12em}
\begin{table}[H]
\centering
\captionsetup{format=longCap}
\caption{Right-of-center As any dedicated reader can clearly see, the Ideal of
practical reason is a representation of, as far as I know, the things
in themselves; as I have shown elsewhere, the phenomena should only be
used as a canon for our understanding. }
\hspace{\nudge}
\begin{tabular}{c c c c}
\hline\hline
Case & Method\#1 & Method\#2 & Method\#3 \\ [0.5ex]
\hline
1 & 50 & 837 & 970 \\
2 & 47 & 877 & 230 \\
3 & 31 & 25 & 415 \\
4 & 35 & 144 & 2356 \\
5 & 45 & 300 & 556 \\ [1ex]
\hline
\end{tabular}
\end{table}
\newpage
\section{Short caption => centered}
\setlength{\nudge}{-6em}
\begin{figure}[H]
\begin{center}
\hspace{\nudge}
\includegraphics[scale=1,keepaspectratio=true]{example-image}
\captionsetup{format=shortCap}
\caption{move to left of centre... }
\end{center}
\end{figure}
And, same for moving a table left- or right-of-centre -- hspace moves the table, but not the
caption:
\setlength{\nudge}{12em}
\begin{table}[H]
\centering
\captionsetup{format=shortCap}
\caption{Right-of-center. }
\hspace{\nudge}
\begin{tabular}{c c c c}
\hline\hline
Case & Method\#1 & Method\#2 & Method\#3 \\ [0.5ex]
\hline
1 & 50 & 837 & 970 \\
2 & 47 & 877 & 230 \\
3 & 31 & 25 & 415 \\
4 & 35 & 144 & 2356 \\
5 & 45 & 300 & 556 \\ [1ex]
\hline
\end{tabular}
\end{table}
\end{document}