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?

oarfish
  • 1,419
  • 1
    Try \wrapfigure and \endwrapfigure instead of \begin{wrapfigure} and \end{wrapfigure} wrapfigure is no real environment in the sense of \newenvironment. –  Sep 08 '15 at 09: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 –  Sep 08 '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 08 '15 at 10:16
  • @daleif: \initextsep is completely unknown to me. Is it a TeX primitive? Do you mean \intextsep? –  Sep 08 '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 08 '15 at 10:24

1 Answers1

6

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 08 '15 at 10:55
  • @egreg: Right, I just used the values in the example on the manual starter page ;-) –  Sep 08 '15 at 10:57
  • I had to read the package code for finding them, they aren't specified anywhere, AFAICT. – egreg Sep 08 '15 at 10:58
  • @egreg: I don't understand why the lower wrapfigure usage has a downshift. Regardless, which order mywrapfigure or wrapfigure are used, even two innocent wrapfigure will yield the second one shifted down –  Sep 08 '15 at 10:58
  • My impression is that \intextsep should be set at the outer level. – egreg Sep 08 '15 at 10:59
  • @egreg: The manual mentions \wrapoverhang on the 3rd page, but I found it after your first comment (didn't read that far in the manual) –  Sep 08 '15 at 11:00
  • @egreg: You mean \setlength{\intextsep} before mywrapfigure and restore it after \endmywrapfigure? –  Sep 08 '15 at 11:02
  • Yes, but this should be done outside the environment. Let me experiment some more. – egreg Sep 08 '15 at 11:03
  • @egreg: \xpretocmd{\mywrapfigure}{\begingroup\setlength{...}}{} etc? –  Sep 08 '15 at 11:04
  • When I leave out the second optional argument, I get an ! Undefined control sequence. \WF@ovh ->\wrapfigoverhang. Is that intended? Also, I am still seeing the useless top space in my document, maybe not in the mwe. Only thing that seems to help is setting \intextsep globally. – oarfish Sep 08 '15 at 20:25
  • @oarfish: Sorry, \wrapoverhang, not \wrapfigoverhang, that was my fault. For the other problem: The MWE works, but I never guaranteed it would do so as well in the full document. –  Sep 08 '15 at 20:28
  • @ChristianHupfer, there is an interesting bug here. Try switching to book and add a \chapter right before mywrapfigure, then the black box is not well aligned with the text. Any idea why? – daleif Nov 11 '15 at 14:55
  • 1
    @daleif: Thanks for information, I'll take a look. Perhaps I find an explanation, but I have to test first –  Nov 11 '15 at 17:25
  • @ChristianHupfer I've added it to the site as a question of its own: http://tex.stackexchange.com/q/278091/3929 – daleif Nov 13 '15 at 15:16
  • @ChristianHupfer, see Davids answer to my question, we did not read the manual carefully enough, it cannot be done like this because of item 2 on the first page of the manual. – daleif Nov 13 '15 at 16:08
  • 1
    @daleif: Apparently, thanks. (And sorry for answering to your comment after some days only. I apologize!) –  Nov 17 '15 at 21:09