42

Wrapfigure environment has excessive whitespace BELOW the caption. Inserting negative vspace just shifts the position of the caption relative to the image, actually increasing the 2+ lines of pointless space that usually sit below it. Any ideas?

3
  • 2
    Welcome to TeX.sx! Please add a minimal working example (MWE) that illustrates your problem.
    – egreg
    Oct 16, 2011 at 21:47
  • 4
    wrapfig has an optional argument specifying the no. of lines to reserve, e.g. \begin{wrapfigure}[11]{i}[0pt]{0.5\textwidth} -- where 11 is the no. of lines. I usually just play with this number until I get the desired spacing. Oct 16, 2011 at 22:48
  • 1
    floatfigure from the package floatfig has much better spacing defaults than wrapfigure, but it doesn't play well with sectioning commands (especially the inline ones, like \paragraph) Feb 1, 2017 at 8:55

5 Answers 5

39

As @prettygully says, the wrapfigure environment takes an optional 1st argument called number of narrow lines; from the documentation:

enter image description here

Compare the following (default):

enter image description here

\documentclass{article}

\usepackage{lipsum} % sample text
\usepackage{wrapfig}

\begin{document}

\begin{wrapfigure}{r}{3cm}
\centering
\rule{20pt}{10pt}
\caption{My caption}
\end{wrapfigure}
\lipsum[1]

\end{document}

To this, with number of narrow lines set as 4:

...
\begin{wrapfigure}[4]{r}{3cm}
...

enter image description here

This is a little fiddly- perhaps someone knows of a way to change this globally.

1
  • 5
    Does anyone know how to change it globally, e.g., always one line less than it thinks...
    – Andy
    Nov 4, 2014 at 13:57
3

I personnally use mostly floatingfigure from the floatflt package:

\documentclass{article}

\usepackage{lipsum} % sample text
\usepackage{floatflt}

\begin{document}
\begin{floatingfigure}{3cm}
\centering
\rule{20pt}{10pt}
\caption{My caption}
\end{floatingfigure}
\lipsum[1]

\end{document}

The spacing is much more pleasing than wrapfigure. However, it has drawbacks: it doesn't do well with sectioning commands, especially inline ones like \paragraph, and if a figure hits a page boudary, it's just lost. wrapfigure, on the other hand, handles both page breaks and sectioning commands much better.

1
  • It turns out this is not my preferred way to handle this anymore... Jul 26, 2021 at 7:21
3

An alternative to wrapfigure is provided by the (much older) picins package. It has a similar setup in terms of horizontal and vertical displacement. Here's a working minimal example:

enter image description here

\documentclass{article}

\usepackage{picins,lipsum}
% Use [demo] option to graphicx if you don't have tiger.pdf
\usepackage{graphicx}
\usepackage{caption}

\begin{document}

\pichskip{15pt}% Horizontal gap between picture and text
\parpic[r][t]{%
  \begin{minipage}{40mm}
    \includegraphics[width=\linewidth]{tiger}%
    \captionof{figure}{This is a tiger.}
  \end{minipage}
}
\lipsum[1]

\end{document}

The image (or any <content>) is placed using \parpic[<options>][<position>]{<content>} (you're also able to specify some horizontal/vertical width/height and offset; see the picins documentation). <options> are for the image location in the paragraph (left l or right r), while <position> refers to alignment within the \parpic (left l, top t, bottom b or right r). There are also some other elementary framing and shadow commands available.

Horizontal image placement is modified using \parhskip{<len>}, while vertical number-of-lines is altered by \parskip{<n>}. I've seen that the vertical height of the \parpic does not adhere to the \parskip command when used with the caption package's \captionof command. However, you can manually add content below the figure to increase the gap between the caption and the paragraph text below it, if needed. Or, use the height parameter in \parpic(<width>,<height>)....

Some additional context related to the use of the picins package:

  1. picins needs to be downloaded manually due it's current license. See Where do I place my own .sty or .cls files, to make them available to all my .tex files? for an option on handling/using manually-downloaded content.

  2. It seems to (at least in LuaTeX) prevent \linewidth from working within the shorter lines next to the picture. As a pseudo solution you can determine the space left over in your line with the linegoal package in the following way:

    % Before the loading in LuaTeX you need to enable the pdfTeX commands of savepos
    % If you do not use LuaTeX you should leave out these lines
    \let\pdfsavepos\savepos
    \let\pdflastxpos\lastxpos
    \let\pdflastypos\lastypos
    \usepackage{linegoal}
    
    % Now you declare your measuring command
    \newlength{\leftoverline}
    \newcommand*{\testleftoverwidth}{%
      \noindent\mbox{}%
      \setlength{\leftoverline}{\linegoal}}
    

    Afterwards you can now use \leftoverline as the width for a parbox/colorbox/pictures, etc.

1
  • 13
    It should be noted that picins is not in TeX Live (it has a nonfree license).
    – egreg
    Oct 17, 2011 at 15:06
2

Original (Rus) Опциональный аргумент (number of narrow lines), задающий количество линий под фигуру следует использовать только в случае, если Вам необходимо "подогнать" размеры конкретного изображения. В остальном проще всего настроить отступ после caption

Добавь в преамбулу строку:

\setlength{\belowcaptionskip}{-.8\baselineskip}

При необходимости размер отступа можно скорректировать.

Translate (en) The optional argument (number of narrow lines) specifying the number of lines for a shape should be used only if you need to "fit" the dimensions of a particular image. Otherwise, the easiest way to adjust the indent after caption

Add a line to the preamble:

\setlength{\belowcaptionskip}{-.8\baselineskip}

If necessary, the indent size can be adjusted.

1
  • where exactly should I position this command?
    – florian
    Mar 3 at 14:36
1

Since my other answer, I have switched back to using wrapfigure, because I ran into a couple of issues with floatflt that I couldn't resolve, with figures lost without warning. The trick is simply to add negative \vspace at the beginning and at the end of the wrapfigure environment. I find this strategy much nicer than to count the lines, and it also offers the possibility to use some space above the first line if desired.

\documentclass{article}

\usepackage{lipsum} % sample text
\usepackage{wrapfig}

\begin{document}

\begin{wrapfigure}{r}{3cm}
\centering
\rule{20pt}{10pt}
\caption{My caption}
\vspace{-3mm}
\end{wrapfigure}
\lipsum[1]

\end{document}

This way, you can adjust the spacing manually, it will adapt mostly fine if the vertical size of the figure changes, and it handles much better sectioning commands.

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.