在页面顶部应用 \intextsep

在页面顶部应用 \intextsep

在尝试回答这个问题时:两栏问题——图片和文字的定位,我意识到我需要将wrapfigure向上移动以解决 增加的额外间距\colorbox。我调整了\intextsep长度,效果很好,除了页面顶部。请注意,黑色框未与水平线的顶部对齐,但蓝色框与水平线的顶部对齐。

在此处输入图片描述

我发现了许多可以“修复”此问题的 hackish 解决方案(但在页面顶部添加了少量空白),包括:,,,,,,\smash{}这些都让TeX 知道我们不在页面顶部,因此会应用这些解决方案。 \llap{}\rlap{}\hphantom{}\vphantom{}\\intextsep

问题:

  1. 修复这个间距的正确方法是什么?

  2. 我该如何在顶部添加一个零高度、零宽度的框,这样就不会添加任何空间\intextsep,但会让 TeX 表现得好像它不在页面顶部一样?当然,#1 的替代解决方案可能不需要这样做,但我仍然有兴趣了解。

以下是代码:

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{wrapfig}
\usepackage{xcolor}

\setlength{\parindent}{0pt}
\setlength{\parskip}{\baselineskip}

\newcommand*{\WarningHead}[1]{%
  \colorbox{black}{\parbox{\linewidth}{\color{white}\textbf{#1}}}\vspace{10px}%
}

\newcommand*{\WrapFigure}[1]{%
  \setlength{\intextsep}{-3pt}%
  \begin{wrapfigure}{r}[0pt]{0.3\linewidth}%
    \raggedleft% right align the figures
    \includegraphics[width=0.95\linewidth]{#1}%
  \end{wrapfigure}
}%

\newcommand*{\lorem}{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis pellentesque iaculis nunc eget congue. Etiam lobortis nisi velit. Proin tristique massa a lectus ullamcorper semper. Aenean ut dignissim diam. Integer ullamcorper eros nibh. Cras molestie neque quis lectus lobortis egestas. Maecenas vel tortor in nulla sagittis venenatis. Nulla ac eros dui, eget lacinia ante. Fusce elementum nisl ac tortor hendrerit id lacinia orci malesuada. Praesent eu iaculis mi. Vestibulum sodales tempor rutrum. }

\begin{document}    \WrapFigure{foo}
\WarningHead{WARNING}
\lorem

\color{blue}
\WrapFigure{foo}
\WarningHead{WARNING}
\lorem
\end{document}

答案1

尝试以下方法:

\newcommand*\WarningHead[1]{%
  \colorbox{black}{\parbox{\linewidth}{\color{white}\textbf{#1}}}\vspace{10px}%
}

\newcommand*\WrapFigure[1]{\leavevmode%
  \setlength\intextsep{-3pt}%
  \begin{wrapfigure}{r}[0pt]{0.3\linewidth}%
    \raggedleft% right align the figures
    \includegraphics[width=0.95\linewidth]{#1}%
  \end{wrapfigure}
}%

在此处输入图片描述

为了防止额外的垂直空间,你可以这样做

\newcommand*\WrapFigure[1]{%
  \ifhmode\else\leavevmode\vspace*{-\normalbaselineskip}\fi%
  \setlength\intextsep{-3pt}%

答案2

\phantom{a}只需在 之后插入一条指令即可\begin{document}。我根本不会称其为“黑客行为”。:-)

相关内容