Skip to content

Instantly share code, notes, and snippets.

@kmaed
Created January 4, 2016 19:24
Show Gist options
  • Save kmaed/27fbf2491bcf836de095 to your computer and use it in GitHub Desktop.
Save kmaed/27fbf2491bcf836de095 to your computer and use it in GitHub Desktop.
etoolbox のパッチ関係コマンドを自作のマクロ内に入れると……
\documentclass{minimal}
\usepackage[T1]{fontenc}
\usepackage{etoolbox}
\makeatletter
% パッチを当てて,失敗したときだけ定型のエラーメッセージを出したいとする.
% いちいち面倒なのでマクロ化する.
\newrobustcmd*{\mypatchcmdi}[3]{% 一見良さそうだが,実はダメ.
\patchcmd{#1}{#2}{#3}{}{\message{^^JError!: \string#1, #2, #3^^J}}}
\long\def\mypatchcmdii@#1#2#3#4{#1{#2}{#3}{#4}{}{\message{^^JError!: \string#2, #3, #4^^J}}}
\newrobustcmd*{\mypatchcmdii}{% これならOK.
\etb@dbg@trce\mypatchcmdii
\begingroup
\@makeother\#%
\mypatchcmdii@\etb@patchcmd}
\makeatother
\begin{document}
\def\hoge{a}
\mypatchcmdi{\hoge}{a}{b}
\meaning\hoge % OK
\mypatchcmdii{\hoge}{b}{a}
\meaning\hoge % OK
\def\hoge#1#2{#1}
\mypatchcmdi{\hoge}{#1}{#2}
\meaning\hoge % NG!! #1, #2 が ##1, ##2 に化けてしまう.
\mypatchcmdii{\hoge}{#1}{#2}
\meaning\hoge % OK
\end{document}
@kmaed
Copy link
Author

kmaed commented Jan 5, 2016

biblatex では不用意に \DeclareBibliographyExtras 内にパラメータ入りのパッチを書くと失敗するというさらなる罠が…….
どうせ \scantokens されるので \catcode`#=12 しておくのが正解(という理解でいいのか?).

参考:
http://tex.stackexchange.com/questions/113147/patching-arguments-inside-a-macro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment