如何避免 wrapfig 中不必要的额外空格

如何避免 wrapfig 中不必要的额外空格

我的目标在大多数文字处理程序中都很容易实现,在这里也确实可以实现。我想在某个部分的顶部,在右侧显示该部分所讨论人物的照片,并且我希望文字环绕该照片。这看起来相当简单,对吧?

我为此使用了 \wrapfig,但出现了一些相当奇怪的副作用,它不仅能很好地环绕图片,还似乎影响了下一节中的每个段落。示例在底部。以下是它产生的结果:

在此处输入图片描述

以下页面是:

在此处输入图片描述

如你看到的:

(1) 节标题顶部和照片顶部之间有间隙。我希望照片从节标题的同一行开始,而不是低 1/4 行。

(2)包装覆盖了标题

(3)此“包装”块似乎在该部分的每个段落中重复出现。

(4) 如果照片太靠近页面底部,问题会更加严重,因此针对这种情况,我会手动添加分页符。

以下是 MWE:

\documentclass[paper=7in:10in,11pt,pagesize=pdftex,openright,headings=twolinechapter,chapterprefix=true]{scrbook}

\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{xcolor}
\usepackage{colortbl}
\usepackage{makeidx}
\usepackage{fontspec}
\usepackage{needspace}
\usepackage{longtable}
\usepackage{framed}
\usepackage{enumitem}
\usepackage{setspace}
\usepackage{tcolorbox}
\usepackage{url}
\usepackage{showframe}
\usepackage{lineno}


\usepackage[natbibapa]{apacite}
\bibliographystyle{apacite}
%\usepackage{natbib}
%\usepackage[style=authoryear,sorting=ynt]{biblatex}
%\DefineBibliographyStrings{english}{urlseen = {Accessed\addcolon}}

\setmainfont[Ligatures={Common,TeX}, Numbers={OldStyle}]{Palatino Linotype}
%\KOMAoptions{DIV=last}   % goofy KOMA requirement whenever you change leading
\addtokomafont{footnote}{\footnotesize\fontspec[Ligatures=TeX]{Alegreya Sans}}
\deffootnote{1.5em}{1em}{% modified example from page 83
  \makebox[1.5em][l]{\textsuperscript{\thefootnotemark}}}

\definecolor{skyblue}{rgb}{0.53, 0.81, 0.92}
\definecolor{shadecolor}{rgb}{0.95, 0.95, 0.95}

\makeindex

%page is 7 x 10.  With these margins the printed area
%is 5.75 inches by 9 inches.
\usepackage[inner=0.75in,outer=0.50in,top=0.75in, bottom=0.75in, footnotesep=.3in, footskip=.4in]{geometry}

\renewcommand*\chapterheadstartvskip{\vspace*{.1\textheight}}
\renewcommand*\chapterheadendvskip{\vspace*{.1\textheight}}

\addtokomafont{chapter}{\fontspec[Ligatures=TeX]{Alegreya Sans SC Light}}
\addtokomafont{chapterprefix}{\fontspec[Ligatures=TeX]{Alegreya Sans SC Light}\Huge}

\addtokomafont{chapterprefix}{\raggedleft}
\renewcommand*{\chapterformat}{%
   \mbox{\scalebox{1}{\chapappifchapterprefix{\nobreakspace}}%
   \scalebox{4}{\color{gray}\thechapter}\enskip}}


%Page size settings
\pdfpagewidth=\paperwidth
\pdfpageheight=\paperheight
\widowpenalty10000
\clubpenalty10000

\hyphenation{}

\addtokomafont{section}{\raggedright}
\addtokomafont{subsection}{\raggedright}
\setcounter{secnumdepth}{0}

\setlength{\emergencystretch}{15pt}

\newenvironment{glossarylist}%
   {\begin{list}{}{\setlength\labelwidth{0pt}%
                   \setlength\itemindent{-\leftmargin}%
                   \let\makelabel\descriptionlabel}}%
   {\end{list}}


   
