2

I want to create a wrapper environment for wrapfigure in order to locally set \initextsep to 0. I know, it could be done differently, but this code is exhibiting strange behaviour. It moves the figure to a place after the paragraph it is defined in.

\documentclass{article}
\usepackage{lipsum}
\usepackage{wrapfig}
\begin{document}
\lipsum[1]
\newenvironment{mywrapfigure}[2]{
   \begin{wrapfigure}{#1}{#2}
   }
   {
   \end{wrapfigure}
}

\begin{mywrapfigure}{r}{.3\textwidth}
   \rule{5cm}{5cm}
\end{mywrapfigure}
\lipsum[1]
\end{document}

Note that all I am doing is creating a dummy environment around wrapfigure. enter image description here

How do I fix this?

  • 1
    Try \wrapfigure and \endwrapfigure instead of \begin{wrapfigure} and \end{wrapfigure} wrapfigure is no real environment in the sense of \newenvironment. – user31729 Sep 8 '15 at 9:56
  • Other question: Why do you want a wrapper? You're losing the important 1st and 3rd optional arguments here, which may be used for fine-control – user31729 Sep 8 '15 at 10:01
  • @ChristianHupfer to set \initexsep locally. I also find it annoying that that setting is not automatically zero inside wrapfigure – daleif Sep 8 '15 at 10:16
  • @daleif: \initextsep is completely unknown to me. Is it a TeX primitive? Do you mean \intextsep? – user31729 Sep 8 '15 at 10:19
  • @ChristianHupfer i don't remember. There is a description of what it's used for in the memoir manual (Peter Wilson explained most of the mechanics behind floats). It controls (AFAIK) the distance from the top of the wrapfigure cavity to the top if the inserted material. Often you want that to be zero. But is has to be set before the env is used. I haven't tried, but perhaps one could make an interface keeping wrapper using xparse – daleif Sep 8 '15 at 10:24
5

Perhaps this is the wrapper wanted: I added an optional 5th argument holding the \intexsep value, defaulting here to 0pt.

The wrapfig environment isn't a real environment defined with \newenvironment. In this case, it's better to use the fake environment starter and end commands \wrapfigure and \endwrapfigure.

\documentclass{article}
\usepackage{blindtext}
\usepackage{wrapfig}

\usepackage{xparse}


\NewDocumentEnvironment{mywrapfigure}{O{}mO{\wrapoverhang}mO{0pt}}{%
  \setlength{\intextsep}{#5}%  Defaults to 0pt \plus 0pt \minus 0pt
  \wrapfigure[#1]{#2}[#3]{#4}%
}{%
  \endwrapfigure%
}


\begin{document}

\begin{mywrapfigure}[8]{r}[20pt]{.4\textwidth}
  \rule{5cm}{2cm}
\end{mywrapfigure}
\blindtext[1]

\hrule
\vskip\baselineskip
\noindent Traditional: 
\begin{wrapfigure}[8]{r}[20pt]{.4\textwidth}
  \rule{5cm}{2cm}
\end{wrapfigure}
\blindtext[1]

\end{document}

enter image description here

  • The default value for the first optional argument is empty; for the second one it is \wrapoverhang. – egreg Sep 8 '15 at 10:55
  • @egreg: Right, I just used the values in the example on the manual starter page ;-) – user31729 Sep 8 '15 at 10:57
  • I had to read the package code for finding them, they aren't specified anywhere, AFAICT. – egreg Sep 8 '15 at 10:58
  • 1
    @daleif: Thanks for information, I'll take a look. Perhaps I find an explanation, but I have to test first – user31729 Nov 11 '15 at 17:25
  • 1
    @daleif: Apparently, thanks. (And sorry for answering to your comment after some days only. I apologize!) – user31729 Nov 17 '15 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.