6

In the example below, the first \hbox to \linewidth ends up at the left of the page, despite the fact that it is inside an itemize environment. The presence of \hspace*{0pt} before the second seems to eliminate the problem.

\documentclass[12pt]{article}
\begin{document}
This is some text which shows the width of the whole page.
We must make sure it is long enough to occupy at least one whole line.
\begin{itemize}
\item 
Inside the itemize environment, the text is not so wide. 
\par
\hbox to \linewidth{\rule{\linewidth}{1pt}}
\par
\hspace*{0pt}\hbox to \linewidth{\rule{\linewidth}{1pt}}
\end{itemize}
\end{document}

What is going on here?

2
  • \par is special inside a list. Using \endgraf you'll notice a different output than you see with using \par.
    – Werner
    Jun 26, 2012 at 16:15
  • 1
    \hbox is not a supported LaTeX command: you are supposed to use \makebox Jun 26, 2012 at 16:20

1 Answer 1

6

\hbox is not a supported LaTeX command: you are supposed to use \makebox or in this case just \rule directly. (Note similarly that while the LaTeX \rule works the TeX primitive \hrule would fail in the same way as \hbox.

enter image description here

\documentclass[12pt]{article}
\begin{document}
This is some text which shows the width of the whole page.
We must make sure it is long enough to occupy at least one whole line.
\begin{itemize}
\item 
Inside the itemize environment, the text is not so wide. 
\par
\hbox to \linewidth{\rule{\linewidth}{1pt}}
\par
\hspace*{0pt}\hbox to \linewidth{\rule{\linewidth}{1pt}}
\par
\rule{\linewidth}{1pt}
\par
\makebox[\linewidth]{\rule{\linewidth}{1pt}}
\end{itemize}
\end{document}

Technically what is going wrong is that LaTeX lists produce indentation by adjusting the paragraph shape, but \hbox does not start a paragraph it adds itself directly to the current vertical or horizontal list, with the result you see.

2
  • In the actual application I need implicit braces and \makebox[\linewidth]\bgroup stuff \egroup fails. Any ideas? Jun 26, 2012 at 16:31
  • 3
    well I could tell you to use an environment form but if no one is looking I could tell you to put \leavevmode in front of the \hbox. If you compare the definitions of \mbox and \hbox you'll see that is the main difference. Of course \leavevmode isn't a supported LaTeX command either, but sometimes two wrongs do make a right. Jun 26, 2012 at 16:36

You must log in to answer this question.

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