다른 매크로 내에 정의된 로컬 명령 패치

다른 매크로 내에 정의된 로컬 명령 패치

이것은 후속 질문이며, 각각 (아직) 답변되지 않은 질문으로 이어진 주요 원인입니다.\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}

관련 정보