3
\documentclass{article}

\usepackage[table]{xcolor}
\definecolor{background}{rgb}{1,0.62502,0}

\usepackage{caption}
\usepackage{lipsum}
\usepackage{hyperref}
\hypersetup{
  colorlinks   = true,
  urlcolor     = blue,
  linkcolor    = blue,   
  citecolor    = red    
  }
 \usepackage{wrapfig}

\begin{document}

\lipsum[1]
\begin{wrapfigure}{l}{6.5cm}
\noindent
\setlength{\fboxsep}{0pt}
\fcolorbox{background}{background}{
\begin{minipage}[t]{0.5\textwidth} 
\lipsum[4]
%\fcolorbox{frame color}{box background color}{text}
\end{minipage}}
\end{wrapfigure}
\lipsum[1]

\end{document}

enter image description here

The vertical misalignment between the text in the minipage and the text around it is very slight, but noticeable (see esp the bottom of the snip and how the bottom of the p is cut off in the minipage but largely visible in the adjacent line in the wrapped text). When I remove \usepackage{caption} from the above code, the text in the minipage becomes aligned with the wrapping text:

enter image description here

How can I stop the caption package from producing this vertical misalignment?

arara
  • 776
  • If you use a bottom aligned minipage ([b]) or center [c] aligned, then with and without the caption package produce the same result. – Steven B. Segletes Apr 16 '22 at 16:53
  • 1
    I tried changing to [b] and [c] and the quite slight vertical misalignment remained. – arara Apr 16 '22 at 17:06
  • The package caption hasn't anything with space around your wrap figure. If you omit minipage position option [t] your wrapfigure is nice centered. If you like to have less vertical space around it, you need manually set its height, for example: \begin{wrapfigure}[14]{l}{6.5cm} \vskip-0.5\baselineskip˙ – Zarko Apr 16 '22 at 17:10
  • 1
    I cannot reproduce your behavior with your provided MWE, when the [b] or [c] option of minipage is used. – Steven B. Segletes Apr 16 '22 at 17:12

1 Answers1

2

This is an interaction between the caption package and hyperref. You can discover this by using \showoutput and examining the .log file, where you will see the following material is inserted

........\glue -6.02773
........\rule(0.0+0.0)x*
........\penalty 10000
........\glue -6.0
........\glue 0.0
........\pdfdest name{figure.caption.1} xyz
........\penalty 10000
........\rule(0.0+0.0)x*
........\penalty 10000
........\glue 6.0
........\glue 0.0
........\glue(\parskip) 0.0
........\glue(\parskip) 0.0

This looks like the glues ought to cancel out, but apparently they do not. Setting the option hypcap=false for the caption package removes the extra vertical space. The hyperref anchor, then gets placed at any caption you add instead of the beginning of the environment.

Sample output

\documentclass{article}

\usepackage[table]{xcolor} \definecolor{background}{rgb}{1,0.62502,0}

\usepackage{wrapfig} \usepackage[hypcap=false]{caption} \usepackage{lipsum} \usepackage{hyperref} \hypersetup{ colorlinks = true, urlcolor = blue, linkcolor = blue, citecolor = red, }

\begin{document}

\lipsum[1] \begin{wrapfigure}{l}{6.5cm} \setlength{\fboxsep}{0pt}% \fcolorbox{background}{background}{% \begin{minipage}[t]{0.5\textwidth} \lipsum[4] \end{minipage}}% \end{wrapfigure} \lipsum[1]

\end{document}

Incidentally, I have commented out (using %) a couple of line endings in your set-up, that introdced some horizontal spacing, that may or may not have been desired.

Andrew Swann
  • 95,762
  • Thank you very much! I also see you added a % to remove horizontal spacing, which I was also curious about how that got into my environment. Can you explain why the % is needed? I do not see anything that comes after the % and so I do not understand how it is removing horizontal spacing. – arara Apr 20 '22 at 18:19
  • 1
    Glad this helped. Ends of lines in many circumstances count as a space. There is some explanation and references in the answers to https://tex.stackexchange.com/q/7453/15925 – Andrew Swann Apr 21 '22 at 15:59