[Tex/LaTex] Why does \hspace*{\fill} need \null for desired output

horizontal alignmentspacing

I was trying to centre some math within an enumerate list, without a lineskip.

I eventually found this discussion of \hfill vs \hspace*{\fill} which seems to suggest that using \hspace*{\hfill} will avoid space on the RHS being gobbled up; however, when I implement it, I don't get any difference in rendering between the two.

NB: I resolved the specific issue by including a \null, but would like to know what misunderstanding is leading me to expect different output,

i.e. why does \hspace*{\hfill} still require \null to not gobble right-hand-side space?

MWE:

\documentclass{article}
\usepackage{enumitem}
\begin{document}
\begin{enumerate}
    \item \hspace*{\fill} %
            % Some math content
            $y(x) = a_o + a_1 (x-k) + a_2(x-k)^2 + a_3(x-k)^3 + a_4(x-k)^4$ 
            %
            \hspace*{\fill}
    %
    \item \hspace*{\fill} %
            % Some math content
            $y(x) = a_o + a_1 (x-k) + a_2(x-k)^2 + a_3(x-k)^3 + a_4(x-k)^4$
            %
            \hspace*{\fill}\null
\end{enumerate}
\end{document}

Notice the difference between the first line (without \null) and the second line (with \null):

enter image description here

Best Answer

It's more normal in latex to use \centering or the center environment to centre things, however the reason that you need the \null is \item if used in horizontal mode removes horizontal space at the end of the previous paragraph.

If you remove the % these two lines are set the same way

enter image description here

\documentclass{article}
\usepackage{enumitem}
\begin{document}
\begin{enumerate}
    \item \hspace*{\fill} %
            % Some math content
            $y(x) = a_o + a_1 (x-k) + a_2(x-k)^2 + a_3(x-k)^3 + a_4(x-k)^4$ 
            %
            \hspace*{\fill}
    % > If you remove this % (or add a line above it) both will render the same.
    \item \hspace*{\fill} %
            % Some math content
            $y(x) = a_o + a_1 (x-k) + a_2(x-k)^2 + a_3(x-k)^3 + a_4(x-k)^4$
            %
            \hspace*{\fill}\null
\end{enumerate}
\end{document}

Note also the usage \hspace*{\fill} % also breaks the centering as it adds fill glue plus one word space, you probably intended \hspace*{\fill}%