0

From a previous question, take this example

\documentclass{book}

\usepackage{lipsum}

\usepackage[left, pagewise]{lineno} % LINE \usepackage{ltxcmds}

\makeatletter \newcommand{\OnlyIfPackageLoaded}[2]{\ltx@ifpackageloaded{#1}{#2}{}} \makeatother

\makeatletter \OnlyIfPackageLoaded{lineno}{ \def\makeLineNumberLeft{% \linenumberfont\llap{\hb@xt@\linenumberwidth{\LineNumber\hss}\hskip\linenumbersep}% left line number \hskip\columnwidth% skip over column of text \rlap{\hskip\linenumbersep\hb@xt@\linenumberwidth{\hss\LineNumber}}\hss}% right line number \leftlinenumbers% Re-issue [left] option } \makeatother

\begin{document}

\OnlyIfPackageLoaded{lineno}{\linenumbers}

\lipsum[1]

\end{document}

where \OnlyIfPackageLoaded is used to make lineno-related things disappear if I comment out its \usepackage.

Unfortunately:

  • if I compile from a clean directory with THAT line commented, the compilation succeeds;
  • if I decomment THAT line and recompile, the compilation succeeds;
  • if I re-comment THAT line and recompile, the compilation fails:
    ...
    (./main.aux
    ! Undefined control sequence.
    l.2 \@LN
            {0}{0}
    ? 
    
  • if I delete the .aux file and recompile, the compilation succeeds.

What can I do to avoid having to manually delete the AUX file when I go from with-lineno to without-lineno?

Enlico
  • 2,592

1 Answers1

3

You just need to not hide the else branch of \@ifpackageloaded so you can define the aux command to do nothing. (ltxcmds does nothing here as this is all already latex)

\documentclass{book}

\usepackage{lipsum}

\usepackage[left, pagewise]{lineno} % LINE % not useful here \usepackage{ltxcmds}

\makeatletter \newcommand{\OnlyIfPackageLoadedTF}{@ifpackageloaded} \newcommand{\OnlyIfPackageLoadedT}[2]{@ifpackageloaded{#1}{#2}{}} \makeatother

\makeatletter \OnlyIfPackageLoadedTF{lineno}{ \def\makeLineNumberLeft{% \linenumberfont\llap{\hb@xt@\linenumberwidth{\LineNumber\hss}\hskip\linenumbersep}% left line number \hskip\columnwidth% skip over column of text \rlap{\hskip\linenumbersep\hb@xt@\linenumberwidth{\hss\LineNumber}}\hss}% right line number \leftlinenumbers% Re-issue [left] option } % else { \def@LN#1#2{} } \makeatother

\begin{document}

\OnlyIfPackageLoadedT{lineno}{\linenumbers}

\lipsum[1]

\end{document}

David Carlisle
  • 757,742
  • What is \def\@LN#1#2{} for? And what are T and TF suffixes meant to stand for? – Enlico Mar 05 '23 at 06:42
  • 1
    @Enlico as you show in your question the aux file has lines \@LN{...}{...} and you need to define them to do nothing. TF is a convention used by several newer latex commands mimicking expl3, the TF version has true and false arguments the T version is your original with just a true branch and no else – David Carlisle Mar 05 '23 at 08:12