# Numbering on margin

I want to do something similar to but not the same as line numbering:

A macro \foo is defined and a counter foocounter in an environment foonumber. When the macro is used, foocounter is increased by 1 and then printed on the left margin of the line where the macro is used. If more the macro is used than once on a line, all foocounter should be printed on the left margin, separated by a space.

This describes what I want to do, but I have no idea how I can print the counter on the left margin.

In addition, I am using multicol with two columns. I want the counter to be printed on the left side of each column. (In contrast, line numbering usually print the numbers on the outer margin.)

I write a macro \foo in the following code. This seems successful, but the last occasion of the number jumps to the next page. The final page has no next page and the number disappears. Can anybody fix it?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\def\temppp{}
\newcounter{BAR}
\newcommand\foo{\stepcounter{BAR}$^{\arabic{BAR}}$\tikz[remember picture,overlay]\path let\p1=(current page.west),\p2=(X)in(\x1,0)coordinate(X)+(120pt,\y2)node[above left]{\ifdim\y2=0pt\global\edef\temppp{\temppp~\hbox{\arabic{BAR}}}\else\scriptsize\temppp\global\edef\temppp{\hbox{\arabic{BAR}}}\fi};}
\begin{document}
\hbox to 0pt{\tikz[remember picture]\node(X){};}%Put an X at the start
\foo Lorem ipsum dolor sit amet, consectetuer adipiscing elit. \foo Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis. \foo Curabitur dictum gravida mauris. \foo Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna. \foo Donec vehicula augue eu neque. \foo Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. \foo Mauris ut leo. \foo Cras viverra metus rhoncus sem. \foo Nulla et lectus vestibulum urna fringilla ultrices. \foo Phasellus eu tellus sit amet tortor gravida placerat. \foo Integer sapien est, iaculis in, pretium quis, viverra ac, nunc. \par
\vskip 360pt \foo Praesent eget sem vel leo ultrices bibendum. \foo Aenean faucibus. \foo Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. \foo  Curabitur auctor semper nulla. \foo Donec varius orci eget risus. \foo Duis nibh mi, congue eu, accumsan eleifend, sagittis quis, diam. \foo Duis eget orci sit amet orci dignissim rutrum. \foo Lorem ipsum dolor sit amet, consectetuer adipiscing elit. \foo Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis. \foo Curabitur dictum gravida mauris. \foo Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna. \foo Donec vehicula augue eu neque. \foo Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. \foo Mauris ut leo. \foo Cras viverra metus rhoncus sem. \foo Nulla et lectus vestibulum urna fringilla ultrices. \foo Phasellus eu tellus sit amet tortor gravida placerat. \foo Integer sapien est, iaculis in, pretium quis, viverra ac, nunc. \par
\end{document}

• Why does \foo have to be used in the foonumber environment? (Or: what happens if \foo is used elsewhere?) – jon Apr 1 '14 at 3:59
• This might help: tex.stackexchange.com/questions/163567/… – John Kormylo Apr 1 '14 at 4:01
• jon: it is okay if we do not limit \foo in some environment. A possible reason to do it in this way is that we reset the counter whenever we begin the environment. – HYC Apr 1 '14 at 4:13
• Texts in two columns are not supported well. What you would like to have -- margin numbers on the left side of the columns -- is difficult to get, as far as I know. Could you describe a little bit more the purpose of your text and the margin numbers? Maybe there is another solution... – Keks Dose Apr 1 '14 at 10:32
• K.D.: Imagine a bible with verse numbers printed inline and on the margin for readers' convenience to locate the verse in a paragraph. However, I'm preparing a Chinese document printed vertically. Then the horizontally left and right columns will become vertically up and down columns. I hope those counters printed on top of the columns. Hence I have to print them on the left side of each of the two columns horizontally. (You need to rotate a vertically printed document by 90 degrees clockwise in order to read it.) – HYC Apr 1 '14 at 15:31

A try with marginnote. But this won't work with multicols as you wanted. And it also doesn't work with on left side demand.

\documentclass[twoside]{scrreprt}
\usepackage[a4paper,left=1in,right=2in,marginparwidth=1in,marginparsep=0.1in,showframe]{geometry}
\newcounter{foocounter}
\setcounter{foocounter}{0}
\newcommand\foo[1]{%
\refstepcounter{foocounter}
\marginnote{\thefoocounter}\emph{#1}
}

\usepackage{multicol}

\begin{document}
This is a\foo{goes to side} side note.
\newpage
\begin{multicols}{2}
Some text and another me and you one which is not here  but some where else and its mark is alone here.

But here \foo{go to the side and span more than one line  if possible}
\end{multicols}
\end{document}

• Thank you. I do not need any argument in \foo, so I simply write \def\foo{\stepcounter{foocounter}\marginnote{\thefoocounter}}. But it fails when the same line contains two \foo. – HYC Apr 1 '14 at 8:04