Why does \hspace*{\fill} need \null for desired output?

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}%


First of all:

  • \hspace*{\fill} % generates \hfill folowed by space (note the space before percent character) but direct \hfill does not this.

  • \hspace* does protection at the start of the line, no at its end.

  • TeX (no LaTeX!) removes the last glue when \par is followed.

  • LaTeX's \item macro does \unskip\unskip\par when it is processed in horizontal mode.

  • LaTeX's \hspace*{\fill} macro puts \vrule width0pt \hfill \hskip0pt into horizontal metarilal.

The \hspace*{\fill} % followed by \item removes your unwanted space before percent character using fist \unskip, then removes \hskip0pt using second \unskip and finally \par removes \hfill. This is the reason why your \hspace*{\fill} % disappears when it is followed by \item.