letltxmacro
Provides enhanced macro redefinition capabilities that correctly handle LaTeX commands with optional arguments and robust commands.
Overview
Addresses a critical need in LaTeX programming by offering safe and reliable ways to redefine macros, particularly when dealing with complex command structures. The package solves common issues that arise when using TeX's native \let command with LaTeX-specific macro types.
- Enables proper redefinition of commands that have optional arguments
- Correctly handles robust commands created with \DeclareRobustCommand
- Essential for package developers and users who need to modify existing LaTeX commands
- Particularly useful in document classes and packages where precise macro manipulation is required
- Helps prevent unexpected behavior when redefining LaTeX commands
Getting Started
To use letltxmacro
, include it in your document preamble:
\documentclass{article}
\usepackage{letltxmacro}
This package is primarily intended for package authors. The main functionality is:
% Properly redefine LaTeX macros
\LetLtxMacro{\NewCommand}{\OldCommand}
Examples
Redefining a LaTeX command that has optional arguments.
% Original behavior of \section
\section{Original Section}
% Now redefine \section to add a star automatically
\LetLtxMacro{\originalsection}{\section}
\renewcommand{\section}[1]{\originalsection*{#1}}
% The redefined \section now always creates unnumbered sections
\section{Modified Section}
\section{Another Modified Section}
Safely redefining a robust command defined with \DeclareRobustCommand.
% Create a robust command
\DeclareRobustCommand{\mycommand}[2][default]{Command with #1 and #2}
% Original behavior
\mycommand{mandatory}
\mycommand[optional]{mandatory}
% Redefine safely using \LetLtxMacro
\LetLtxMacro{\oldmycommand}{\mycommand}
\renewcommand{\mycommand}[2][default]{\textbf{Modified: }\oldmycommand[#1]{#2}}
% Modified behavior
\mycommand{mandatory}
\mycommand[optional]{mandatory}