8

It seems that math in captions gets stretched out as illustrated in Figures 2, 3 and 4:

enter image description here

The problem shows up with pdflatex on the second run.

The solution given in Lock spacing in math mode of adding an additional curly brace ${y = mx + b}$ works but wondering if there is a better solution for captions.

A bit of stretch in order to justify text would be ok, but in this case that does not seem to be the issue and the stretching seems quite large.

References:

Code:

\documentclass{article}
\usepackage{showframe}
\usepackage{graphicx}

\usepackage{caption}

\DeclareCaptionStyle{mystyle}
  {format=plain,%
    font=footnotesize,
    textformat=period,
    justification=RaggedRight,
    singlelinecheck=true,
  }% all captions are left aligned

\DeclareCaptionStyle{singlelinecentered}
  [justification=Centering]% centered if single line and no `singlelinecheck=false`
  {style=mystyle}% other captions are left aligned

\DeclareCaptionStyle{singlelineraggedleft}
  [justification=RaggedLeft]% right aligned if single line and no `singlelinecheck=false`
  {style=mystyle}% other captions are left aligned

\captionsetup{style=singlelineraggedleft}

\begin{document}
\noindent
\begin{minipage}{0.5\linewidth}
    \centering
    \includegraphics[width=0.95\linewidth, height=2cm]{example-image}%
    \captionsetup{width=0.95\linewidth}%
    \captionsetup{singlelinecheck=false}%
    \captionof{figure}{$y=mx+b$ on Left}%
\end{minipage}%
\begin{minipage}{0.5\linewidth}
    \centering
    \includegraphics[width=0.95\linewidth, height=2cm]{example-image}%
    \captionsetup{width=0.95\linewidth}%
    \captionof{figure}{$y=mx+b$ on Right}%
\end{minipage}%

\medskip\noindent
\begin{minipage}[t]{0.5\linewidth}
    \centering
    \includegraphics[width=0.95\linewidth, height=2cm]{example-image}%
    \captionsetup{width=0.95\linewidth,style=singlelinecentered}%
    \captionof{figure}{$y=mx+b$}%
\end{minipage}%
\begin{minipage}[t]{0.5\linewidth}
    \centering
    \includegraphics[width=0.95\linewidth, height=2cm]{example-image}%
    \captionsetup{width=0.95\linewidth}%
    \captionof{figure}{$y=mx+b$}%
\end{minipage}%
\end{document}
  • When I compile your MWE (just like you posted it) using pdflatex I don't get any stretching. Both xelatex and lualatex result in the stretching you showed. – Skillmon Apr 10 '18 at 6:56
  • @Skillmon: Did you try running pdflatex twice. It seems to be ok on the first run, but not the second. BTW, .I am using pdflatex with TeXLive 2017. – Peter Grill Apr 10 '18 at 7:16
  • That's the issue. Second run is stretched. – Skillmon Apr 10 '18 at 7:17
9

I see nothing strange and it hasn't to do with math, but rather with how ragged2e does its work.

  1. In a \RaggedRight, \RaggedLeft or \Centering context, the \spaceskip parameter is set to \spaceskip\fontdimen2\font, that is, the normal interword space for the current font, but with no stretch and shrink.

  2. In a \RaggedRight context, the values of \rightskip is set to \RaggedRightRightskip (default 0pt plus 2em); similar settings are done for \RaggedLeft and \Centering.

  3. A caption shorter than \linewidth-2em (for \RaggedRight) will need to be stretched.

  4. The only stretchability is present in the spaces around binary operation and relation symbols.

  5. Hey! Just math is stretched.

Solution: don't use ragged2e for this.

Alternative solution: remove the stretchability and shrinkability of \medmuskip and \thickmuskip:

\documentclass{article}
\usepackage{showframe}
\usepackage{graphicx}

\usepackage{ragged2e}
\usepackage{caption}
\usepackage{etoolbox}

\csappto{@raggedtwoe@everyselectfont}{%
  \medmuskip=1\medmuskip
  \thickmuskip=1\thickmuskip
}

\DeclareCaptionStyle{mystyle}{
  format=plain,
  font=footnotesize,
  textformat=period,
  justification=RaggedRight,
  singlelinecheck=true,
  }% all captions are left aligned

\DeclareCaptionStyle{singlelinecentered}
  [justification=Centering]% centered if single line and no `singlelinecheck=false`
  {style=mystyle}% other captions are left aligned

\DeclareCaptionStyle{singlelineraggedleft}
  [justification=RaggedLeft]% right aligned if single line and no `singlelinecheck=false`
  {style=mystyle}% other captions are left aligned

\captionsetup{style=singlelineraggedleft}

\begin{document}
\noindent
\begin{minipage}{0.5\linewidth}
    \captionsetup{width=0.95\linewidth}%
    \captionsetup{singlelinecheck=false}%
    \centering
    \includegraphics[width=0.95\linewidth, height=2cm]{example-image}
    \captionof{figure}{$y=mx+b$ on Left}
\end{minipage}%
\begin{minipage}{0.5\linewidth}
    \captionsetup{width=0.95\linewidth}%
    \centering
    \includegraphics[width=0.95\linewidth, height=2cm]{example-image}
    \captionof{figure}{$y=mx+b$ on Right}
\end{minipage}

\medskip\noindent
\begin{minipage}[t]{0.5\linewidth}
    \captionsetup{width=0.95\linewidth,style=singlelinecentered}%
    \centering
    \includegraphics[width=0.95\linewidth, height=2cm]{example-image}%
    \captionof{figure}{$y=mx+b$}%
\end{minipage}%
\begin{minipage}[t]{0.5\linewidth}
    \captionsetup{width=0.95\linewidth}
    \centering
    \includegraphics[width=0.95\linewidth, height=2cm]{example-image}
    \captionof{figure}{$y=mx+b$}
\end{minipage}

\end{document}

But you'll get “Underfull \hbox” messages for every short caption.

enter image description here

  • Thanks. Using raggedright instead of RaggedRight seems to work. Why is the spacing with RaggedRight ok after the first run? – Peter Grill Apr 10 '18 at 9:03
  • @PeterGrill It's a caption feature: it wants to be sure that ragged2e is loaded, so it makes a note in the .aux file and, when it doesn't find it (first run), it uses \raggedright and similar from the kernel. – egreg Apr 10 '18 at 9:07

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.