1

This question was asked before where the accepted answer patches relevant LaTeX macros involved. This accepted answer does not work for me - the code has no effect. Anyway, the lineno documentation specifically states

You do not usually turn off line numbering explicitly, but rather restrict line numbering to a group or an environment. (pg. 3)

So is this telling me there's an easier way than hacking \section and \xsect (as in the linked answer) which doesn't involve constantly having to write \begin{linenumbers} ... \end{linenumbers} environments?

If I have to use the linked answer, (1) can somebody check the code is correct and (2), how could you also exclude enumerated lists too?

Also - for my info: the linked accepted answer uses mtoolbox for the patchcmd macro. Can't this be achieved with \renewcommand e.g \renewcommand*{\section}{\nolinenumbers\section\linenumbers}?

EDIT:

  • How could I achieve this for itemize environment, enumerated lists, and blockquotes?
Timmy
  • 95

1 Answers1

1

You didn't give an example of why the accepted answer you linked does not work for you. To my experience, the reason might be:

  1. You are not using the standard classes article, book etc. In your class there might be no \@startsection or \@xsect so the patches do not work.
  2. The style of \section, \subsection, etc. have been modified by package like titlesec.

A quick note: the command \section is more sophisticated than you thought: it detects whether it has a star follows and there is usually also an optional argument for assigning the short title for substituting the actually title in the hyperlinks, and then it reads in the main argument, i.e. your section title. Thus you cannot expect something simple as \renewcommand*{\section}{\nolinenumbers\section\linenumbers} to work.

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 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.

Now for the solution, either way, you can use titlesec and 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
  • apologies, yes, I am using \documentclass{scrartcl} ... \usepackage{scrletter} in my preamble (for a letter but with section capabilities).
  • – Timmy Mar 01 '22 at 15:22