TransWikia.com

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?

One Answer

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}

enter image description here

Answered by Karl Hagen on December 19, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP