I am trying to understand the difference between \hfil
and \hfill
. I always thought that these commands were roughly equivalent except that \hfill
was "stronger" than \hfil
. As a result, I thought that I could centre a line of text using either of the lines:
\hfil Horizontally centred \hfil
\hfill Horizontally centred \hfill
It turns out that only the first of these snippets actually horizontally centers the text but I do not understand why.
There is a similar issue with \vfil
and \vfill
. My understanding is that \vfill
can be defined as \vspace{\fill}
, so it makes sense that you cannot vertically center some text using
\vfill Vertically centered \vfill
because \vspace
is ignored at the top of the page. By the same reasoning,
\vspace*{\fill} Vertically centred\vfill
should work and it does, although not quite perfectly. In a similar way, you can use \hspace*{\fill}
to horizontally centre text.
Is anyone able to clear explanation of what the differences are between \hfil
and \hfill
, and between \vfil
and \vfill
? I am aware of the post What is the difference between 'fil' and 'fill'? but this does not help me.
In trying to understand what is going on I created the following MWE:
\documentclass{article}
\usepackage[paperheight=40mm, paperwidth=80mm,showframe]{geometry}
\parindent=0pt
\begin{document}
\hfil Horizontally centred \hfil
\hfill Horizontally centred \hfill
\hspace*{\fill} Horizontally centred \hfill
\hfill Horizontally centred \hspace*{\fill}
\hfil Horizontally centred \hspace*{\fill}
\newpage
\vfil Vertically centred\vfil
\newpage
\vfill Vertically centred\vfill
\newpage
\vspace*{\fill} Vertically centred\vfill
\end{document}
which produces the four pages:
Of course, I know that I can centre text with either
\centerline{Horizontally centred}
\begin{center}Horizontally centred\end{center}
but that's not what I am asking....although I am not convinced that I know the best way to vertically centre text!
\hfil
and friends always need something to push against. So I tend to add struts. AFAIR\hspace*
automatically adds "stomething to push against