[Tex/LaTex] Print text on the left margin aligned with a given line

line-breakingmargins

I would like to place numbers on the left margin of the document, similar to the line number of fancyvrb or other listing packages, but for ordinary text. I don't want to it be margin par, which will float. And in fact, I still want to keep the margin in the right, which might have some real margin notes. (So some questions that were answered with techniques using margin par, or reverse margin, does not apply here). So far, my best attempt is something similar to the following MWE,

\documentclass{article}
\usepackage[showframe]{geometry}

\begin{document}
\hbox{\hbox to 0pt{\hss{Numbers}\hskip1em}Beginning of the line...}
\end{document}

enter image description here

Thi is more or less what I want to get in the end. However, without the outer hbox, I got a missed aligned two lines, with apparently a line break after Numbers,

enter image description here

Insert an unbreakable space ~ or temporarily change the line break penalty does not seem to help. With the outer hbox, it works fine for shorter text, but first line may not fit into a single line and thus it is troubling.

I would like to know how do I get the effect in the first screen shot for arbitrarily long line after Benning of lines.

Best Answer

You are missing the fact that \hbox does not start a paragraph

enter image description here

\documentclass{article}
\usepackage[showframe]{geometry}

\begin{document}
\noindent\hbox to 0pt{\hss Numbers\hskip1em}Beginning of the line...
middle of line end of line
\end{document}

If you do not want to lose the paragraph indentation, you need \leavevmode instead, and then adjust the spacing with \parindent

Note that the issue with \hbox not starting a paragraph is avoided if you use the official LaTeX syntax. All LaTeX box commands take care to start horizontal mode before inserting the box. As noted in comments

\noindent\makebox[0pt][r]{Numbers\quad}

would be better LaTeX markup here.

However if you want to number all such lines, perhaps the lineno package is what you need.