# Special section names and counter in memoir with cleveref

I'm trying to have separately numbered "Resource" sections in a memoir document, but have produced clevereferences that refer to the "current" Resource counter value instead of the referenced Resource's counter value.

I don't think this is due to the incompatibilities between memoir and titlesec (see the edit, below).

I'm open to non-titlesec solutions, but I do require memoir for the document as a whole. Here's a MWE.

\documentclass[11pt]{memoir}

\usepackage{titlesec}

\newcounter{resourcecounter}
\setcounter{resourcecounter}{0}

\newcommand{\resource}[2]{
\stepcounter{resourcecounter}
\titleformat{\section}{\Large\sffamily\bfseries}{Resource \arabic{resourcecounter}}{1ex}{}
\section[Resource \arabic{resourcecounter}: #1]{#2}
\renewcommand*\thesubsection{\arabic{resourcecounter}.\arabic{subsection}}
\titleformat{\subsection}{\sffamily}{Resource \thesubsection}{1ex}{}
}

\crefname{resource}{Resource}{Resources}
\creflabelformat{resource}{#2\arabic{resourcecounter}#3}
\crefname{resource:sub}{Resource}{Resources}

\begin{document}

\section{Baz}

Foo \cref{res:bar}.

\resource{Bar}{Bar}
\label[resource]{res:bar}

Bar \cref{res:bar}.

\end{document}


Note that I have found several related issues, none of which has been exactly the same, AFAICT.

## Edit

I've confirmed that the same numbering issue remains when titlesec and \titleformat are removed. It seems to have something to do with cleveref and declaring a \section with a custom label inside the \resource macro.

• Should you use \refstepcounter{resourcecounter} instead of \stepcounter{resourcecounter}? – Peter Wilson Sep 1 '18 at 19:16
• @PeterWilson, it doesn't seem to have any effect. – Rico Picone Sep 1 '18 at 19:41

After isolating the problem to references in sections (note: this also applies when using nameref if there's a counter in the title), I found this clever answer to a very related problem. I adapted this solution (which didn't use a section macro) and developed the following more minimal, actually working example.

\documentclass[11pt]{memoir}

\newcounter{resourcecounter}

\newcommand{\resourcer}[1]{% this is the magic
\refstepcounter{resourcecounter}\label[resource]{#1}%
}

\newcommand{\resource}[3]{
\resourcer{#3} % using the magic
\section[Resource \theresourcecounter: #1]{Resource \theresourcecounter: #2}
}

\usepackage{cleveref}
\crefname{resource}{Resource}{Resources}
\creflabelformat{resource}{#2#1#3}

\begin{document}

\section{Foo}

See \cref{res:bar}.

\resource{Bar}{Bar}{res:bar} % note how labeled in macro

See \cref{res:bar}.

\section{Foo}

See \cref{res:bar}.

\resource{Baz}{Baz}{res:baz}

See \cref{res:bar}.

\end{document}
`

The result is