获取图形和标题的高度 - 用于包装图形分页符检测

获取图形和标题的高度 - 用于包装图形分页符检测

我想要一个函数,它可以自动检测在 wrapfigure 环境中使用时图形及其标题是否会超出页面,以便它将与我在代码中放置的文本保持一致。为此,我找到了一个有用的线程这里已经从我使用的地方詹姆斯·阿什顿回答得出以下解决方案。

\newsavebox\curwrapfig
\makeatletter
\long\def\wrapfiguresafe#1#2#3{%
  \sbox{\curwrapfig}{ 
        \includegraphics[scale = \figureScale]{#1}
    }
  \par\penalty-100%
  \begingroup % preserve \dimen@
    \dimen@\pagegoal \advance\dimen@-\pagetotal % space left
    \advance\dimen@-\baselineskip % allow an extra line
    \ifdim \ht\curwrapfig>\dimen@ % not enough space left
      \break%
    \fi%
  \endgroup%
  \begin{wrapfigure}{O}{0.5\textwidth}
        \centering
        \includegraphics[scale = \figureScale]{#1}
        \caption{#3}
        \label{#2}
  \end{wrapfigure}
}
\makeatother

但是,此解决方案不包括标题高度。因此,我将上述代码中的 savebox 替换为以下内容

\sbox{\curwrapfig}{ \parbox{0.5\textwidth}
    {
        \addtocounter{figure}{1}
        \includegraphics[scale = \figureScale]{#1}
        \vspace{0.7\abovecaptionskip}
        \newline \small
        \textbf{Figure \thefigure:} #3
        \addtocounter{figure}{-1}
    }
}

现在的问题是,这个解决方案似乎不起作用。事实上,测量的高度现在似乎更小了,也就是说,在我尝试包含标题高度后,首先导致分页符的图形不再导致分页符。为了说明这一点,我创建了以下最小工作示例。在这里,当使用第一个保存框时,在放置图像 A 之前会放置一个分页符。图像 B 有一个长标题,并被移动到下一页,但不在预期文本旁边。当使用第二个保存框解决此问题时,现在图像 A 也会移动到下一页,但不包含其文本。

\documentclass{article}

\usepackage{wrapfig}
\usepackage{lipsum}
\usepackage{graphicx}

\newsavebox\curwrapfig
\makeatletter
\long\def\wrapfiguresafe#1#2#3{%
  \sbox{\curwrapfig}{ 
    \includegraphics[height=#2]{#1}
  }
  %\sbox{\curwrapfig}{ \parbox{0.5\textwidth}
    %{
      %\addtocounter{figure}{1}
      %\includegraphics[height=#2]{#1}
      %\vspace{0.7\abovecaptionskip}
      %\newline \small
      %\textbf{Figure \thefigure:} #3
      %\addtocounter{figure}{-1}
    %}
  %}
  \par\penalty-100%
  \begingroup % preserve \dimen@
    \dimen@\pagegoal \advance\dimen@-\pagetotal % space left
    \advance\dimen@-\baselineskip % allow an extra line
    \ifdim \ht\curwrapfig>\dimen@ % not enough space left
      \break%
    \fi%
  \endgroup%
  \begin{wrapfigure}{O}{0.5\textwidth}
        \centering
        \includegraphics[width = 0.45\textwidth,height=#2]{#1}
        \caption{#3}
  \end{wrapfigure}
}
\makeatother

\begin{document}

\paragraph{1} \lipsum[1]
\paragraph{2} \lipsum[2]
\paragraph{3} \lipsum[3]

\wrapfiguresafe{example-image-a}{65mm}
{This is figure A, paragraph \textbf{4} should be right beside this figure on a new page.}

\paragraph{4} \lipsum[4]
\paragraph{5} \lipsum[5]

\wrapfiguresafe{example-image-B}{40mm}
{This is figure B, paragraph \textbf{6} should be right beside this figure. Figure B has a very long caption, and therefore it will surely run off the page. Because 'O' was specified, figure B is moved to the next page, but in doing so it is separated from paragraph \textbf{6}}

\paragraph{6} \lipsum[6]
\paragraph{7} \lipsum[7]

\end{document}

是否有人知道一种测量图形(包括其标题)高度的好方法,以便可以使用该测量值来检查分页符?

答案1

请注意,wrapfig 和 lipsum 不能很好地协同工作。我不知道,\pagetotal但如果准确的话,这应该可以按照你想要的方式工作。

\documentclass{article}

\usepackage{wrapfig}
\usepackage{lipsum}
\usepackage{graphicx}

\newsavebox\curwrapfig
\makeatletter
\long\def\wrapfiguresafe#1#2#3{%
  \setbox\curwrapfig=\vbox{\hsize=0.5\textwidth
    \def\@captype{figure}%
    \centering
    \includegraphics[width=0.45\textwidth,height=#2]{#1}
    \caption{#3}
  }%
  \par\penalty-100%
  \begingroup % preserve \dimen@
    \dimen@\pagegoal \advance\dimen@-\pagetotal % space left
    \advance\dimen@-\baselineskip % allow an extra line
    %\the\dimen@
    \ifdim \ht\curwrapfig>\dimen@ % not enough space left
      \clearpage
    \fi%
  \endgroup%
  \begin{wrapfigure}{O}{0.5\textwidth}
        \usebox\curwrapfig
  \end{wrapfigure}
}
\makeatother

\begin{document}
\sloppy
\paragraph{1} \LipsumPar{1}
\paragraph{2} \LipsumPar{2}
\paragraph{3} \LipsumPar{3}

\wrapfiguresafe{example-image-a}{65mm}
{This is figure A, paragraph \textbf{4} should be right beside this figure on a new page.}

\paragraph{4} \LipsumPar{4}
\paragraph{5} \LipsumPar{5}

\wrapfiguresafe{example-image-b}{40mm}
{This is figure B, paragraph \textbf{6} should be right beside this figure. Figure B has a very long caption, and therefore it will surely run off the page. Because 'O' was specified, figure B is moved to the next page, but in doing so it is separated from paragraph \textbf{6}}

\paragraph{6} \LipsumPar{6}
\paragraph{7} \LipsumPar{7}

\end{document}

此版本使用\needspace常规保存箱。

\documentclass{article}

\usepackage{wrapfig}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{needspace}

\newsavebox\curwrapfig
\makeatletter
\long\def\wrapfiguresafe#1#2#3{%
  \sbox\curwrapfig{\parbox{0.5\textwidth}{%
    \def\@captype{figure}%
    \centering
    \includegraphics[width=0.45\textwidth,height=#2]{#1}
    \caption{#3}
  }}%
  \needspace{\dimexpr \ht\curwrapfig+\dp\curwrapfig+2\intextsep}%
  \begin{wrapfigure}{O}{0.5\textwidth}
    \usebox\curwrapfig
  \end{wrapfigure}
}
\makeatother

\begin{document}
\sloppy
\paragraph{1} \LipsumPar{1}
\paragraph{2} \LipsumPar{2}
\paragraph{3} \LipsumPar{3}

\wrapfiguresafe{example-image-a}{65mm}
{This is figure A, paragraph \textbf{4} should be right beside this figure on a new page.}

\paragraph{4} \LipsumPar{4}
\paragraph{5} \LipsumPar{5}

\wrapfiguresafe{example-image-b}{40mm}
{This is figure B, paragraph \textbf{6} should be right beside this figure. Figure B has a very long caption, and therefore it will surely run off the page. Because 'O' was specified, figure B is moved to the next page, but in doing so it is separated from paragraph \textbf{6}}

\paragraph{6} \LipsumPar{6}
\paragraph{7} \LipsumPar{7}

\end{document}

相关内容