Tx · LaTeX & TikZ

Can You Prevent a LaTeX Package from Redefining a Command?

Published By the 9to5Science editorial team

At a glance

  1. 1. Check when it changes

    Look for a package option or documentation that controls the command definition.

  2. 2. Load the package

    Most often, let the package load and then restore or define the command you want.

  3. 3. Handle delayed changes

    If the package changes it at document start, restore it at the end of that initialization.

  4. 4. Test dependent features

    Confirm that other commands from the package still work with your restored definition.

A LaTeX package can define or redefine commands while it loads, or schedule a change to happen later. There is no general mechanism that tells LaTeX to ignore every attempted redefinition of one selected command. The practical approach is to find when the package changes the command and apply your preferred definition afterward.

First check for a package option

The cleanest fix is often an option provided by the package. Read its documentation or inspect the package file for options related to the symbol or command. A package option is preferable to overriding a command manually because the package may use its own definition consistently across related features.

If there is no suitable option, decide whether you want the command’s old definition back or a new definition of your own.

Restore or replace the command after loading

For a command already defined before the package is loaded, save its meaning first with \let. Load the package, then restore the saved meaning:

\documentclass{article}

\newcommand{\sample}{Original meaning}
\let\savedSample\sample

% Replace this line with the package that changes \sample.
% For illustration, the next line simulates such a package change.
\renewcommand{\sample}{Package meaning}

\let\sample\savedSample

\begin{document}
\sample
\end{document}

This is a complete example of the save-and-restore pattern. In a real document, put the \let\savedSample\sample before the relevant package is loaded and the restoring \let after it. \let copies the command’s current meaning, so the saved command is not affected when the package later changes \sample.

If you want your own definition instead of the earlier one, redefine the command after the package:

\usepackage{somepackage}
\renewcommand{\sample}{My preferred meaning}

Here, somepackage and \sample stand for the package and command in your document. Use \renewcommand when the command is already defined; it reports an error if it is not. \newcommand is for a command that is not yet defined, while \providecommand defines it only if it is not already defined. Neither \newcommand nor \providecommand prevents a later package from changing the command.

If the package changes it at document start

A package can defer its change until \begin{document}. In that case, a redefinition placed after \usepackage in the preamble may not be the last change. With a current LaTeX format, you can schedule a restoration for the end of the begin-document hook:

\documentclass{article}
\usepackage{somepackage}

\let\savedSample\sample
\AddToHook{begindocument/end}{\let\sample\savedSample}

\begin{document}
\sample
\end{document}

This assumes \sample has the meaning you want when the \let runs in the preamble, and that the package’s deferred redefinition occurs during begin-document initialization. If the command is undefined at that point, save or define the intended meaning another way. For older LaTeX formats without the hook interface, a package-specific hook or a carefully ordered \AtBeginDocument command may be needed; check the package documentation and test the result.

Common mistakes

  • Redefining too early: A later package load can overwrite your change. Put your fix after the package unless the package documentation specifies a different order.
  • Using \newcommand on an existing command: This normally causes an error. Use \renewcommand to replace a command that already exists.
  • Saving after the package changed it: \let\savedSample\sample saves the current meaning. If you want the pre-package meaning, save it before loading the package.
  • Assuming every conflict is only visual: Other package commands may rely on the redefined command. Restoring an older definition can affect spacing, font selection, or related symbols, so test the document and consult the package documentation.
  • Trying to block all redefinitions: A generic wrapper cannot reliably stop arbitrary package code from assigning a new meaning. A targeted restoration, package option, or different package is usually safer.

If overriding the command breaks another feature, consider using a different command for your preferred symbol or choosing a package that provides the desired behavior without the conflict.

References

  1. LaTeX2e unofficial reference manual

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.