Created
January 4, 2016 19:24
-
-
Save kmaed/27fbf2491bcf836de095 to your computer and use it in GitHub Desktop.
etoolbox のパッチ関係コマンドを自作のマクロ内に入れると……
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\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} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
biblatex では不用意に \DeclareBibliographyExtras 内にパラメータ入りのパッチを書くと失敗するというさらなる罠が…….
どうせ \scantokens されるので \catcode`#=12 しておくのが正解(という理解でいいのか?).
参考:
http://tex.stackexchange.com/questions/113147/patching-arguments-inside-a-macro