6

I've been using LaTeX for years and never saw this before: if an item (in enumerate) starts with an \hbox, then the \hbox is placed before the label!

\documentclass[12pt]{article}

\def\Blank#1{\relax{\hbox to 1in{\hrulefill\textsl{#1}\hrulefill}}}

\begin{document}

\section{Example}

Here's a list:
\begin{enumerate}
\item
\Blank{test}, plus stuff after.

\item{}
\Blank{test}, plus stuff after.

\item\relax
\Blank{test}, plus stuff after.

\item{ }
\Blank{test}, plus stuff after.

\item{ }\relax
\hbox to 1in{\hrulefill\textsl{test}\hrulefill}, plus stuff after.

\item{ X }
\Blank{test}, plus stuff after.

% etc.

\end{enumerate}

\end{document}

The result is unexpected, except for the last item. shows text coming before labels

Blanks, \relax, nothing stops the \hbox being moved back except actual text.

What am I doing wrong?

2

2 Answers 2

8

As explained in

Differentiating between \hbox and \mbox

\hbox is a TeX primitve not a LaTeX command and does not react well with most LaTeX constructs, notably it is not centered by a center environment and has various perhaps unexpected behaviours at the start of a paragraph.

The specific interaction with lists is not mentioned there though so answering here rather than suggesting that is a duplicate.

LaTeX implements list item labels by typesetting the label into a box register and then inserting the label at the start of the following paragraph. \hbox does not start a paragraph, this primitive box construct inserts the hbox directly into the surrounding vertical list, so the saved item label is not inserted until some following text does start the paragraph. So the lable comes after the box.

So you could fix this by using \leavevmode\hbox instead of simply \hbox (that is the main part of the definition of \mbox) or as you are using the \hbox to syntax you could use the standard latex equivalent namely

\makebox[1cm]{...}

which will automatically apply \leavevmode as well as constructing the 1cm wide box.

2
  • didn't see befor posting . Sorry ;-)
    – touhami
    Aug 6, 2019 at 17:54
  • @touhami no harm done:-) Aug 6, 2019 at 17:55
3

What happen is that the label is placed with \everypar command.

\everypar content is inserted at the beging of every paragraph, when TeX enter to horizental mode (after indentation).

The \hbox primitive doesn't start a new paragraph i.e. it's add to the vertical list, so the label will wait for additional stuff that start a new paragraph (comma , in the OP).

See this question

ECM

\documentclass{article}

\def\myitem{\setbox0=\hbox{\textbullet\quad}\everypar{\unhbox0\everypar{}}}

\begin{document}
\myitem foo bar.

\myitem you see.

\myitem \mbox{OK. here} every thing is good.

\myitem \hbox {No!} but not here.
\end{document}

ECM

\documentclass{article}

\begin{document}
\begingroup
\everypar{\textbf{Foo} }
yes

\mbox{yes} again

\hbox{what's} now
\endgroup
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .