清除 LaTeX 中的浮动

清除 LaTeX 中的浮动

我正在使用回忆录主题制作文档。我希望在我的文档中图片有时浮动到左侧或右侧。

我的工作做得相当好:

\documentclass[twoside]{memoir}
\usepackage[papersize={8.5in,11in}, vmargin=0.5in, outer=1in, inner=0.5in, includehead, includefoot]{geometry}
\usepackage{color}
\usepackage{fontspec}
\usepackage{newunicodechar}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage[none]{hyphenat}
\usepackage{graphicx}
\usepackage{wrapfig}
\graphicspath{ {images/} }

\makeatletter

\newcommand\floatingimage[4]{
  \begin{wrapfigure}{#3}{0.25\textwidth}%
    \vspace{-20pt}%
    \begin{center}%
      \includegraphics[width=0.25\textwidth]{#1}%
    \end{center}%
    \vspace{-15pt}%
    \legend{#4}%
    \vspace{-15pt}%
  \end{wrapfigure}%
}

% Remove 'Figure 1' text
\patchcmd{\legend}%Cmd
    {\normalsize}%Search
    {\small\bfseries\raggedright}%Replace
    {}%Success
    {}%Fail

% Custom section heading
\newcommand{\beginsection}[1]{%
  \let\@section@title@\relax
  \normalfont\large\bfseries #1
  \normalfont\normalsize
  \par
  \vspace{0.5pt} % Required for vertical alignment, not quite sure why it's necessary

  \gdef\@section@title@{#1}
}

\makeatother

\setlength{\parindent}{0pt}%
\nonzeroparskip

\begin{document}
\chapterstyle{scp}
\chapter{Example}
\floatingimage{800placeholder}{800}{R}{Figure A}
\beginsection{Section 1}
\lipsum[1-1]

\beginsection{Section 2}
\lipsum[2-2]

\floatingimage{150placeholder}{150}{L}{Figure B}
\lipsum[3-3]

\beginsection{Section 3}
\lipsum[4-4]

\end{document}

截屏

唯一的问题是,我不喜欢“第 3 节”标题向内推的方式。我希望标题出现在浮动元素之后,即行首。理想情况下,我希望此行为仅在浮动位于左侧时适用,因为右侧的浮动不会推动标题。

在 CSS 中我可以使用 来实现这一点h3 { clear: left; }。LaTeX 的对应内容是什么?

我搜索了一下,在placeins包中找到了 \FloatBarrier,但这根本没有改变输出。下面是我尝试使用它的方法。

\documentclass[twoside]{memoir}
\usepackage[papersize={8.5in,11in}, vmargin=0.5in, outer=1in, inner=0.5in, includehead, includefoot]{geometry}
\usepackage{color}
\usepackage{fontspec}
\usepackage{newunicodechar}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage[none]{hyphenat}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{placeins}
\graphicspath{ {images/} }

\makeatletter

\newcommand\floatingimage[4]{
  \begin{wrapfigure}{#3}{0.25\textwidth}%
    \vspace{-20pt}%
    \begin{center}%
      \includegraphics[width=0.25\textwidth]{#1}%
    \end{center}%
    \vspace{-15pt}%
    \legend{#4}%
    \vspace{-15pt}%
  \end{wrapfigure}%
}

% Remove 'Figure 1' text
\patchcmd{\legend}%Cmd
    {\normalsize}%Search
    {\small\bfseries\raggedright}%Replace
    {}%Success
    {}%Fail

% Custom section heading
\newcommand{\beginsection}[1]{%
  \let\@section@title@\relax
  \FloatBarrier
  \normalfont\large\bfseries #1
  \normalfont\normalsize
  \par
  \vspace{0.5pt} % Required for vertical alignment, not quite sure why it's necessary

  \gdef\@section@title@{#1}
}

\makeatother

\setlength{\parindent}{0pt}%
\nonzeroparskip

\begin{document}
\chapterstyle{scp}
\chapter{Example}
\floatingimage{800placeholder}{800}{R}{Figure A}
\beginsection{Section 1}
\lipsum[1-1]

\beginsection{Section 2}
\lipsum[2-2]

\floatingimage{150placeholder}{150}{L}{Figure B}
\lipsum[3-3]

\beginsection{Section 3}
\lipsum[4-4]
\end{document}

(不会附加屏幕截图,因为它看起来与上面的完全相同)

我怎样才能达到我想要的效果?

编辑:我发现了这个相关问题但我无法让他们的解决方案为我工作。

答案1

只要小心一点,您就可以清除 wrapfig 的设置,这样后面的文本就不会缩进,尽管您需要添加额外的空间来补偿。(如果您经常这样做,您可以自动测量图像和段落的大小,但对于手写文档来说并不值得,只适用于自动化工作流程)

请注意,wrapfig 即使使用L而不是也不l是真正的乳胶浮点数,因此问题标题有点误导。我使用的\section自定义定义似乎错过了部分标题的所有重要代码(例如控制标题后的分页符)

还添加了一些缺失%和使用的内容\centering(不添加垂直空间),而不是center负空间调整。

在此处输入图片描述

\documentclass[twoside]{memoir}
\usepackage[papersize={8.5in,11in}, vmargin=0.5in, outer=1in, inner=0.5in, includehead, includefoot]{geometry}
\usepackage{color}
\usepackage{fontspec}
\usepackage{newunicodechar}
\usepackage[nopar]{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage[none]{hyphenat}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{placeins}
\graphicspath{ {images/} }

\makeatletter

\newcommand\floatingimage[4]{%
  \begin{wrapfigure}{#3}{0.25\textwidth}%
\centering
      \includegraphics[width=0.25\textwidth]{#1}%

    \legend{#4}%
  \end{wrapfigure}%
}

% Remove 'Figure 1' text


\makeatother

\setlength{\parindent}{0pt}%
\nonzeroparskip

\begin{document}
\chapterstyle{scp}
\chapter{Example}
\floatingimage{example-image-a}{800}{R}{example-image-a}
\section{Section 1}
\lipsum[1-1]

\section{Section 2}
\lipsum[2-2]

\floatingimage{example-image-b}{150}{l}{example-image-b}
\lipsum[3-3]

\makeatletter
\WF@@everypar{}
\vspace{\baselineskip}
\makeatother

\section{Section 3}
\lipsum[4-4]
\end{document}

答案2

这更像是一个评论而不是一个答案,因为它只是一个微小的改进。

本质上,我将左侧图像视为列表中的项目符号itemize。这可确保在下一个段落之前清除图像。因此,比图像短的段落后面会有一个较大的段落。软件包adjustboxenumitem将使图像正确对齐,并使右侧段落与文档边距对齐。

\documentclass[twoside]{memoir}
\usepackage[papersize={8.5in,11in}, vmargin=0.5in, outer=1in, inner=0.5in, includehead, includefoot]{geometry}
\usepackage{color}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage[none]{hyphenat}
\usepackage{graphicx}
\usepackage{wrapfig}
\graphicspath{ {images/} }

\makeatletter

\newcommand\floatingimage[4]{
  \begin{wrapfigure}{#3}{0.25\textwidth}%
    \vspace{-20pt}%
    \begin{center}%
      \includegraphics[width=0.25\textwidth]{#1}%
    \end{center}%
    \vspace{-15pt}%
    \legend{#4}%
    \vspace{-15pt}%
  \end{wrapfigure}%
}

% Remove 'Figure 1' text
\patchcmd{\legend}%Cmd
    {\normalsize}%Search
    {\small\bfseries\raggedright}%Replace
    {}%Success
    {}%Fail

% Custom section heading
\newcommand{\beginsection}[1]{%
  \let\@section@title@\relax
  \normalfont\large\bfseries #1
  \normalfont\normalsize
  \par
  \vspace{0.5pt} % Required for vertical alignment, not quite sure why it's necessary

  \gdef\@section@title@{#1}
}

\makeatother

\setlength{\parindent}{0pt}%
\nonzeroparskip
\usepackage{adjustbox}
\usepackage{enumitem}

\newcommand{\leftimage}[3]{
\begin{itemize}[labelwidth=0.25\textwidth, labelindent=0pt, leftmargin=!, itemindent=0.05\textwidth, labelsep*=0.025\textwidth]
    \item[{\adjustbox{valign=T}{{\includegraphics[width=0.25\textwidth]{#1}}}}] \begin{minipage}[t]{0.675\textwidth} \relax #2 \end{minipage}
\end{itemize}
}

\begin{document}
\chapterstyle{scp}
\chapter{Example}
\floatingimage{Example-image}{800}{R}{Figure A}
\beginsection{Section 1}
\lipsum[1-1]

\beginsection{Section 2}
\lipsum[2-2]

\leftimage{Example-image}{\lipsum[4-4]}

\lipsum[4-4]

\beginsection{Section 3}
\lipsum[4-4]

\end{document}

答案3

这是我最终使用的方法,基于 David Carlisle 的回答。我所做的更改是让它自动应用于我使用 \beginsection 和 \floatingimage 的任何地方。(但前提是图像位于左侧)

\documentclass[twoside]{memoir}
\usepackage[papersize={8.5in,11in}, vmargin=0.5in, outer=1in, inner=0.5in, includehead, includefoot]{geometry}
\usepackage{color}
\usepackage{fontspec}
\usepackage{newunicodechar}
\usepackage[nopar]{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage[none]{hyphenat}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{placeins}
\graphicspath{ {images/} }

\makeatletter

\let\shouldwrap\relax

\providetoggle{floattoclear}

\newcommand\floatingimage[4]{%
  \ifnum `l=`#3
    \settoggle{floattoclear}{true}%
  \else
    \settoggle{floattoclear}{false}%
  \fi
  \begin{wrapfigure}{#3}{0.25\textwidth}%
    \centering
    \includegraphics[width=0.25\textwidth]{#1}%
    \vspace{-5pt}%
    \legend{#4}%
  \end{wrapfigure}%
}

\newcommand\beginsection[1]{%
    \let\@section@title@\relax
    \iftoggle{floattoclear}{
        \WF@@everypar{}%
        \vspace{\numexpr6-\c@WF@wrappedlines\relax\baselineskip}%
        \settoggle{floattoclear}{false}
    }
    \normalfont\secheadstyle #1
    \normalfont\normalsize
    \par
    \vspace{0.5pt} % Required for vertical alignment, not quite sure why it's necessary

    \gdef\@section@title@{#1}
}

\makeatother

\setlength{\parindent}{0pt}%
\nonzeroparskip

\begin{document}
\chapterstyle{scp}
\chapter{Example}
\floatingimage{example-image-a}{800}{r}{example-image-a}
\beginsection{Section 1}
\lipsum[1-1]

\beginsection{Section 2}
\lipsum[2-2]

\floatingimage{example-image-b}{150}{l}{example-image-b}
\lipsum[3-3]

\beginsection{Section 3}
\lipsum[4-4]
\end{document}

相关内容