居中并对齐对页上的图形

居中并对齐对页上的图形

这是问题的延伸“如何确保两对图形位于两张相对的页面上?”。我不仅想确保图形在对开的页面上,还想确保高度较小的图形与高度较大的图形对齐。后者应在页面上居中。以下示例应该可以解释该问题:

\documentclass{book}
\usepackage{dpfloat}
\usepackage[demo]{graphicx}

\begin{document}

Some Text

\begin{figure}
  \begin{leftfullpage}
    \centering
    \includegraphics[width=5cm,height=3cm]{inserthere}
    \caption{This is the short caption.}
  \end{leftfullpage}
\end{figure}

\begin{figure}
  \begin{fullpage}
    \centering
    \includegraphics[width=5cm,height=3cm]{inserthere}
    \caption{This second caption does not only look larger, it actually is,
      by one line.}
  \end{fullpage}
\end{figure}

\end{document}

结果是未对齐的数字

我希望图 2 保持在同一位置(页面居中),但图 1 稍微向上移动,以便两个图的顶部以及标题的开头对齐。

答案1

TeX 编译器 pdfTeX/LuaTeX(DVI 和 PDF)和 XeTeX 提供了位置跟踪功能。因为直到页面发出去之后才能知道最终位置,所以需要运行两次 LaTeX。包zref-savepos(来自包zref)提供了该功能的接口。在浮动块的开头,存储垂直位置。然后比较两个图像的位置,并移动较小的图像。在图形的末尾重新进行移动。然后 LaTeX 认为浮动块仍然具有与之前相同的高度,并且不会改变页面上图形的位置,这也会移动浮动块开头保存的垂直位置。

\documentclass[12pt]{book}
\usepackage{dpfloat}
\usepackage[demo]{graphicx}

\usepackage{zref-savepos}
\providecommand*{\zsaveposy}{\zsavepos}% support older zref-savepos

\begin{document}

Some Text

\begin{figure}
  \begin{leftfullpage}
    \centering
    \zsaveposy{figa}%
    \ifnum\zposy{figb}>\zposy{figa} %
      \vspace{-\dimexpr\zposy{figb}sp-\zposy{figa}sp}%
    \fi
    \includegraphics[width=5cm,height=3cm]{inserthere}
    \caption{This is the short caption.}
    \ifnum\zposy{figb}>\zposy{figa} %
      \vspace{\dimexpr\zposy{figb}sp-\zposy{figa}sp}%
    \fi
  \end{leftfullpage}
\end{figure}

\begin{figure}
  \begin{fullpage}
    \centering
    \zsaveposy{figb}%
    \ifnum\zposy{figa}>\zposy{figb} %
      \vspace{-\dimexpr\zposy{figa}sp-\zposy{figb}sp}%
    \fi
    \includegraphics[width=5cm,height=3cm]{inserthere}
    \caption{This second caption does not only look larger, it actually is,
      by one line.}
    \ifnum\zposy{figa}>\zposy{figb} %
      \vspace{\dimexpr\zposy{figa}sp-\zposy{figb}sp}%
    \fi
  \end{fullpage}
\end{figure}

\end{document}

结果

答案2

您可以重新定义字幕构建命令,以便字幕的高度为零。

\documentclass{book}
\usepackage[a6paper,margin=10pt]{geometry}
\usepackage{dpfloat}
\usepackage[demo]{graphicx}

\makeatletter
\long\def\@makecaption#1#2{%
  \vskip\abovecaptionskip
  \sbox\@tempboxa{#1: #2}%
  \ifdim \wd\@tempboxa >\hsize
    \vbox to 0pt{#1: #2\par\vss}
  \else
    \vbox to 0pt{\hb@xt@\hsize{\hfil\box\@tempboxa\hfil}\vss}%
  \fi
  \vskip\belowcaptionskip}
\makeatother

\begin{document}

Some Text

\begin{figure}[p]
  \begin{leftfullpage}
    \centering
    \hrule
    \includegraphics[width=5cm,height=3cm]{inserthere}
    \hrule
    \caption{This is the short caption.}
  \end{leftfullpage}
\end{figure}

\begin{figure}[p]
  \begin{fullpage}
    \centering
    \hrule
    \includegraphics[width=5cm,height=3cm]{inserthere}
    \hrule
    \caption{This second caption does not only look larger, it
    actually is, by one line more.}
  \end{fullpage}
\end{figure}

\end{document}

示例输出

将重新定义放在包含图形的本地组中,以仅影响这些标题。

当然,您可能希望将标题视为具有不同的高度。下面设置一个环境,fixedheightcaptions其中所有标题都有可定义的高度,默认值为0pt

\documentclass{book}
\usepackage[a6paper,margin=10pt]{geometry}
\usepackage{dpfloat}
\usepackage[demo]{graphicx}

\makeatletter
\newlength{\@fcaptionheight}
\long\def\@makefixedcaption#1#2{%
  \vskip\abovecaptionskip
  \sbox\@tempboxa{#1: #2}%
  \ifdim \wd\@tempboxa >\hsize
    \vbox to \@fcaptionheight{#1: #2\par\vss}
  \else
    \vbox to \@fcaptionheight{\hb@xt@\hsize{\hfil\box\@tempboxa\hfil}\vss}%
  \fi
  \vskip\belowcaptionskip}

\newenvironment{fixedheightcaptions}[1][0pt]{\setlength{\@fcaptionheight}{#1}
\let\@makecaption\@makefixedcaption}{}

\makeatother

\begin{document}

Some text.

\begin{figure}[h]
  \centering
  \hrule
  \caption{Test caption, normal}
\end{figure}

Some text.

\begin{fixedheightcaptions}[5cm]
  \begin{figure}[h]
    \centering \hrule
    \caption{Test caption of height 5cm}
  \end{figure}
\end{fixedheightcaptions}

Some text.

\begin{fixedheightcaptions}
  \begin{figure}[p]
    \begin{leftfullpage}
      \centering \hrule
      \includegraphics[width=5cm,height=3cm]{inserthere}
      \hrule
      \caption{This is the short caption.}
    \end{leftfullpage}
  \end{figure}

  \begin{figure}[p]
    \begin{fullpage}
      \centering \hrule
      \includegraphics[width=5cm,height=3cm]{inserthere}
      \hrule
      \caption{This second caption does not only look larger, it
      actually is, by one line more.}
    \end{fullpage}
  \end{figure}
\end{fixedheightcaptions}
\end{document}

这是输出的第一页:

输出的第一页

相关内容