2
  • A friend writes his master thesis and asked me for help.
  • He wants to use sub figures.
  • "No problem" I said...two hours later here we are :)
  • I thought that the subcaption package is the best suited for this task.
  • I tried to prepare a nice collection of examples and encountered a problem with the calculation of the width.
  • Problem: According to my (apparently wrong) thinking, I have a sum of 100 % but I get a line break.
  • I need to have less than 100 % in order to have all figures in one line.

enter image description here


\documentclass{article}

\usepackage[showframe]{geometry}

\usepackage{graphicx}

% Here: H option for float placement
\usepackage{float}

% caption and subcaption work together
\usepackage{caption}
\usepackage{subcaption}

\begin{document}

\section*{Two Figures}

\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.45\textwidth]{example-image-a}}%
\hspace{0.1\textwidth}
\subcaptionbox{Subcaption B}{\includegraphics[width=0.45\textwidth]{example-image-b}}%
\caption{Caption}
\end{figure}

\begin{equation}
45\,\% + 10\,\% + 45\,\% = 100\,\%
\end{equation}

\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.44\textwidth]{example-image-a}}%
\hspace{0.1\textwidth}
\subcaptionbox{Subcaption B}{\includegraphics[width=0.44\textwidth]{example-image-b}}%
\caption{Caption}
\end{figure}

\begin{equation}
44\,\% + 10\,\% + 44\,\% = 98\,\%
\end{equation}

\pagebreak
\section*{Three Figures}

\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.30\textwidth]{example-image-a}}%
\hspace{0.05\textwidth}
\subcaptionbox{Subcaption B}{\includegraphics[width=0.30\textwidth]{example-image-b}}%
\hspace{0.05\textwidth}
\subcaptionbox{Subcaption C}{\includegraphics[width=0.30\textwidth]{example-image-c}}%
\caption{Caption}
\end{figure}

\begin{equation}
30\,\% + 5\,\% + 30\,\% + 5\,\% + 30\,\% = 100\,\%
\end{equation}

\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.29\textwidth]{example-image-a}}%
\hspace{0.05\textwidth}
\subcaptionbox{Subcaption B}{\includegraphics[width=0.29\textwidth]{example-image-b}}%
\hspace{0.05\textwidth}
\subcaptionbox{Subcaption C}{\includegraphics[width=0.29\textwidth]{example-image-c}}%
\caption{Caption}
\end{figure}

\begin{equation}
29\,\% + 5\,\% + 29\,\% + 5\,\% + 29\,\% = 97\,\%
\end{equation}

\pagebreak
\section*{Four Figures}

\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.2125\textwidth]{example-image-a}}%
\hspace{0.05\textwidth}
\subcaptionbox{Subcaption B}{\includegraphics[width=0.2125\textwidth]{example-image-b}}%
\hspace{0.05\textwidth}
\subcaptionbox{Subcaption C}{\includegraphics[width=0.2125\textwidth]{example-image-c}}%
\hspace{0.05\textwidth}
\subcaptionbox{Subcaption C}{\includegraphics[width=0.2125\textwidth]{example-image}}%
\caption{Caption}
\end{figure}

\begin{equation}
21.25\,\% + 5\,\% + 21.25\,\% + 5\,\% + 21.25\,\% + 5\,\% +21.25\,\% = 100\,\%
\end{equation}


\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.20\textwidth]{example-image-a}}%
\hspace{0.05\textwidth}
\subcaptionbox{Subcaption B}{\includegraphics[width=0.20\textwidth]{example-image-b}}%
\hspace{0.05\textwidth}
\subcaptionbox{Subcaption C}{\includegraphics[width=0.20\textwidth]{example-image-c}}%
\hspace{0.05\textwidth}
\subcaptionbox{Subcaption C}{\includegraphics[width=0.20\textwidth]{example-image}}%
\caption{Caption}
\end{figure}

\begin{equation}
20\,\% + 5\,\% + 20\,\% + 5\,\% + 20\,\% + 5\,\% +20\,\% = 95\,\%
\end{equation}

\end{document}

Question

What/Which length are taken into account when calculating the total width?

Solution for the Symptoms

User Mico pointed out that \hfill replacing all \hspace{0.xx\textwidth} solves the problem/symptom since it's a variable length.

I still don't understand why the calculations don't add up.