\newenvironment{centerTable}[1][Examples]%
   {\begin{longtable}{| p{2.5in} |} \hline \rowcolor{lightgray} \emph{#1} \\ \hline }%
   {\end{longtable}}

\newcommand{\partfrontpage}[1]{%
    \cleardoublepage\vspace*{2.0in}\thispagestyle{empty}%
    \begin{center}%
    \noindent\fontspec[Ligatures=TeX]{Alegreya Sans SC Light}%
    \doublespacing\Huge #1%
    \end{center}}

\newcommand{\centerLine}[1]{ #1 \\ \hline }
   
   
%this allows graphics from one folder to be included in one style
%but when building hypertext it includes a different set of graphics from 
%a different folder
\newcommand{\bookcoverimage}[1]{\noindent\includegraphics[width=2in]{img/#1}}
    
\newcommand{\simpleimage}[2]{%
  \begin{figure}[!ht]%
  %\vspace{10pt}%
  \begin{center}%
    \noindent\includegraphics[width=5in]{img/#1}\\%
    \emph{#2}%
  \end{center}%
  \end{figure}%
}
\newcommand{\sizeimage}[3]{%
  \begin{figure}[!ht]%
  %\vspace{10pt}%
  \begin{center}%
    \noindent\includegraphics[width=#3in]{img/#1}\\%
    \emph{#2}%
  \end{center}%
  \end{figure}%
}

%\intextsep = 0pt

\newcommand{\rightimage}[3]{%
  \begin{wrapfigure}{r}{#3in}%
  \noindent\includegraphics[width=#3in]{img/#1}\\%
  \centering\emph{#2}%
  \end{wrapfigure}%
}


\newcommand{\narrowimage}[1]{\noindent\includegraphics[width=2in]{img/#1}}

\newcommand{\citePlain}[1]{\footnote{\citep{#1}}}
\newcommand{\citePlainP}[2]{\footnote{\citep[#2]{#1}}}

\newcommand{\citeSpecial}[1]{\footnotetext{\citep{#1}}}
\newcommand{\citeSpecialP}[2]{\footnotetext{\citep[#2]{#1}}}


%control whether extra vertical space is distributed between paragraphs
%either raggedbottom or flushbottom
\raggedbottom


\usepackage{tcolorbox}
\newtcolorbox{boxedQuote}{colback=gray!30,boxrule=0pt,arc=0pt,boxsep=2pt,left=2pt,right=2pt,leftrule=1pt}

\begin{document}


Dave Campbell Purcell, second son of Margaret and David Purcell, always maintained a strong interest in our David family history---especially in his later life. In the 1960s he and his wife Elizabeth (``Betty''), made at least one trip from their home in Maine to visit their cousins in the Wallace, Gulf Shore, Fox Harbour areas of northern Nova Scotia.  I have several letters that Dave had written to my mother during those years. In later years, I corresponded with him on all things relating to the David family. He was particularly interested in any effort to learn more about the early family members who came to North America from Europe. Several times Mary Lee and I visited with Dave and Betty at their home in Maine. They were always charming and welcoming – Betty interested to know about our son and daughter, and Dave, his desk covered with notes, charts, and printouts, eager to discuss any new discoveries on the family history front. Dave died January 8th, 2004 at the age of eighty-eight and Betty on July 10, 2014.

\rightimage{ch19-DavidBettyPurcell.jpg}{Dave \& Betty Purcell   ca 1998}{2}

\section{Dave Campbell Purcell}

Dave Campbell Purcell, second son of Margaret and David Purcell, always maintained a strong interest in our David family history---especially in his later life. In the 1960s he and his wife Elizabeth (``Betty''), made at least one trip from their home in Maine to visit their cousins in the Wallace, Gulf Shore, Fox Harbour areas of northern Nova Scotia.  I have several letters that Dave had written to my mother during those years. In later years, I corresponded with him on all things relating to the David family. He was particularly interested in any effort to learn more about the early family members who came to North America from Europe. Several times Mary Lee and I visited with Dave and Betty at their home in Maine. They were always charming and welcoming – Betty interested to know about our son and daughter, and Dave, his desk covered with notes, charts, and printouts, eager to discuss any new discoveries on the family history front. Dave died January 8th, 2004 at the age of eighty-eight and Betty on July 10, 2014.

Dave and Betty Purcell had three children: David, Joanne and Elizabeth. In early December, 2017, I was able to establish contact with Joanne and her husband Bob Flynn. They live in Vermont. Throughout the spring of 2018 Joanne and I got acquainted through emails and phone chats. At one point I asked her if she had some special memories of her father that she could share with me. This is a part of her reply:

My father and I were very close; I have so many memories: spending endless house at his elbow in his workshop watching him explore, create, take apart, put together. Electrical . . . mechanical, didn't matter, he could fix anything---all the while teaching, explaining. We once even took apart a television set so he could describe how it worked! I can still envision the large picture tube, something that doesn't even exist anymore. I remember his infectious laugh, his wonderful sense of humour  . . . and his pranks  . . . he loved to play harmless pranks. One of the best was the Halloween he put a vacuum cleaner under our porch so that when the Trick or Treaters stepped up to the door, the machine would turn on. Only a few ran away!

Dave Campbell Purcell, second son of Margaret and David Purcell, always maintained a strong interest in our David family history---especially in his later life. In the 1960s he and his wife Elizabeth (``Betty''), made at least one trip from their home in Maine to visit their cousins in the Wallace, Gulf Shore, Fox Harbour areas of northern Nova Scotia.  I have several letters that Dave had written to my mother during those years. In later years, I corresponded with him on all things relating to the David family. He was particularly interested in any effort to learn more about the early family members who came to North America from Europe. Several times Mary Lee and I visited with Dave and Betty at their home in Maine. They were always charming and welcoming – Betty interested to know about our son and daughter, and Dave, his desk covered with notes, charts, and printouts, eager to discuss any new discoveries on the family history front. Dave died January 8th, 2004 at the age of eighty-eight and Betty on July 10, 2014.

Dave and Betty Purcell had three children: David, Joanne and Elizabeth. In early December, 2017, I was able to establish contact with Joanne and her husband Bob Flynn. They live in Vermont. Throughout the spring of 2018 Joanne and I got acquainted through emails and phone chats. At one point I asked her if she had some special memories of her father that she could share with me. This is a part of her reply:

My father and I were very close; I have so many memories: spending endless house at his elbow in his workshop watching him explore, create, take apart, put together. Electrical . . . mechanical, didn't matter, he could fix anything---all the while teaching, explaining. We once even took apart a television set so he could describe how it worked! I can still envision the large picture tube, something that doesn't even exist anymore. I remember his infectious laugh, his wonderful sense of humour  . . . and his pranks  . . . he loved to play harmless pranks. One of the best was the Halloween he put a vacuum cleaner under our porch so that when the Trick or Treaters stepped up to the door, the machine would turn on. Only a few ran away!

\end{document}

之前我得到了改变的答案\intextsep但是改变会弄乱文档中的数字。

第二步

我的第一个样本存在一些可以修复的问题:

(A)\\应将其替换为,\par以避免标题文本出现问题

(B)\mbox必须在 之后添加\wrapfig以避免导致节中的每个段落都换行。 我不知道为什么会发生这种情况,但我知道这是因为节头导致以下文本被分组? 以某种方式将和 节\mbox分开。\wrapfig

(C) 必须手动控制包裹空间的长度。我现在只好手动为书中的 48 张图片中的每一个设置它。如果它可以从图像大小本身计算出来就好了,但是现在如果我能让它工作,我可以手动为 48 张图片设置盒子大小。

(D) 框的顶部与部分不太正确,因此可以使用将部分中的文本(特别是部分的标题)拉到页面上,\vspace*{-1.2\baselineskip}以使顶部大约位于图像的顶部。

我有时需要将图像放在标题旁边,有时需要将图像放在段落中间。

还剩下两个问题:

(1) 带图片的节头间距略有不同。如果我vspace在图片前放置一个,我就可以将其放在页面的较低位置,然后我可以调整另一张底片vspace以将其对齐。

在此处输入图片描述

(2) 我创建了一个单独的命令,用于将图像放置在文本中(而不是在节头)。如果\mbox在那里,它会留下大约一行的间隙。所以这个新命令消除了\mbox。我使用 来vspace{-1.0\intextsep}取消顶部的空间,然后当然使用手动调整大小来控制下面的空间。

第三步

事实证明,还有另一种情况需要考虑,那就是当节标题出现在页面顶部时,只要上一页没有足够的空间放置图片,就会发生这种情况。

\mbox方法不起作用,因为在上述情况下会\mbox导致页面顶部出现不需要的额外空白。

更好的方法是将图像放入段落中节标题,但使用 vspace 将图像提升到标题的高度。然后,因为我们手动控制要缩短的行数,所以我们可以让空间在正确的位置关闭。

要定义的两个命令是:

\newcommand{\rightimageH}[4][]{%       for use at sections head
  \begin{wrapfigure}[#1]{r}{#4in}%
  \vspace*{-3.1\baselineskip}%
  \noindent\includegraphics[width=#4in]{example-image-1x1.png}\par%
  \centering\emph{#3}%
  \end{wrapfigure}%
}
\newcommand{\rightimage}[4][]{%        for use in body text
  \begin{wrapfigure}[#1]{r}{#4in}%
  \vspace*{-1.0\intextsep}%
  \noindent\includegraphics[width=#4in]{example-image-1x1.png}\par%
  \centering\emph{#3}%
  \end{wrapfigure}%
}

然后将其与如下节标题一起使用:

\section{Dave Campbell Purcell}

\rightimageH[10]{ch19-DavidBettyPurcell.jpg}{Dave \& Betty Purcell   ca 1998}{2}
Dave Campbell Purcell, second son of Margaret ...

请注意,在这种情况下保留了 10 行,但是如果将其直接放入文本中(无标题),则会缩短为 12 行:

\rightimage[12]{ch19-DavidBettyPurcell.jpg}{Dave \& Betty Purcell   ca 1998}{2}
Dave Campbell Purcell, second son of Margaret and David ...

通过这些,我成功地在页面右侧获得了以下紧密环绕的图片:

在此处输入图片描述

答案1

正如之前评论的那样,\\图像和标题之间是错误的,应该是空白行或等效的\par

重置失败wrapfig通常是因为第一个段落在组内开始,而图形是在组外指定的。通常这很容易避免,但在这里,您的第一个段落是节标题,因此更容易\mbox在标题前放置一个假的空段落。无论哪种情况,都需要手动调整剪切的大小,因为 wrapfig 只是假设正常段落,而不考虑标题的较大间距。

在此处输入图片描述

您的示例远非简约,并且使用了未提供的图像,因此我将其删减了一点

\documentclass[paper=7in:10in,11pt,pagesize=pdftex,openright,headings=twolinechapter,chapterprefix=true]{scrbook}

\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{fontspec}
\usepackage{xcolor}

\setmainfont[Ligatures={Common,TeX}, Numbers={OldStyle}]{Palatino Linotype}
%\KOMAoptions{DIV=last}   % goofy KOMA requirement whenever you change leading
\addtokomafont{footnote}{\footnotesize\fontspec[Ligatures=TeX]{Alegreya Sans}}
\deffootnote{1.5em}{1em}{% modified example from page 83
  \makebox[1.5em][l]{\textsuperscript{\thefootnotemark}}}

\definecolor{skyblue}{rgb}{0.53, 0.81, 0.92}
\definecolor{shadecolor}{rgb}{0.95, 0.95, 0.95}

\makeindex

%page is 7 x 10.  With these margins the printed area
%is 5.75 inches by 9 inches.
\usepackage[inner=0.75in,outer=0.50in,top=0.75in, bottom=0.75in, footnotesep=.3in, footskip=.4in]{geometry}

% why set these after geometry???
%Page size settings
\pdfpagewidth=\paperwidth
\pdfpageheight=\paperheight

% these will force bad page breaks
\widowpenalty10000
\clubpenalty10000

\hyphenation{}

\addtokomafont{section}{\raggedright}
\addtokomafont{subsection}{\raggedright}
\setcounter{secnumdepth}{0}

\setlength{\emergencystretch}{15pt}

    

%\intextsep = 0pt

\newcommand{\rightimage}[4][]{%
  \begin{wrapfigure}[#1]{r}{#4in}%
  \noindent\includegraphics[width=#4in]{example-image-1x1.png}\par
  \centering\emph{#3}%
  \end{wrapfigure}%
}


%control whether extra vertical space is distributed between paragraphs
%either raggedbottom or flushbottom
\raggedbottom




%must use WITHOUT gap to next paragraph so that the noindent works.
% would be better to use \@afterheading like section commands 
%which never use \noindent
\newcommand{\setOffPara}[0]{%
    \begin{center}%
    \line(1,0){50}%
    \end{center}%
    \noindent}


  

\begin{document}


Dave Campbell Purcell, second son of Margaret and David Purcell,
always maintained a strong interest in our David family
history---especially in his later life. In the 1960s he and his wife
Elizabeth (``Betty''), made at least one trip from their home in Maine
to visit their cousins in the Wallace, Gulf Shore, Fox Harbour areas
of northern Nova Scotia.  I have several letters that Dave had written
to my mother during those years. In later years, I corresponded with
him on all things relating to the David family. He was particularly
interested in any effort to learn more about the early family members
who came to North America from Europe. Several times Mary Lee and I
visited with Dave and Betty at their home in Maine. They were always
charming and welcoming – Betty interested to know about our son and
daughter, and Dave, his desk covered with notes, charts, and
printouts, eager to discuss any new discoveries on the family history
front. Dave died January 8th, 2004 at the age of eighty-eight and
Betty on July 10, 2014.

\rightimage[12]{ch19-DavidBettyPurcell.jpg}{Dave \& Betty Purcell   ca 1998}{2}
\mbox{}

\vspace*{-1.2\baselineskip}

\section{Dave Campbell Purcell}


Dave Campbell Purcell, second son of Margaret and David Purcell,
always maintained a strong interest in our David family
history---especially in his later life. In the 1960s he and his wife
Elizabeth (``Betty''), made at least one trip from their home in Maine
to visit their cousins in the Wallace, Gulf Shore, Fox Harbour areas
of northern Nova Scotia.  I have several letters that Dave had written
to my mother during those years. In later years, I corresponded with
him on all things relating to the David family. He was particularly
interested in any effort to learn more about the early family members
who came to North America from Europe. Several times Mary Lee and I
visited with Dave and Betty at their home in Maine. They were always
charming and welcoming – Betty interested to know about our son and
daughter, and Dave, his desk covered with notes, charts, and
printouts, eager to discuss any new discoveries on the family history
front. Dave died January 8th, 2004 at the age of eighty-eight and
Betty on July 10, 2014.

Dave and Betty Purcell had three children: David, Joanne and
Elizabeth. In early December, 2017, I was able to establish contact
with Joanne and her husband Bob Flynn. They live in
Vermont. Throughout the spring of 2018 Joanne and I got acquainted
through emails and phone chats. At one point I asked her if she had
some special memories of her father that she could share with me. This
is a part of her reply:

My father and I were very close; I have so many memories: spending
endless house at his elbow in his workshop watching him explore,
create, take apart, put together. Electrical . . . mechanical, didn't
matter, he could fix anything---all the while teaching, explaining. We
once even took apart a television set so he could describe how it
worked! I can still envision the large picture tube, something that
doesn't even exist anymore. I remember his infectious laugh, his
wonderful sense of humour . . . and his pranks . . . he loved to play
harmless pranks. One of the best was the Halloween he put a vacuum
cleaner under our porch so that when the Trick or Treaters stepped up
to the door, the machine would turn on. Only a few ran away!

Dave Campbell Purcell, second son of Margaret and David Purcell,
always maintained a strong interest in our David family
history---especially in his later life. In the 1960s he and his wife
Elizabeth (``Betty''), made at least one trip from their home in Maine
to visit their cousins in the Wallace, Gulf Shore, Fox Harbour areas
of northern Nova Scotia.  I have several letters that Dave had written
to my mother during those years. In later years, I corresponded with
him on all things relating to the David family. He was particularly
interested in any effort to learn more about the early family members
who came to North America from Europe. Several times Mary Lee and I
visited with Dave and Betty at their home in Maine. They were always
charming and welcoming – Betty interested to know about our son and
daughter, and Dave, his desk covered with notes, charts, and
printouts, eager to discuss any new discoveries on the family history
front. Dave died January 8th, 2004 at the age of eighty-eight and
Betty on July 10, 2014.

Dave and Betty Purcell had three children: David, Joanne and
Elizabeth. In early December, 2017, I was able to establish contact
with Joanne and her husband Bob Flynn. They live in
Vermont. Throughout the spring of 2018 Joanne and I got acquainted
through emails and phone chats. At one point I asked her if she had
some special memories of her father that she could share with me. This
is a part of her reply:

My father and I were very close; I have so many memories: spending
endless house at his elbow in his workshop watching him explore,
create, take apart, put together. Electrical . . . mechanical, didn't
matter, he could fix anything---all the while teaching, explaining. We
once even took apart a television set so he could describe how it
worked! I can still envision the large picture tube, something that
doesn't even exist anymore. I remember his infectious laugh, his
wonderful sense of humour . . . and his pranks . . . he loved to play
harmless pranks. One of the best was the Halloween he put a vacuum
cleaner under our porch so that when the Trick or Treaters stepped up
to the door, the machine would turn on. Only a few ran away!

\end{document}

或者,如果你只想将图像剪切到段落中,请使用 wrapfig标题,并进行类似的空间调整。


原始答案

您没有提供任何示例,顶部的空间不是预期的,我无法重现。无论如何,我在这里进行了更正,导致图像突出于文本之上。

大部分底部的空间是由 强制的,\\它永远不应该在段落末尾使用,并导致 TeX 以 badness 10000 警告未满的框(即强制空行)在这种情况下,即使没有 ,\\如果需要紧密配合,也可以使插入少一行。

在此处输入图片描述

\documentclass{article}

\usepackage{wrapfig}
\usepackage{graphicx}

\begin{document}

\begin{wrapfigure}{R}{2in}
% I would not expect any space at the top  but
\vspace{-\baselineskip}
\includegraphics[width=2in]{example-image-1x1.png}%
% never use \\ at end of paragraph\\
% TeX will give badness 10000 warning
\vspace{-\baselineskip}
\end{wrapfigure}
Zzzz zzz zzz zzz zzz zzz zzz zzz zzz zzz.
Zabc abc abc abc abc abc abc abc abc abc.
Zzzz zzz zzz zzz zzz zzz zzz zzz zzz zzz.
Zabc abc abc abc abc abc abc abc abc abc.
Zzzz zzz zzz zzz zzz zzz zzz zzz zzz zzz.
Zabc abc abc abc abc abc abc abc abc abc.
Zzzz zzz zzz zzz zzz zzz zzz zzz zzz zzz.
Zabc abc abc abc abc abc abc abc abc abc.
Zzzz zzz zzz zzz zzz zzz zzz zzz zzz zzz.
Zabc abc abc abc abc abc abc abc abc abc.
Zzzz zzz zzz zzz zzz zzz zzz zzz zzz zzz.
Zabc abc abc abc abc abc abc abc abc abc.
Zzzz zzz zzz zzz zzz zzz zzz zzz zzz zzz.
Zabc abc abc abc abc abc abc abc abc abc.
Zzzz zzz zzz zzz zzz zzz zzz zzz zzz zzz.
Zabc abc abc abc abc abc abc abc abc abc.
Zzzz zzz zzz zzz zzz zzz zzz zzz zzz zzz.
Zabc abc abc abc abc abc abc abc abc abc.
Zzzz zzz zzz zzz zzz zzz zzz zzz zzz zzz.
Zabc abc abc abc abc abc abc abc abc abc.
Zzzz zzz zzz zzz zzz zzz zzz zzz zzz zzz.
Zabc abc abc abc abc abc abc abc abc abc.
Zzzz zzz zzz zzz zzz zzz zzz zzz zzz zzz.
Zabc abc abc abc abc abc abc abc abc abc.
\end{document}

相关内容