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}
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}


#in the definition of\loadCleverefby##. 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 '19 at 16:53