Solution for the Problem

User Arash Esbati and user Mico found the root cause. I need to mask the line break after \hspace{0.xx\textwidth} with a % as in \hspace{0.1\textwidth}%. The line break adds an extra space that leads to a total width of more than 100 %.

Related

Final Solution and Some Additional Examples

\documentclass{article}

\usepackage[
    showframe, % This option shows the margins and so on
]{geometry}

\usepackage{graphicx}

% Here: H option for float placement
\usepackage{float}

% caption and subcaption work together
\usepackage{subcaption} % loads the caption package

\begin{document}

\section{Two Figures}
\subsection{Horizontal}

\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.45\textwidth]{example-image-a}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption B}{\includegraphics[width=0.45\textwidth]{example-image-b}}%
\caption{Caption}
\end{figure}

\subsection{Vertical}

\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.45\textwidth]{example-image-a}}%
\\ % <-- Line break
\subcaptionbox{Subcaption B}{\includegraphics[width=0.45\textwidth]{example-image-b}}%
\caption{Caption}
\end{figure}

\section{Three Figures}
\subsection{Horizontal}

\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.30\textwidth]{example-image-a}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption B}{\includegraphics[width=0.30\textwidth]{example-image-b}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption C}{\includegraphics[width=0.30\textwidth]{example-image-C}}%
\caption{Caption}
\end{figure}

\subsection{Vertical}

\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.30\textwidth]{example-image-a}}%
\\ % <-- Line break
\subcaptionbox{Subcaption B}{\includegraphics[width=0.30\textwidth]{example-image-b}}%
\\ % <-- Line break
\subcaptionbox{Subcaption C}{\includegraphics[width=0.30\textwidth]{example-image-C}}%
\caption{Caption}
\end{figure}

\subsection{Mixed}

\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.30\textwidth]{example-image-a}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption B}{\includegraphics[width=0.30\textwidth]{example-image-b}}%
\\ % <-- Line break
\subcaptionbox{Subcaption C}{\includegraphics[width=0.30\textwidth]{example-image-C}}%
\caption{Caption}
\end{figure}

\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.45\textwidth]{example-image-a}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption B}{\includegraphics[width=0.45\textwidth]{example-image-b}}%
\\ % <-- Line break
\subcaptionbox{Subcaption C}{\includegraphics[width=0.30\textwidth]{example-image-C}}%
\caption{Caption}
\end{figure}

\section{Four Figures}
\subsection{Horizontal}

\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.20\textwidth]{example-image-a}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption B}{\includegraphics[width=0.20\textwidth]{example-image-b}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption C}{\includegraphics[width=0.20\textwidth]{example-image-C}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption D}{\includegraphics[width=0.20\textwidth]{example-image}}%
\caption{Caption}
\end{figure}

\subsection{Mixed}

\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.45\textwidth]{example-image-a}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption B}{\includegraphics[width=0.45\textwidth]{example-image-b}}%
\\ % <-- Line break
\subcaptionbox{Subcaption C}{\includegraphics[width=0.45\textwidth]{example-image-C}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption D}{\includegraphics[width=0.45\textwidth]{example-image}}%
\caption{Caption}
\end{figure}

\section{Symmetric Padding}

See the following example with small picture widths.

\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}{\includegraphics[width=0.20\textwidth]{example-image-a}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption B}{\includegraphics[width=0.20\textwidth]{example-image-b}}%
\\ % <-- Line break
\subcaptionbox{Subcaption C}{\includegraphics[width=0.20\textwidth]{example-image-C}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption D}{\includegraphics[width=0.20\textwidth]{example-image}}%
\caption{Caption}
\end{figure}

You can use the optional argument of the \texttt{\textbackslash subcaptionbox} in order to define the width of the sub element.

\begin{figure}[H]
\centering
\subcaptionbox{Subcaption A}[0.50\textwidth]{\includegraphics[width=0.20\textwidth]{example-image-a}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption B}[0.50\textwidth]{\includegraphics[width=0.20\textwidth]{example-image-b}}%
\\ % <-- Line break
\subcaptionbox{Subcaption C}[0.50\textwidth]{\includegraphics[width=0.20\textwidth]{example-image-C}}%
\hfill % <-- Seperation
\subcaptionbox{Subcaption D}[0.50\textwidth]{\includegraphics[width=0.20\textwidth]{example-image}}%
\caption{Caption}
\end{figure}

