How to eliminate excessive vertical space in wrapfigure?

Compiling the following document using xelatex results in unwanted vertical space above the wrapfigure caption. Removing either the inclusion of the polyglossia package or \centercaption usage eliminates the problem. However, this is not a viable solution.

\documentclass{article}
\usepackage{polyglossia}
\usepackage{wrapfig}
\usepackage{caption}
\usepackage{lipsum}

\newcommand\centercaption{%
\captionsetup{format=plain,labelsep=newline,justification=centering}}

\begin{document}

\begin{wrapfigure}{r}{2cm}
\rule{2cm}{2cm}
\centercaption
\caption{test}
\end{wrapfigure}

\lipsum[1]

\end{document}


Above document produces

Without polyglossia the spacing is correct:

Is there any other way to eliminate the excessive vertical space above the caption? Note that setting the number of lines to be consumed by wrapfigure doesn't have any effect.

-

I'm not sure why polyglossia adds extra space between the graphic and caption, maybe a bug. You can just use \centercaption at the beginning of the wrapfigure environment to prevent the extra space it produces.

BTW, wrapfig package's wrapfigure environment has several optional parameters. The first one is the number of lines it vertically occupies. For your example:

\begin{wrapfigure}[8]{r}{2cm}
\centercaption
\rule{2cm}{2cm}
\caption{test}
\end{wrapfigure}


See the manual of wrapfig for more options and decriptions.

-
Using \centercaption at the beginning of the wrapfigure works indeed. Though the solution is not ideal, since it requires to update all \centercaption usages in a document which was previously compiled using pdflatex. – caustic May 10 '11 at 18:05

The extra vertical space that you see is because of the redefinition of \@esphack macro in package fixltx2e (so obviously user Leo Liu is wrong in saying that this is a polyglossia bug, polyglossia loads fontspec and fontspec loads fixltx2e). The original definition of \@esphack in latex.ltx is:

\def\@esphack{%
\relax
\ifhmode
\spacefactor\@savsf
\ifdim\@savsk>\z@
\ignorespaces
\fi
\fi}


And the redefinition of \@esphack in fixltx2e is:

\def\@esphack{%
\relax
\ifhmode
\spacefactor\@savsf
\ifdim\@savsk>\z@
\nobreak \hskip\z@skip  % <------
\ignorespaces
\fi
\fi}


So as you can see clearly \nobreak \hskip\z@skip causes that extra vertical space. The fixltx2e package documentation states that this was suggested by Donald Arseneau to fix the bug "No hyphenation in first word after float environment" (page 18).

Update: The actual bug report can be found here.

-