0

I'm using the cleveref package with some customizations, using \creflabelformat. Because cleveref should be the last package to load, and because the customization commands should come after cleveref loads, I'd like to have a command \loadCleveref that loads cleveref and executes the customizations. That way I can define \loadCleveref in my standard custom package (even though cleveref isn't yet loaded) and then call \loadCleveref near the end of the document's preamble.

A problem arises when I call \creflabelformat{snipCounter}{(S#2#1#3)} from within \loadCleveref: I get Illegal parameter number in definition of \loadCleveref which appears to be complaining about the # characters. Presumably, it's confused by them, thinking that I'm referring to nonexistent arguments of \loadCleveref`.

Is there a way to execute this \creflabelformat{snipCounter}{(S#2#1#3)} command from within a macro?

First, what does work (i.e., when I don't try to load cleveref from a command):

\documentclass{article}
%\newcommand{\loadCleveref}{%
    \usepackage[nameinlink,noabbrev]{cleveref}
    \crefname{snipCounter}{snippet}{snippets}
    \Crefname{snipCounter}{Snippet}{Snippet}
    \creflabelformat{snipCounter}{(S#2#1#3)}
%}
%\loadCleveref
\newcounter{snipCounter}
\newenvironment{codeSnip}%
    {\refstepcounter{snipCounter}\begin{minipage}[c]{0.8\textwidth}}%
    {\end{minipage}\begin{minipage}[c]{0.2\textwidth}\hspace*{\stretch{1}}(S\thesnipCounter)\end{minipage}%
    \bigskip
    }%
\begin{document}
\begin{codeSnip}
\label{aSnippetLabel}
\texttt{A code snippet}
\end{codeSnip}
\Cref{aSnippetLabel} is a snippet of code.
\end{document}

which produces the output: enter image description here

The error occurs when I un-comment the \newcommand{\loadCleveref} and \loadCleveref commands like:

\documentclass{article}
\newcommand{\loadCleveref}{%
    \usepackage[nameinlink,noabbrev]{cleveref}
    \crefname{snipCounter}{snippet}{snippets}
    \Crefname{snipCounter}{Snippet}{Snippet}
    \creflabelformat{snipCounter}{(S#2#1#3)}
}
\loadCleveref
\newcounter{snipCounter}
\newenvironment{codeSnip}%
    {\refstepcounter{snipCounter}\begin{minipage}[c]{0.8\textwidth}}%
    {\end{minipage}\begin{minipage}[c]{0.2\textwidth}\hspace*{\stretch{1}}(S\thesnipCounter)\end{minipage}%
    \bigskip
    }%
\begin{document}
\begin{codeSnip}
\label{aSnippetLabel}
\texttt{A code snippet}
\end{codeSnip}
\Cref{aSnippetLabel} is a snippet of code.
\end{document}
  • 3
    It'll work if you replace every # in the definition of \loadCleveref by ##. A single #<number> is interpreted as an argument of \loadCleveref (but it doesn't have any) and ##<number> is interpreted as #<number> would otherwise be interpreted. (I hope that makes sense.) See e.g. this answer. – Circumscribe Jan 11 at 16:53
  • Perfect. If you make this an answer, I'll accept it. – Jim Ratliff Jan 11 at 17:29
2

It'll work if you replace every # in the definition of \loadCleveref by ##.

Inside a macro definition, #<number> is interpreted as an argument of the command being defined (but \loadCleveref doesn't have any arguments), and ##<number> is interpreted as #<number> would be interpreted outside the definition.

More information about this can for instance be found in this answer.


Here is your MWE with # replaced by ##:

\documentclass{article}
\newcommand{\loadCleveref}{%
    \usepackage[nameinlink,noabbrev]{cleveref}
    \crefname{snipCounter}{snippet}{snippets}
    \Crefname{snipCounter}{Snippet}{Snippet}
    \creflabelformat{snipCounter}{(S##2##1##3)}
}
\loadCleveref
\newcounter{snipCounter}
\newenvironment{codeSnip}%
    {\refstepcounter{snipCounter}\begin{minipage}[c]{0.8\textwidth}}%
    {\end{minipage}\begin{minipage}[c]{0.2\textwidth}\hspace*{\stretch{1}}(S\thesnipCounter)\end{minipage}%
    \bigskip
    }%
\begin{document}
\begin{codeSnip}
\label{aSnippetLabel}
\texttt{A code snippet}
\end{codeSnip}
\Cref{aSnippetLabel} is a snippet of code.
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.