2

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.

2
  • Perhaps see the marginnote package, similar to marginpar but does not float
    – daleif
    Commented Dec 31, 2016 at 10:09
  • lineno package? Commented Dec 31, 2016 at 10:41

2 Answers 2

6

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.

5
  • I was under the impression that \makebox[0pt][r]{Numbers\quad} was the best choice. Should I stand corrected?
    – egreg
    Commented Dec 31, 2016 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 Commented Dec 31, 2016 at 15:40
  • Maybe adding the better version would help. ;-)
    – egreg
    Commented Dec 31, 2016 at 15:45
  • @egreg anything to keep you quiet:-) Commented Dec 31, 2016 at 16:31
  • @egreg Thank you both for the answer and the helpful comments
    – Yan Zhou
    Commented Dec 31, 2016 at 18:52
0

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.

You must log in to answer this question.

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