0

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.

Enlico
  • 2,592
  • (different specific command, same issue, same fix applies.) – user202729 Mar 04 '23 at 19:33
  • Why not just use a custom \if. You can keep the package and just place the relevant macros inside \if... wrappers, then you can use \...true/\...false as a switch. But indeed correctly positioning makeatletter and \makeatother already should solve your problem. – Jasper Habicht Mar 04 '23 at 20:09
  • @user202729, I don't think I've understood how to use that solution. I'll add my attempt to the question. – Enlico Mar 04 '23 at 21:07
  • You should not write \OnlyIfPackageLoaded{lineno}{ \makeatletter ... \makeatother }, but \makeatletter \OnlyIfPackageLoaded{lineno}{ ... } \makeatother. So, you should place \OnlyIfPackageLoaded{lineno}{ ... } between \makeatletter and \makeatother, because these two macros may not be used inside other macros. – Jasper Habicht Mar 04 '23 at 21:17

0 Answers0