7

I'm using the lineno package to number lines in a document. By default, lineno attempts to number everything, including section headers. I would like disable line numbering on section headers, preferably in an automated way so I don't need to manually edit every section header in my document.

The lineno manual provides the following ways to disable line numbering:

\nolinenumbers
... text ...
\linenumbers

\begin{nolinenumbers}
... text ...
\end{nolinenumbers}

This works fine except I need to manually add this code around every \section{...} call in my document.

Presumably I could \renewcommand{\section} to do this automatically, but I am not sure of the Right Way (TM) to do this. The following naive code appears to work, but I am not convinced that I won't trip over some nasty edge case by doing this. Among other things, I'll need to redefine \subsection and \subsubsection in addition to \section for this to work in general.

\let\oldsection\section
\renewcommand{\section}[1]{\nolinenumbers \oldsection{#1} \linenumbers}

Is there a more robust and/or elegant way of doing this?

2 Answers2

7

If you're not using any special packages, the following would be sufficient:

enter image description here

\documentclass{article}
\usepackage{etoolbox,lineno}% http://ctan.org/pkg/{etoolbox,lineno}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\makeatletter
% \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
\patchcmd{\@startsection}{\@ifstar}{\nolinenumbers\@ifstar}{}{}
\patchcmd{\@xsect}{\ignorespaces}{\linenumbers\ignorespaces}{}{}
\makeatother
\linenumbers
\begin{document}
\section{A section} \lipsum[2]
\subsection{Another section} \lipsum[2]
\subsubsection{Final section} \lipsum[2]
\end{document}

There are two patches applied with the aid of etoolbox.

  • The first inserts \nolinenumbers as part of \@startsection - called just after \par is issued to ensure there is no line numbers in the sectional title line;
  • The second inserts \linenumbers just after the sectional title has been set.

The specific location of these insertions will allow for unnumbered sectional units of all types (starred or not), although the effect will not be visible in some instances (like \paragraph and \subparagraph, by default), since run-in sections have text immediately following the title.

Werner
  • 603,163
  • I'm running into an error when I try to compile this on overleaf. I'm not sure what is causing it. Here is the part of my preamble where I call the \lineno package. \usepackage[pagewise]{etoolbox,lineno} \patchcmd{\@startsection}{\@ifstar}{\nolinenumbers\@ifstar}{}{} \patchcmd{\@xsect}{\ignorespaces}{\linenumbers\ignorespaces}{}{} \makeatother \linenumbers\relax

    The error I'm getting is "Option clash package for etoolbox" and "Missing \begin{document}" but it's not missing.

    I also added the global option pagewise to the documentclass but that didn't work either.

    – WnGatRC456 Sep 27 '20 at 00:58
  • @WnGatRC456: Try with \usepackage{etoolbox}\usepackage[pagewise]{lineno}. – Werner Sep 27 '20 at 19:51
  • unfortunately, that doesn't seem to work. I'll make a separate question to try and get this sorted out. – WnGatRC456 Sep 29 '20 at 20:10
0

Some supplements to Werner's great answer, in case you are using titlesec, for which Werner's patch does not work.

First, a quick aside of using \nolinenumbers and \linenumbers. Actually, in my own packages or classes, I usually have the following:

\newif\ifLNturnsON
\def\LocallyStopLineNumbers{\LNturnsONfalse%
    \ifLineNumbers\LNturnsONtrue\fi\nolinenumbers}
\def\ResumeLineNumbers{\ifLNturnsON\linenumbers\fi}

\LocallyStopLineNumbers and \ResumeLineNumbers are better than just \nolinenumbers and \linenumbers in that they detect if the line numbering has already been turned on, so after disabling the line numbers \ResumeLineNumbers won't enable it if the user hasn't enabled the line numbering at the time of \LocallyStopLineNumbers.

With titlesec, you can add \LocallyStopLineNumbers to the beginning of the format, and [\LocallyStopLineNumbers] to the end. In the following example, I included a complete set of formats that mimic the default styles (the code is copied from documentation of titlesec, "8.2. Standard Classes"). You can of course modify your own style in the similar way.

\documentclass{article}
\usepackage{lineno}
\usepackage{lipsum}

\usepackage{titlesec}

\newif\ifLNturnsON \def\LocallyStopLineNumbers{\LNturnsONfalse% \ifLineNumbers\LNturnsONtrue\fi\nolinenumbers} \def\ResumeLineNumbers{\ifLNturnsON\linenumbers\fi}

\makeatletter

\titleformat{\chapter}[display] {\LocallyStopLineNumbers\normalfont\huge\bfseries}{\chaptertitlename\ \thechapter}{20pt}{\Huge}[\ResumeLineNumbers] \titleformat{\section} {\LocallyStopLineNumbers\normalfont\Large\bfseries}{\thesection}{1em}{}[\ResumeLineNumbers] \titleformat{\subsection} {\LocallyStopLineNumbers\normalfont\large\bfseries}{\thesubsection}{1em}{}[\ResumeLineNumbers] \titleformat{\subsubsection} {\LocallyStopLineNumbers\normalfont\normalsize\bfseries}{\thesubsubsection}{1em}{}[\ResumeLineNumbers] \titleformat{\paragraph}[runin] {\LocallyStopLineNumbers\normalfont\normalsize\bfseries}{\theparagraph}{1em}{}[\ResumeLineNumbers] \titleformat{\subparagraph}[runin] {\LocallyStopLineNumbers\normalfont\normalsize\bfseries}{\thesubparagraph}{1em}{}[\ResumeLineNumbers] \titlespacing{\chapter} {0pt}{50pt}{40pt} \titlespacing{\section} {0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex} \titlespacing{\subsection} {0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex} \titlespacing{\subsubsection}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex} \titlespacing{\paragraph} {0pt}{3.25ex plus 1ex minus .2ex}{1em} \titlespacing{\subparagraph} {\parindent}{3.25ex plus 1ex minus .2ex}{1em}

\makeatother

\linenumbers

\begin{document} \section{A section} \lipsum[2] \subsection{Another section} \lipsum[2] \subsubsection{Final section} \lipsum[2] \end{document}

enter image description here

Jinwen
  • 8,518