11

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

  • Could s/o add more appropriate tags? Seems to me this one alone won't show up very well in searches including \hfill or \hspace, but they don't appear as options... – Rax Adaam Apr 24 '17 at 15:45
  • The final \null serves no purpose. – egreg Apr 24 '17 at 16:36
  • @egreg How can you say that without explaining the difference that Rax is seeing in the output with or without that control sequence. – Kaz Apr 24 '17 at 17:15
  • @Kaz Remove \null and you'll see that the output is exactly the same. – egreg Apr 24 '17 at 17:16
  • @Kaz this point is addressed by David C.'s answer (specifically, there is no item following the final \hspace{\fill}). – Rax Adaam Apr 24 '17 at 17:28
14

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

| improve this answer | |
  • Amazing: you pointed out several things here that I wasn't registering even though I "knew" them - thank you for not only addressing the question but pointing out related issues (extra spaces due to poor use of % etc). Best, RA – Rax Adaam Apr 24 '17 at 16:25
  • More precisely, when used in horizontal mode \item will remove three globs of glue (see latex.ltx, ll. 4671–4673), substituting the \parfillskip glue for them (0pt plus 1fil, weaker than \fill); in @RaxAdaam’s example these are the \fill glue, the null skip that \hspace* adds after it, and the glue due to the uncommented end-of-line after \hspace*{\fill}. Incidentally, \endtrivlist in horizontal mode removes only two globs (line 4625), and that’s why, if you remove the \null in the second item, the \fill survives notwithstanding (who knows whether egreg knew this… ;-) . – GuM Apr 24 '17 at 18:36
  • 1
    @GustavoMezzetti yes I noticed that while checking for this answer, but decided the extra information wasn't clarifying the main point about the fact that \null is guarding from \unskip :-) – David Carlisle Apr 24 '17 at 18:40
  • Actually, the main reason for which I added my comment was to tease you about @egreg… :-) (“@” intentionally added.) – GuM Apr 24 '17 at 18:51
  • @GustavoMezzetti who's egreg? – David Carlisle Apr 24 '17 at 18:55
4

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.

| improve this answer | |

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.