# Center two images using multiple \hfills [duplicate]

I have two images that I want to show side by side, horizontally centered. I would like to distribute the remaining horizontal space evenly in three parts: To the left of the first image, between the images, and after the second image. So I tried this:

\documentclass{article}

\usepackage{graphicx}
\usepackage{blindtext}
\usepackage{float}
\usepackage{mwe}

\begin{document}

\blindtext

\begin{figure}[H]
\centering
\hfill
\includegraphics[width=4cm]{example-image-a}
\hfill
\includegraphics[width=4cm]{example-image-a}
\hfill
\end{figure}

\blindtext

\end{document}


However the last \hfill is ignored:

Why is this happening? How can I achieve the desired result ?

• I was typing an answer when this question got closed. Anyway, if you want to understand all the glue on the line, try the following (as a bare "paragraph", without \centering or the figure environment): \noindent\null\hfill\includegraphics[width=4cm]{example-image-a}\hfill\includegraphics[width=4cm]{example-image-a}\hfill\null. You can play around with removing various parts of it and see what happens. – ShreevatsaR Jan 18 '17 at 19:06

An extra \hfill will do it. Also, \centering not needed in this case. And, technically speaking, one should end lines with % signs, in order to avoid the insertion of small spaces.

\documentclass{article}

\usepackage{graphicx}
\usepackage{blindtext}
\usepackage{float}
\usepackage{mwe}

\begin{document}

\blindtext

\begin{figure}[H]
%\centering
\hfill%
\includegraphics[width=4cm]{example-image-a}%
\hfill%
\includegraphics[width=4cm]{example-image-a}%
\hfill%
\hfill%
\end{figure}

\blindtext

\end{document}


Alternately (and more naturally), \hfil all around will do it with a single. However, in this case, \centering cannot be used, without the addition of an \hfilneg (negative infinite glue) at the beginning of the line (or else added asymmetric \hfils).

\documentclass{article}

\usepackage{graphicx}
\usepackage{blindtext}
\usepackage{float}
\usepackage{mwe}

\begin{document}

\blindtext

\begin{figure}[H]
%\centering\hfilneg
\hfil%
\includegraphics[width=4cm]{example-image-a}%
\hfil%
\includegraphics[width=4cm]{example-image-a}%
\hfil%
\end{figure}

\blindtext

\end{document}


• Point taken re. \centering and %. However: In the solution involving \hfill, why is the additional \hfill needed? And why it is NOT needed if using \hfil instead ? – Grodriguez Jan 18 '17 at 18:31
• @Grodriguez I can't offer a technical explanation, other than to say that some types of alignments come with pre-built fils (or anti-fils that can absorb a fil) at the beginning and end of line, and so any fils you add are competing with these. – Steven B. Segletes Jan 18 '17 at 18:33
• @Grodriguez pp. 71-72 of Knuth's The TeXbook describes some of these glues, \hfil, \hfill, \hss, and \hfilneg. – Steven B. Segletes Jan 18 '17 at 19:00
• Adding a \null or a \strut instead of a second \hfill also seems to do the trick. This answer mentions that "LaTeX removes horizontal space at the end of a line". – Grodriguez Jan 19 '17 at 8:55

Alternatively with \centering and \hfil between images:

\documentclass{article}
\usepackage{graphicx}
\usepackage{blindtext}

\begin{document}

\blindtext
\begin{figure}[htb]
\centering
\includegraphics[width=4cm]{example-image-a}
\hfil
\includegraphics[width=4cm]{example-image-b}
\end{figure}

\blindtext

\end{document}


• Thank you -- but why does this work with only one single \hfill ? – Grodriguez Jan 18 '17 at 18:31
• not with \hfill (double l, i.e:ll) but with hfil (single l) and centering. difference between them is explained in tex.stackexchange.com/questions/21022/… – Zarko Jan 18 '17 at 18:35
• Yes, I actually meant \hfil (typo in my comment). I also read the question you linked but I still don't understand -- why this works in this specific case? – Grodriguez Jan 18 '17 at 18:43
• I'm affray that I'm not able to answer better as it is explained in given link :(. Simplified: power of \hfil is smaller than of hfill ... it move images apart so, that in centering environment the horizontal empty space is equal distributed around images. With \hfill between images would move them to left and right text border. – Zarko Jan 18 '17 at 18:49