# Columnwise line numbering in a two column environment (with multicol)

TeX - LaTeX Asked by OvC on December 19, 2020

In a two column environment, I need

• columnwise line numbering and
• the possibility to get the actual column number.

Now, columnwise line numbering is possible with lineno and the option columnwise.

And I can get the actual column number in a multicols environment, using multicol with the option colaction and the command docolaction.

But unfortunately, columnwise line numbering seems not to work in the multicols environment. So how can I achieve what I want?

(Actually, I want to create references to columns, but I would already be happy if I could just get the actual column number.)

You may consider the following example:

documentclass[a4paper,fontsize=12pt,DIV=14]{scrartcl}
usepackage{xltxtra}

usepackage[columnwise,modulo]{lineno}

usepackage[colaction]{multicol}

newcommandshowcolumn{%
docolaction{1}{}{2}%
}

setlength{columnsep}{1cm}

newcommandTest{THIS IS COLUMN showcolumn{}. }

usepackage{lipsum}

begin{document}
%% twocolumn
begin{multicols}{2}
linenumbers

lipsum*[1]
Test
lipsum*[1-2]
Test
lipsum*[1]

end{multicols}
end{document}



In the result, line numbering is not columnwise. When removing the multicols environment and using the twocolumn command instead, line numbering is columnwise, but then I cannot use docolaction. So what to do?

Columnwise numbering in lineno is incompatible with multicol. The problem is that lineno attaches its numbers to each line as they are generated, and then multicol takes all those lines and puts them into columns when the page is being output. So at the time the line numbers are generated, neither lineno nor multicol can know which column a line will wind up in.

If you use twocolumn mode, you can figure out which column you're in by checking the x value at the point you're referencing. Since it seems like your end goal is to create a column/line reference, I hacked up a simple solution using zref-savepos that produces a reference in the form [column].[line]. It simply assumes that if we're on the left half of the page we're in the first column and otherwise in the second, but it should be easy to adapt to your specific needs. It will require at least two runs to get the correct value.

documentclass[twocolumn,a4paper,fontsize=12pt,DIV=14]{scrartcl}
usepackage[columnwise,modulo]{lineno}
usepackage{zref-savepos}
usepackage{lipsum}

setlength{columnsep}{1cm}
newlength{midpage}
setlength{midpage}{.5paperwidth}
newcommandlabelcolumnline[1]{%
zsavepos{#1}%
linelabel{#1}%
}

newcommandrefcolumnline[1]{%
ifnumnumberzposx{#1}>numbermidpagerelax 2else 1fi%
.ref{#1}%
}

begin{document}
linenumbers

lipsum*[1]labelcolumnline{r1}

lipsum*[1-2]labelcolumnline{r2}

lipsum*[1]

The first paragraph ends at refcolumnline{r1}.

The third paragraph ends at refcolumnline{r2}.
end{document}


Answered by Karl Hagen on December 19, 2020

## Related Questions

### How can I keep equations left-aligned only if they do not exceed a certain width?

1  Asked on December 31, 2020

### how can I use math environment for a non English language?

2  Asked on December 31, 2020 by marmik

### Circle diagram made with tkz-euclide renders differently in TeX Live 2018 versus TeX Live 2020

1  Asked on December 31, 2020 by mathemanic

### (LyX) Show Changes in Output (Color Change)

1  Asked on December 31, 2020 by drmeat

### Smaller bullet for homological grading

3  Asked on December 31, 2020 by gaussler

### white lines in paragraphs

0  Asked on December 31, 2020 by imago-mundi

### Unexpected difference between lualatex and pdflatex with babel and quotes

0  Asked on December 31, 2020 by john-macfarlane

### Can a tufte-latex file output tufte-css HTML?

1  Asked on December 31, 2020 by user23924

### Rowcolor in xltabular and whitespace

2  Asked on December 31, 2020

### I am facing problem in giving continuous line numbers from title in double column format latex template

0  Asked on December 30, 2020 by nayan-ranjan-das

### Configuring build.lua to install images

1  Asked on December 30, 2020

### How to avoid small caps exceeding pre-defined document margins?

2  Asked on December 30, 2020 by serendipity

### Listings define language in a separate file

0  Asked on December 30, 2020 by mab0189

### Rotated half a circle in 3D, lines are slightly off in tikzpicture

1  Asked on December 30, 2020

### How to do custom ordering with Datatool

1  Asked on December 30, 2020 by manuel-ruiz

### Font sizes for different header levels

1  Asked on December 30, 2020 by user877329

### Justify the list of figures’ captions

0  Asked on December 30, 2020 by zizoo-jiloo

### pdflatex + includegraphics pdf file size very large

0  Asked on December 30, 2020

### pdflatex not working anymore after updating pdflatex.ini and initexmf –dump=pdflatex

1  Asked on December 30, 2020 by user140574

### Create an adapted color macro for RTL mode (xelatex)

0  Asked on December 30, 2020