[Tex/LaTex] Exclude line numbers from margin width

double-sidedline-numberingmarginparmarginsspacing

I am using the lineno package to add linenumbers on my twosided document. However, I have a restriction to leave a margin of 2.5cm inner and 4cm outer margin. This has to be completely free space.

But the line numbers of the lineno package are being added into both those margins.

  1. Can I somehow specify that the linenumbers are added to the document body instead of the margin?
  2. If not, how do I set the margin of the document so that the correct amount of space is free of content?

I thought about just adding the line number width to the margin, but because the line numbers are always on the left margin on even and odd pages, I would have to alternate this additional margin for my twosided document.

Here is a minimal example:

\documentclass[12pt,twoside,ngerman]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[a4paper]{geometry}
\geometry{verbose,
    tmargin=2.5cm,
    bmargin=2.5cm,
    lmargin=4cm,
    rmargin=2.5cm,
    nomarginpar,
    showframe}
\usepackage{fancyhdr}
\pagestyle{fancy}

\usepackage{lineno}
\usepackage{lipsum}

\renewcommand{\linenumberfont}{\footnotesize}

\begin{document}

\linenumbers
\lipsum

\end{document}

This screenshot illustrates my problem:

margins

The orange line shows how far the 4cm / 2.5cm go, the blue line shows how they should actually be. The line numbers should be further to the right.

Best Answer

Redefine \makeLineNumber and give more room to the left margin:

\documentclass[12pt,twoside,ngerman]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[a4paper]{geometry}
\geometry{verbose,
    tmargin=2.5cm,
    bmargin=2.5cm,
    lmargin=5cm,
    rmargin=2.5cm,
    nomarginpar,
    headheight=14.5pt,
    heightrounded,
    showframe}
\AtBeginDocument{\addtolength{\evensidemargin}{1cm}}
\usepackage{fancyhdr}
\pagestyle{fancy}

\usepackage{lineno}
\usepackage{lipsum}

\renewcommand{\makeLineNumber}{\llap{\linenumberfont\rlap{\LineNumber}\hspace{1cm}}}

\renewcommand{\linenumberfont}{\footnotesize}

\begin{document}

\linenumbers

\noindent% just to make a rule
\llap{\vrule height 1pt width 3.9cm\hskip1cm}%
\lipsum[1-6]

\noindent% just to make a rule
\llap{\vrule height 1pt width 2.4cm\hskip1cm}%
\lipsum[7]

\end{document}

The two rules are just 1mm short so that we can see the measurement is accurate. I added a setting for headheight and also heightrounded; the first is required by fancyhdr, the second makes the text height contain an integer number of lines.

Top of page 1

enter image description here

Top of page 2

enter image description here

Related Question