我怎样才能将环绕图放在页面顶部而不是段落开头

我怎样才能将环绕图放在页面顶部而不是段落开头

我怎样才能将图像放在页面顶部和段落之间?

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

\begin{document}

\lipsum[1-6]

\begin{wrapfigure}{L}{0.25\textwidth}
\includegraphics[width=0.25\textwidth]{x.jpg}
\end{wrapfigure}

\lipsum[1]

\end{document}

在此处输入图片描述

答案1

为了说明我的评论:

您可能想要将定义移动到另一个文件和\input

\documentclass[a4paper]{article}
\usepackage[draft]{graphicx}
\usepackage{wrapfig}
\usepackage{environ}
\usepackage{xparse}
\usepackage{varwidth}
\usepackage{afterpage}

\usepackage{lipsum}% MWE only
\usepackage{showframe}

\makeatletter
\newsavebox{\wp@image}

\NewDocumentEnvironment{wrappage}{O{0} m O{0} m}{\par
% #1 (optional) is the number of lines until the end of the page
% #2 is r (right) or l (left)
% #3 (optional) is the number of lines for the image.
% #4 width (or 0pt to compute).
  \xdef\wp@page{#1}%
  \ifnum\wp@page=0\relax
    \dimen0=\dimexpr \pagegoal-\pagetotal-0.5\baselineskip\relax
    \xdef\wp@page{\the\numexpr \dimen0/\baselineskip}%
  \fi
  \xdef\wp@side{#2}%
  \xdef\wp@lines{#3}%
  \xdef\wp@width{#4}%
  \def\@captype{figure}%
  \wp@environ}% body here
{\endwp@environ
  \ifnum\wp@lines=0\relax
    \dimen0=\dimexpr \ht\wp@image+\dp\wp@image+\intextsep\relax
    \xdef\wp@lines{\the\numexpr \dimen0/\baselineskip}%
  \fi
  \ifdim \wp@page\baselineskip>\dimexpr\wp@lines\baselineskip+\intextsep\relax% put figure on this page
    \AddToHookNext{para/before}{\begin{wrapfigure}[\wp@lines]{\wp@side}{\wp@width}
      \box\wp@image
    \end{wrapfigure}}
  \else
    \if r\wp@side\relax
      \wp@right% put image on right side
    \else
      \if l\wp@side\relax
        \wp@left% put image on left side
      \else
        \errmessage{Bad wrappage argument: \textbf{\wp@side} must be \texbf{r} or \textbf{l}.}%
      \fi
    \fi
  \fi
}
    
\def\wp@right{% handle right side wrapfig
  \afterpage{\xdef\wp@extra{\the\dimexpr \wp@lines\baselineskip-\ht\AP@partial-\dp\AP@partial}%
    \noindent\hspace{\dimexpr \textwidth-\wp@width}%
    \raisebox{\dimexpr \ht\strutbox-\height}[\ht\strutbox][0pt]{\box\wp@image}%
    \vskip-\baselineskip
    \unvbox\AP@partial
    \ifdim\wp@extra>0pt\relax
      \AddToHookNext{para/begin}{\begin{wrapfigure}{r}{\wp@width}
        \vskip-\intextsep
        \rule{0pt}{\wp@extra}
        \vskip-\intextsep
      \end{wrapfigure}\vskip-\baselineskip\indent}%
    \fi}
  \xdef\wp@parshape{\the\numexpr \wp@page+\wp@lines+1}%
  \edef\wp@test{\the\textwidth}%
  \count1=\wp@page\relax
  \loop\ifnum\count1>0
    \advance\count1 by -1
    \xdef\wp@parshape{\wp@parshape\space 0pt \wp@test}%
  \repeat
  \edef\wp@test{\the\dimexpr \textwidth-\wp@width-\columnsep}%
  \count1=\wp@lines\relax
  \loop\ifnum\count1>0
    \advance\count1 by -1
    \xdef\wp@parshape{\wp@parshape\space 0pt \wp@test}%
  \repeat
  \xdef\wp@parshape{\wp@parshape\space 0pt \the\textwidth}%
  \AddToHookNext{para/before}{\parshape=\wp@parshape}%
}

\def\wp@left{% handle left side wrapfig
  \afterpage{\xdef\wp@extra{\the\dimexpr \wp@lines\baselineskip-\ht\AP@partial-\dp\AP@partial}%
    \noindent\raisebox{\dimexpr \ht\strutbox-\height}[0pt][0pt]{\box\wp@image}%
    \vskip-\baselineskip
    \unvbox\AP@partial
    \ifdim\wp@extra>0pt\relax
      \AddToHookNext{para/begin}{\begin{wrapfigure}{l}{\wp@width}
        \vskip-\intextsep
        \rule{0pt}{\wp@extra}
        \vskip-\intextsep
      \end{wrapfigure}\vskip-\baselineskip\indent}%
    \fi}
  \xdef\wp@parshape{\the\numexpr \wp@page+\wp@lines+1}%
  \edef\wp@test{\the\textwidth}%
  \count1=\wp@page\relax
  \loop\ifnum\count1>0
    \advance\count1 by -1
    \xdef\wp@parshape{\wp@parshape\space 0pt \wp@test}%
  \repeat
  \edef\wp@temp{\the\dimexpr \wp@width+\columnsep}%
  \edef\wp@test{\the\dimexpr \textwidth-\wp@temp}%
  \count1=\wp@lines\relax
  \loop\ifnum\count1>0
    \advance\count1 by -1
    \xdef\wp@parshape{\wp@parshape\space \wp@temp \wp@test}%
  \repeat
  \xdef\wp@parshape{\wp@parshape\space 0pt \the\textwidth}%
  \AddToHookNext{para/before}{\parshape=\wp@parshape}\par
}

\NewEnviron{wp@environ}{% put \BODY into \wp@image
  \ifdim\wp@width=0pt
    \savebox\wp@image{\varwidth{\textwidth}
      \renewcommand{\caption}[2][\empty]{\par}% dummy caption
      \BODY
    \endvarwidth}%
    \xdef\wp@width{\the\wd\wp@image}%
  \fi
  \global\setbox\wp@image=\hbox{\parbox{\wp@width}{\BODY}}%
}
\makeatother
  
\begin{document}
\lipsum[1-5]

\begin{wrappage}{l}{0.25\textwidth}
\includegraphics[width=\linewidth]{x.jpg}
\end{wrappage}

\lipsum[6-7]

\end{document}

答案2

您需要给 wrapfig 一些帮助;在分页符处插入图形。

在此处输入图片描述

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

\begin{document}

\lipsum[1-5]


% {6}
Suspendisse vel felis. Ut lorem lorem, interdum eu, tincidunt sit amet,{\parfillskip=0pt\par}

\begin{wrapfigure}{L}{0.25\textwidth}
\includegraphics[width=0.25\textwidth,height=8cm]{example-image}
\end{wrapfigure}
\noindent
laoreet vitae, arcu. Aenean faucibus pede eu ante. Praesent enim elit,
rutrum at, molestie non, nonummy vel, nisl. Ut lectus eros, malesuada
sit amet, fermentum eu, sodales cursus, magna. Donec eu purus. Quisque
vehicula, urna sed ultricies auctor, pede lorem egestas dui, et
convallis elit erat sed nulla. Donec luctus. Curabitur et nunc. Aliquam
dolor odio, commodo pretium, ultricies non, pharetra in, velit. Integer
arcu est, nonummy in, fermentum faucibus, egestas vel, odio.


\lipsum[1]

\end{document}

相关内容