[Tex/LaTex] How to avoid underfull \hbox when using subfigures

boxessubcaptionwarnings

When compiling the MWE below with pdflatex, I get the warnings Underfull \hbox (badness 10000) in paragraph at lines 26--27 and Underfull \hbox (badness 10000) in paragraph at lines 36--37.

I tried to avoid this by using \hfill between the subfigures, but it didn't work.

\documentclass{scrbook}
\usepackage[utf8]{inputenx}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{epstopdf}
\usepackage{subcaption}
\usepackage{gnuplottex}
\usepackage{xparse}
\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\convertlen}{ O{cm} m }
 {
  \dim_to_unit:nn { #2 } { 1 #1 } cm
 }
\ExplSyntaxOff

\begin{document}
\begin{figure}[htbp]
 \centering
 \begin{subfigure}[b]{.45\textwidth}
  \begin{gnuplot}[terminal = cairolatex, terminaloptions = {size \convertlen{\textwidth},\convertlen{.3\textheight}}]
   plot sin(x)
  \end{gnuplot}
  \caption{A subfigure}
  \label{fig:1a}
 \end{subfigure}%
 \hfill
 \begin{subfigure}[b]{.45\textwidth}
  \begin{gnuplot}[terminal = cairolatex, terminaloptions = {size \convertlen{\textwidth},\convertlen{.3\textheight}}]
   plot sin(x)
  \end{gnuplot}
  \caption{Another subfigure}
  \label{fig:1b}
 \end{subfigure}
 \caption{A figure}\label{fig:1}
\end{figure}
\end{document}

Best Answer

If you add

\showboxdepth3
\showboxbreadth10
\tracingonline1

to see which box is underfull you see

\hbox(178.66272+0.0)x188.21371
.\hbox(0.0+0.0)x0.0
.\hbox(178.66272+0.0)x187.69623
..\special{ps: currentpoint currentpoint translate 1 1 scale neg exch neg exch 
t\ETC.}
..\hbox(178.66272+0.0)x0.0, glue set - 187.69623fil
...\hbox(178.66272+0.0)x187.69623 []
...\glue 0.0 plus 1.0fil minus 1.0fil
..\special{ps: currentpoint currentpoint translate 1 1 div 1 1 div scale neg ex
c\ETC.}
.\glue(\rightskip) 0.0

So the line is trying to be 188.21371pt but the box with the plot is 187.69623pt the only glue on the line is \rightskip and that is zero, so the box is underful.

That's slightly odd as a one line paragraph usually has \parfillskip at the end but that has been removed here somewhere in the verbatim handling for gnuplot.

rather than debug exactly where \parfillskip went, a simpler fix is to make \rightskip non zero, and if you uncomment the %\centering the warning goes away.

(I updated the name of \dim_to_decimal_in_unit:nn)

\documentclass{scrbook}
\usepackage[utf8]{inputenx}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{epstopdf}
\usepackage{subcaption}
\usepackage{gnuplottex}
\usepackage{xparse}
\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\convertlen}{ O{cm} m }
 {
  \dim_to_decimal_in_unit:nn { #2 } { 1 #1 } cm
 }
\ExplSyntaxOff
%\showoutput
\showboxdepth3
\showboxbreadth10
\tracingonline1
\begin{document}
\begin{figure}[htbp]
 \centering
 \begin{subfigure}[b]{.45\textwidth}
%\centering
\begin{gnuplot}[terminal = cairolatex, terminaloptions = {size \convertlen{\textwidth},\convertlen{.3\textheight}}]
   plot sin(x)
  \end{gnuplot}
  \caption{A subfigure}
  \label{fig:1a}
 \end{subfigure}%
 \hfill
 \begin{subfigure}[b]{.45\textwidth}
\centering
  \begin{gnuplot}[terminal = cairolatex, terminaloptions = {size \convertlen{\textwidth},\convertlen{.3\textheight}}]
   plot sin(x)
  \end{gnuplot}
  \caption{Another subfigure}
  \label{fig:1b}
 \end{subfigure}
 \caption{A figure}\label{fig:1}
\end{figure}
\end{document}