Wrapfigure,如果不是页面顶部,则 \baselineskip

Wrapfigure,如果不是页面顶部,则 \baselineskip

总结

wrapfigure 环境会根据页面是否仍为空来以不同的方式对齐包装内容。如果页面不为空,则会将包装内容降低\intextsep


我正在使用 wrapfigure 将小图像嵌入段落中。效果很好,但我遇到了这个问题。这个问题的新问题是,当段落从页面的第一行开始时,问题不会发生,所以我需要检测“页面第一行”的情况。

在引用的问题中,环绕图形的段落永远不能出现在页面的第一行,因此\vspace*{-1\baselineskip}%在环绕图形的开头添加内容可以解决问题。

在我的方法中,完全有可能,有时甚至是想要的,让包装段落从页面的第一行开始。我使用包装如下:

\renewcommand{\subsubsubsection}[2][\relax]{%
              \ifx#1\relax
                    \relax
              \else
                    \settowidth\wrapwidth{\includegraphics{#1}}
                    \begin{wrapfigure}{r}{\wrapwidth}\vspace*{-1\baselineskip}%
                            \includegraphics{#1}
                    \end{wrapfigure}
              \fi%
              \@startsection{paragraph}{4}{\z@}%
                    {3.25ex \@plus1ex \@minus.2ex}%
                    {-1em}%
                    {\raggedsection\normalfont\sc\nobreak\bfseries\small}%
                    {#2}
}

\subsubsubsection{Just a title}
blah blah blah

\subsubsubsection[imagefile]{Title with image}
blah blah blah

我如何检测我的\subsubsubsection意愿是否是页面上的第一个段落和行,以便我可以\vspace*{-1\baselineskip}%在需要时放置?


编辑1

我尝试将其附加%到宏定义中的每一行,但没有产生任何明显的变化。

以下是说明该问题的完整文档:

\documentclass{article}
\usepackage{wrapfig}
\usepackage{lipsum}

\makeatletter
\newcommand{\subsubsubsection}[2][\relax]{%
    \begin{wrapfigure}{r}{1cm}\rule{1cm}{2cm}\end{wrapfigure}%
    \@startsection{paragraph}{4}{\z@}{1em}{-1em}{\bfseries}{#2}%
}
\makeatother

\begin{document}

\subsubsubsection[foo]{This is how I want the black box align to the text}\lipsum[1]

\subsubsubsection[foo]{This is NOT how I want the black box to align with the text}\lipsum[1]

\newpage
\subsubsubsection[foo]{What I want}\lipsum

\subsubsubsection[foo]{Not what I want}
Now, if I just produce enough text to make the beginning of the next subsubsubsection 
become the first line on the new page.It will work. Flip over, to see for yourself! I'll just add some text here to avoid the problems that arise when the wrapped paragraph is fewer lines long than the wrapfigure is high. I'll just add some text here to avoid the problems that arise when the wrapped paragraph is fewer lines long than the wrapfigure is high. I'll just add some text here to avoid the problems that arise when the wrapped paragraph is fewer lines long than the wrapfigure is high.

\rule{2pt}{.34\textheight}

Just a little more to fill up this page \ldots

\subsubsubsection[foo]{Happens to be what I want}\lipsum[2]

\end{document}

我去掉了 Koma-Script、\includegraphics以及 width-calculation 和\ifx。这样会更难看清,但是将以 开头的行替换为 时,问题仍然\@startsection存在#2

下面的图片显示了我想要的和不想要的:

上段设在页面第一行

答案1

从发布问题到现在,我花了大部分时间阅读 TeX、盒子和模式。这些新获得的知识让我很容易发现,我上面描述的问题只不过是 的正常和预期行为wrapfigure

关键内容如下:

\ProvidesPackage{wrapfig}[2003/01/31 \space  v 3.6]

其中:

160    \ifdim\pagetotal=\z@ % \WF@info{Put \WF@wfname at top of p.\thepage}%
161     \global\advance\WF@size-\intextsep
162    \else % \WF@info{Putting \WF@wfname in middle of page}%
163     \setbox\WF@box\hbox{\lower\intextsep\box\WF@box}%
164    \fi \dp\WF@box\z@ \box\WF@box \@tempa

因此答案很简单:wrapfigure 环境必须始终知道\pagetotal=\z@。这可以通过将行添加\let\pagetotal\z@到宏定义来实现。对于上面的示例文档,可以这样做:

\newcommand{\subsubsubsection}[2][\relax]{%
    \let\pagetotal\z@
    \begin{wrapfigure}{r}{1cm}\rule{1cm}{2cm}\end{wrapfigure}%
    \@startsection{paragraph}{4}{\z@}{1em}{-1em}{\bfseries}{#2}%
}

相关内容