
Это следующий вопрос, соответственно основная причина, которая привела к (пока) неотвеченному вопросу:\xpatchcmd определяет макросы при применении к неопределенным командам
Я попытался исправить некоторую команду, скажем, \someothercommand
, которая определена локально внутри другого макроса, скажем, \somecommand
, за пределами последующего макроса, например, в преамбуле.
Однако это не удается. Исправление переходит кЛОЖЬфилиал. Подозреваю, это как-то связано с локальной группировкой.
Это может произойти, когда требуется исправить определения макросов, которые скрываются внутри других макросов, например, в файле класса или пакета.
Рассмотрим случай, когда некоторыеплохойкод следует изменять без копирования всего внешнего макроса или редактирования .cls
файла.sty
Возможно ли вообще исправление таких локально определенных макросов?
Я упростил MWE
\documentclass{article}
\usepackage{xcolor}
\usepackage{xpatch}
\newcommand{\somecommand}[1]{%
\newcommand{\someothercommand}[1]{\textcolor{blue}{##1}}%
% This works
% \xpatchcmd{\someothercommand}{blue}{red}{\typeout{Success!}}{\typeout{Nobody expects the Spanish Inquisition!!!!}}
Inside usage: \someothercommand{#1}%
}
% Patching fails
\xpatchcmd{\someothercommand}{blue}{red}{\typeout{Success!}}{\typeout{Nobody expects the Spanish Inquisition!!!!}}
\begin{document}
\somecommand{Outer command}
Outside usage: \someothercommand{Inside} but should be \textcolor{red}{Inside}
\end{document}
решение1
Я бы поискал подходящее место для \somecommand
установки крючка:
\documentclass{article}
\usepackage{xcolor}
\usepackage{xpatch}
\newcommand{\somecommand}[1]{%
\def\someothercommand##1{\textcolor{blue}{##1}}%
Inside usage: \someothercommand{#1}%
}
\xpatchcmd{\somecommand}{Inside usage:}{Inside usage: \myspecialsetup}{\typeout{Success!}}{\typeout{Nobody expects the Spanish Inquisition!!!!}}
\newcommand\myspecialsetup{\renewcommand\someothercommand[1]{\textcolor{red}{##1}}}
\begin{document}
\somecommand{Outer command}
\end{document}
решение2
Вы можете исправить только те команды, которые были определены, поэтому \somecommand
вот:
\documentclass{article}
\usepackage{xcolor}
\usepackage{xpatch}
\newcommand{\somecommand}[1]{%
\newcommand{\someothercommand}[1]{\textcolor{blue}{##1}}%
% This works
% \xpatchcmd{\someothercommand}{blue}{red}{\typeout{Success!}}{\typeout{Nobody expects the Spanish Inquisition!!!!}}
Inside usage: \someothercommand{#1}%
}
% Patching works
\xpatchcmd{\somecommand}{blue}{red}{\typeout{Success!}}{\typeout{Nobody expects the Spanish Inquisition!!!!}}
\begin{document}
\somecommand{Outer command}
Outside usage: \someothercommand{Inside} but should be \textcolor{red}{Inside}
\end{document}