# Why is \patchcmd not working when also using \@ifclassloaded? [duplicate]

Why do these two MWEs produce different effects?

\documentclass{article}
\usepackage{etoolbox}
\makeatletter
\def\aechanged{Not changed}
\patchcmd{\l@section}
{#1\nobreak\hfil}
{#1\nobreak
\xleaders\hbox{$\m@th \mkern \@dotsep mu\hbox{--}\mkern \@dotsep mu$}
\hfil}
{\def\aechanged{Yes}}
{\def\aechanged{No}}%
\makeatother
\pagestyle{empty}
\begin{document}

\begin{tabular}{ll}
Did \verb=\patchcmd=  work?   & \aechanged
\end{tabular}

\end{document}


which generates:

But,

\documentclass{article}
\usepackage{etoolbox}
\makeatletter
\def\aechanged{Not changed}
{\patchcmd{\l@section}
{#1\nobreak\hfil}
{#1\nobreak
\xleaders\hbox{$\m@th \mkern \@dotsep mu\hbox{--}\mkern \@dotsep mu$}
\hfil}
{\def\aechanged{Yes}}
{\def\aechanged{No}}%
\makeatother
\pagestyle{empty}
\begin{document}

\begin{tabular}{ll}
Did \verb=\patchcmd=  work?   & \aechanged
\end{tabular}

\end{document}


generates:

## marked as duplicate by Werner, Guido, Heiko Oberdiek, Andrew Swann, Ulrich SchwarzJun 19 '13 at 6:20

• You can issue \tracingpatches in the preamble and etoolbox debugs the patching process to the log. Removing #1 from the input will work in this case, \nobreak\hfil only appears once in the definition of \l@section anyway. Using a conditional in \@ifclassloaded and then using that conditional for \patchcmd does work. – Qrrbrbirlbel Jun 19 '13 at 1:43

You can let etoolbox debug the patching process by issuing \tracingpatches in the preamble. The log will contain useful (?) information why the patching failed.

With your code the log will contain:

[debug] tracing \patchcmd on input line 18
[debug] analyzing '\l@section'
[debug] ++ control sequence is defined
[debug] ++ control sequence is a macro
[debug] -- nested patching command and parameters in patch
[debug] -> the patching command seems to be nested in the
[debug]    argument to some other command
[debug] -> the patch text seems to contain # characters
[debug] -> either avoid nesting or use # characters with
[debug]    category code 12 in the patch text
[debug] -> simply doubling the # characters will not work


(The macro \@ifclassloaded will simply issue \@firstoftwo or \@secondoftwo.)

If one would be using \catcode\#=12 for the \patchcmd macro one would, of course, get:

[debug] -> the macro may have been defined under a category
[debug]    code regime different from the current one


Avoiding nesting seems easier (besides not using #1 at all).

## Code

\documentclass{article}
\usepackage{etoolbox}
\makeatletter
\def\aechanged{Not changed}
\tracingpatches
\if@tempswa
\patchcmd{\l@section}
{#1\nobreak\hfil}
{#1\nobreak
\xleaders\hbox{$\m@th \mkern \@dotsep mu\hbox{--}\mkern \@dotsep mu$}
\hfil}
{\def\aechanged{Yes}}
{\def\aechanged{No}}%
\fi
\makeatother
\pagestyle{empty}
\begin{document}
\begin{tabular}{ll}
`