Here I try to illustrate the idea behind the solution -- \textbf{don't use this code}!

\begin{figure}[H]
\centering
\fbox{%
\subcaptionbox{Subcaption A}[0.482\textwidth]{\includegraphics[width=0.20\textwidth]{example-image-a}}%
}%
\hfill % <-- Seperation
\fbox{%
\subcaptionbox{Subcaption B}[0.482\textwidth]{\includegraphics[width=0.20\textwidth]{example-image-b}}%
}%
\\ % <-- Line break
\fbox{%
\subcaptionbox{Subcaption C}[0.482\textwidth]{\includegraphics[width=0.20\textwidth]{example-image-c}}%
}%
\hfill % <-- Seperation
\fbox{%
\subcaptionbox{Subcaption D}[0.482\textwidth]{\includegraphics[width=0.20\textwidth]{example-image}}%
}%
\caption{Caption}
\end{figure}

\end{document}

enter image description here

  • I don't see a question here... – sodd Apr 1 '17 at 11:48
  • @hooy Better? ;) – Dr. Manuel Kuehner Apr 1 '17 at 11:49
  • Why are you using \hspace{0.1\textwidth} instead of \hfill? – Mico Apr 1 '17 at 11:54
  • @Mico Good question :). This helps! I updated the question. Is the original question (why the deterministic calculation doesn't work) still worth it? I would appreciate it. – Dr. Manuel Kuehner Apr 1 '17 at 11:58
  • 2
    You get one extra space after \hspace{0.1\textwidth} and that kills your calculation. Hide the line ends there with % and try again. – Arash Esbati Apr 1 '17 at 12:09
2

For want of a nail the shoe was lost,

for want of a shoe the horse was lost,

for want of a horse the knight was lost,

...

You're missing a % character immediately after each and every instance of

 \hspace{<some fraction of \textwidth>}

Supply those characters, and everthing will be OK, the kingdom won't be lost, and everyone will live happily ever after.

That said, I'd say it must be easier to type \hfill (with or without a terminating % character) than to engage in explicit length calculations to determine the arguments of \hspace.

A full MWE, using \hfill:

enter image description here

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{graphicx}
\usepackage{subcaption} % load 'caption' automatically
\begin{document}

\begin{figure}[t!]
\subcaptionbox{Subcaption A}{%
\includegraphics[width=0.45\textwidth]{example-image-a}}%
\hfill
\subcaptionbox{Subcaption B}{%
\includegraphics[width=0.45\textwidth]{example-image-b}}%
\caption{Two subfigures}
\end{figure}

\begin{figure}[h!]
\subcaptionbox{Subcaption A}{%
\includegraphics[width=0.30\textwidth]{example-image-a}}%
\hfill
\subcaptionbox{Subcaption B}{%
\includegraphics[width=0.30\textwidth]{example-image-b}}%
\hfill
\subcaptionbox{Subcaption C}{%
\includegraphics[width=0.30\textwidth]{example-image-c}}%
\caption{Three subfigures}
\end{figure}

\begin{figure}[h!]
\subcaptionbox{Subcaption A}{%
\includegraphics[width=0.225\textwidth]{example-image-a}}%
\hfill
\subcaptionbox{Subcaption B}{%
\includegraphics[width=0.225\textwidth]{example-image-b}}%
\hfill
\subcaptionbox{Subcaption C}{%
\includegraphics[width=0.225\textwidth]{example-image-c}}%
\hfill
\subcaptionbox{Subcaption D}{%
\includegraphics[width=0.225\textwidth]{example-image}}%
\caption{Four subfigures}
\end{figure}

\end{document}
  • Thanks. Another comment pointed in the same direction. I also updated the question. I should have solved this myself... :). – Dr. Manuel Kuehner Apr 1 '17 at 12:18
  • I also updated the code in the question (I summarized your solution since I didn't know that you also will update your answer). In Figure 10 I also address a different "problem" and provide a solution. – Dr. Manuel Kuehner Apr 1 '17 at 12:34
  • 1
    @Dr.ManuelKuehner - Sorry it took me a few minutes to supply and MWE. – Mico Apr 1 '17 at 12:37

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.