[Tex/LaTex] line numbers across multicols in scrartcl

koma-scriptline-numberingtwo-column

The answer to the following question suggested by @IanThompson with left and right column numbers works sufficiently when we use \documentclass[twocolumn]{article}

However I am using a different document class – that being {scrartcl} and cannot get the left column to have line numbers to the left and vice versa for the right coulmn.

Here is some generic code, to work with:

\documentclass[10pt,a4paper]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[margin=2.5cm]{geometry}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{multicol}
\usepackage{hyperref}
%\usepackage[switch, columnwise]{lineno}
\usepackage{lineno} 
\usepackage{setspace}
\usepackage{lipsum}
\title{Correct formatting line numbers}
\author{Contributions from the world wide web}

\begin{document}
    \maketitle
    \doublespacing

\begin{abstract}
    \begin{linenumbers}
        \lipsum[1-1]
    \end{linenumbers}

\end{abstract}

\begin{multicols}{2}

    \begin{linenumbers}

        \section{Introduction}      
        \lipsum[1-3]

        \end{linenumbers}
    \end{multicols}
\end{document}

As suggested in the linked question at the top of the page, playing around with columnwise in \usepackage[switch, columnwise]{lineno} does not produce the desired output

Best Answer

You can use this new command on this German forum, which is placed inside of the multicols environment. Outside, you can use the regular command for the line numbers.

It requires you to load the colaction option for multicol.

Output

enter image description here

Code

\documentclass[10pt,a4paper]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[margin=2.5cm]{geometry}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage[colaction]{multicol}
\usepackage{hyperref}
\usepackage{lineno} 
\usepackage{setspace}
\usepackage{lipsum}
\title{Correct formatting line numbers}
\author{Contributions from the world wide web}

\newcommand\multicollinenumbers{%
 \linenumbers
 \def\makeLineNumber{\docolaction{\makeLineNumberLeft}{}{\makeLineNumberRight}}}


\begin{document}
\maketitle
\doublespacing

\begin{abstract}
\begin{linenumbers}
    \lipsum[1-1]
\end{linenumbers}

\end{abstract}

\begin{multicols}{2}
\multicollinenumbers

\section{Introduction}      
\lipsum[1-3]

\end{multicols}
\bigskip
\begin{linenumbers}
\lipsum[2-3]
\end{linenumbers}
\end{document}