Etoolboxの\ patchcmdは、hyperrefパッケージが原因で検索パターンを見つけることができません

Aug 22 2020

私はreportクラスを使用し、このサイトの他の質問(LoFの章ごとの図の間隔を削除するなど)から、LoFの章ごとの図の間隔を調整する方法を学びました。伝統を変えたい

\addtocontents{lof}{\protect\addvspace{10\p@}}

report.clsファイルで

\addtocontents{lof}{\protect\addvspace{15\p@}} このパッチを使用することによって:

\makeatletter
\patchcmd{\@chapter}%
  {\addtocontents{lof}{\protect\addvspace{10\p@}}}% <search>
  {\addtocontents{lof}{\protect\addvspace{15\p@}}}% <replace>
  {}{}% <success><failure>
\makeatother

ただし、発行\tracingpatchesすることにより、次のことがわかります。

[debug] analyzing '\@chapter'
[debug] ++ control sequence is defined
[debug] ++ control sequence is a macro
[debug] ++ macro can be retokenized cleanly
[debug] -- search pattern not found in replacement text

私は何か間違ったことをしていますか?\patchcmd指定されたコードが見つからない理由がわかりません。ちなみに、私のドキュメントには他に3つのパッチ(変更なし\@chapter)があり、それらはすべて正常に機能します。

編集: MWEを準備しているときに、パッケージをhyperref削除すると問題が解決するため、パッケージが原因であることに気付きました。

\documentclass[12pt,twoside,openright]{report}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{lmodern}
\usepackage{microtype}

\usepackage{etoolbox}

\usepackage[pdftex,colorlinks,linkcolor=blue,citecolor=red]{hyperref}

%----------------------- Patch -----------------------
\tracingpatches
\makeatletter
\patchcmd{\@chapter}%        
  {\addtocontents{lof}{\protect\addvspace{10\p@}}}% <search>
  {\addtocontents{lof}{\protect\addvspace{15\p@}}}% <replace>
  {}{}% <success><failure>
\makeatother
%-----------------------------------------------------


\begin{document}
\listoffigures
\chapter{A chapter}
\begin{figure}\caption{A figure}\end{figure}
\begin{figure}\caption{A figure}\end{figure}
\chapter{A chapter}
\begin{figure}\caption{A figure}\end{figure}
\begin{figure}\caption{A figure}\end{figure}
\appendix
\chapter{A chapter}
\addtocontents{lof}{\protect\addvspace{10pt}}%
\begin{figure}\caption{A figure}\end{figure}
\begin{figure}\caption{A figure}\end{figure}
\end{document}

これを修正する方法はありますか?hyperrefパッケージをロードし続ける必要があります。

回答

3 moewe Aug 22 2020 at 12:10

hyperref そのことを行うには、非常に多くの内部コマンドを定義する必要があります。

この場合、の元の定義はによって\@chapter保存さ\Hy@org@chapterhyperref\@chapter次に再定義されてリンクを実行してから、を呼び出します\Hy@org@chapter

したがって、ここでの解決策は、ロードされるときでは\Hy@org@chapterなくパッチを適用\@chapterすることhyperrefです。

\documentclass[12pt,twoside,openright]{report}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{lmodern}
\usepackage{microtype}

\usepackage{etoolbox}

\usepackage[pdftex,colorlinks,linkcolor=blue,citecolor=red]{hyperref}

%----------------------- Patch -----------------------
\tracingpatches
\makeatletter
\patchcmd{\Hy@org@chapter}%        
  {\addtocontents{lof}{\protect\addvspace{10\p@}}}% <search>
  {\addtocontents{lof}{\protect\addvspace{15\p@}}}% <replace>
  {}{}% <success><failure>
\makeatother
%-----------------------------------------------------


\begin{document}
\listoffigures
\chapter{A chapter}
\begin{figure}\caption{A figure}\end{figure}
\begin{figure}\caption{A figure}\end{figure}
\chapter{A chapter}
\begin{figure}\caption{A figure}\end{figure}
\begin{figure}\caption{A figure}\end{figure}
\appendix
\chapter{A chapter}
\addtocontents{lof}{\protect\addvspace{10pt}}%
\begin{figure}\caption{A figure}\end{figure}
\begin{figure}\caption{A figure}\end{figure}
\end{document}

与える

そして

[debug] tracing \patchcmd on input line 16
[debug] analyzing '\Hy@org@chapter'
[debug] ++ control sequence is defined
[debug] ++ control sequence is a macro
[debug] ++ macro can be retokenized cleanly
[debug] ++ search pattern found in replacement text
[debug] ++ patching possible
[debug] == retokenizing macro now

または、再定義を適用する前に パッチをhyperref適用してみることもできます。

\documentclass[12pt,twoside,openright]{report}

\usepackage{etoolbox}

%----------------------- Patch -----------------------
\tracingpatches
\makeatletter
\patchcmd{\@chapter}%        
  {\addtocontents{lof}{\protect\addvspace{10\p@}}}% <search>
  {\addtocontents{lof}{\protect\addvspace{15\p@}}}% <replace>
  {}{}% <success><failure>
\makeatother
%-----------------------------------------------------

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{lmodern}
\usepackage{microtype}

\usepackage[pdftex,colorlinks,linkcolor=blue,citecolor=red]{hyperref}



\begin{document}
\listoffigures
\chapter{A chapter}
\begin{figure}\caption{A figure}\end{figure}
\begin{figure}\caption{A figure}\end{figure}
\chapter{A chapter}
\begin{figure}\caption{A figure}\end{figure}
\begin{figure}\caption{A figure}\end{figure}
\appendix
\chapter{A chapter}
\addtocontents{lof}{\protect\addvspace{10pt}}%
\begin{figure}\caption{A figure}\end{figure}
\begin{figure}\caption{A figure}\end{figure}
\end{document}

おかげでデビッドカーライルことを示唆ためのコメントで。