Some time ago I took the solution from this answer to show lineno's line numbers on both sides of the page.
However, I want numbers to be in place only sometimes (say when I need to send the document to somebody else for corrections), so I want to be able to remove lineno in a consistent way by just commenting its \usepackage line. For this purpose, I searched an found this, and thought I could use it to compile-out both the linked trick and the \linenumbers command.
Unfortunately, it looks like the \OnlyIfPackageLoaded command defined below can't compile-out the trick I mentioned earlier, because the \OnlyIfPackageLoaded{lineno}{ the trick I mentioned above } causes a compilation error (on the other hand, the line \OnlyIfPackageLoaded{lineno}{\linenumbers} seems to be doing what I expected).
\documentclass{book}
\usepackage{lipsum}
\usepackage[left, pagewise]{lineno}
\usepackage{ltxcmds}
\makeatletter
\newcommand{\OnlyIfPackageLoaded}[2]{\ltx@ifpackageloaded{#1}{#2}{}}
\makeatother
\OnlyIfPackageLoaded{lineno}{
\makeatletter
\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}
I've tried using the answer proposed at the linked question but I haven't managed to make it work.
Since \OnlyIfPackageLoaded{lineno}{\linenumbers} does what I expect, I assume that
\makeatletter
\newcommand{\OnlyIfPackageLoaded}[2]{\ltx@ifpackageloaded{#1}{#2}{}}
\makeatother
is correct and needs no change. So what needs change is
\OnlyIfPackageLoaded{lineno}{
\makeatletter
\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
}
but how? I've tried removing \makeatletter and \makeatother and then changing \hb@xt@ to \csname hb@xt@\endcsname, but I get the same error. Nevermind, I must have made a typo on the first attempt.
\if. You can keep the package and just place the relevant macros inside\if...wrappers, then you can use\...true/\...falseas a switch. But indeed correctly positioningmakeatletterand\makeatotheralready should solve your problem. – Jasper Habicht Mar 04 '23 at 20:09\OnlyIfPackageLoaded{lineno}{ \makeatletter...\makeatother }, but\makeatletter \OnlyIfPackageLoaded{lineno}{...} \makeatother. So, you should place\OnlyIfPackageLoaded{lineno}{...}between\makeatletterand\makeatother, because these two macros may not be used inside other macros. – Jasper Habicht Mar 04 '23 at 21:17