# Print text on the left margin aligned with a given line

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}


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,

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.

• Perhaps see the marginnote package, similar to marginpar but does not float – daleif Dec 31 '16 at 10:09
• lineno package? – David Carlisle Dec 31 '16 at 10:41

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

\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.

• I was under the impression that \makebox[0pt][r]{Numbers\quad} was the best choice. Should I stand corrected? – egreg Dec 31 '16 at 15:36
• @egreg yes that's what I'd use but just answering that way wouldn't have shown why the OPs version didn't quite work – David Carlisle Dec 31 '16 at 15:40
• Maybe adding the better version would help. ;-) – egreg Dec 31 '16 at 15:45
• @egreg anything to keep you quiet:-) – David Carlisle Dec 31 '16 at 16:31
• @egreg Thank you both for the answer and the helpful comments – Yan Zhou Dec 31 '16 at 18:52

I do not know how to use marginpar. Just see this may help you.

This answer is taken from Labels in the left margin.

\documentclass{article}
\usepackage{enumitem}
\SetLabelAlign{margin}{\llap{#1~~}}
\usepackage{showframe} % just to show the margins
\begin{document}

\begin{description}[align=margin,labelsep=0pt]
\item[Numbers] Beginning of the line..
\item[Another Number]  Beginning of another Line \ldots
\end{description}

\begin{description}[style=multiline,leftmargin=0.25\textwidth,font=\bfseries]
\item[Numbers] Beginning of the line..
\item[Another Number]  Beginning of another Line \ldots
\end{description}

\end{document}


Another description is just to show a different description labels.