# How can I create context-sensitive reference names for an environment which defines label prefixes on-the-fly?

Background: I asked a rather muddled but related question earlier. I received an excellent answer from Manuel to that question but my confusion about the problem means that I unfortunately asked the wrong question. So this is a different question, despite the similar content, which I hope may be a right question, even if not the right question to ask.

I am trying to create a new environment to typeset an enumerated list. The environment is designed to take one mandatory argument, typically a single letter. This is used as a prefix for items in the list. So if the letter is a, the items will be labelled a1, a2 and so on. The labels are typeset so that the letter is in small-caps. References to items should also use small-caps except that sometimes I want the letter set in upper-case e.g. at the beginning of a sentence.

I have been trying to combine cleveref and enumitem to achieve this result. The labels within the list are unproblematic since label=\textsc{<letter>}\arabic* does the job nicely.

In order to facilitate different formats at the beginning of sentences, however, I've used enumitem's ref option alongside label. I then would like to have \Cref and \cref format the label appropriately. However, I cannot figure out how to do this.

If I include the letter in the item's reference e.g. ref=<letter>\arabic*, then I can use \creflabelformat{<type>}{<format>} to typeset the label in, say, small-caps. But this isn't sensitive to context. \Cref and \cref assume that any difference in capitalisation will affect the name associated with the reference type and not the content of the specific item reference itself.

For this reason, I'm using ref=\arabic* and attempting to set the letter prefix using \crefname and \Crefname. However, this means that I need to create custom formats using \crefname and \Crefname inside my custom environment. The problem is that these definitions are not global so they are not defined when I use \cref or \Cref elsewhere in the document.

How can I achieve the desired result without predefining \crefname and \Crefname for the letters I need in advance?

\documentclass{article}
\usepackage{xparse,enumitem,cleveref,ebgaramond}
\NewDocumentEnvironment{lettered}{ O {} m }{%
\let\labelorig\label
\def\label##1{\labelorig[lettered#2]{##1}}%
\crefname{lettered#2}{\textsc{#2}}{\textsc{#2}}%
\Crefname{lettered#2}{\MakeUppercase{#2}}{\MakeUppercase{#2}}%
\begin{enumerate}[label=\textsc{#2}\arabic*., ref=\arabic*, #1]
}{%
\end{enumerate}%
}
\begin{document}
Some examples:
\begin{lettered}{t}
\item\label{t:thing} A thing.
\item\label{t:another} Another thing.
\end{lettered}
\begin{lettered}{c}
\item\label{c:claim} A claim.
\item\label{c:different} A different claim.
\end{lettered}
The actual output is the next sentence.
\Cref{t:thing} and \cref{t:another} are the subject of claims \cref{c:claim} and \cref{c:different}.
The desired output is the next sentence.
T1 and \textsc{t}2 are the subject of claims \textsc{c}1 and \textsc{c}2.
\end{document}


OK. I'm an idiot. I can stare at something all afternoon but only find the answer 30 seconds after I post a question.

It turns out that cleveref's documentation just has a weird idea of what constitutes a 'single' reference. I naïvely assumed that Single Cross-References meant cross-reference tokens and that

Cross-reference formats for single cross-references are defined or redefined using the \crefformat and \Crefformat commands ...

introduced a discussion about typesetting cross-reference tokens.

But it turns out that the documentation is referring to single types of cross-references rather than single tokens of cross-references, as I'd assumed. And that means that I can simply use \crefformat and \Crefformat with a single type of cross-reference for all labels created within my custom environment:

\documentclass{article}
\usepackage{xparse,enumitem,cleveref,ebgaramond}
\NewDocumentEnvironment{lettered}{ O {} m }{%
\let\labelorig\label
\def\label##1{\labelorig[lettered]{##1}}%
\begin{enumerate}[label=\textsc{#2}\arabic*., ref=#2\arabic*, #1]
}{%
\end{enumerate}%
}
\crefformat{lettered}{#2\textsc{#1}#3}
\Crefformat{lettered}{#2\MakeUppercase{#1}#3}
\begin{document}
Some examples:
\begin{lettered}{t}
\item\label{t:thing} A thing.
\item\label{t:another} Another thing.
\end{lettered}
\begin{lettered}{c}
\item\label{c:claim} A claim.
\item\label{c:different} A different claim.
\end{lettered}
The actual output is the next sentence.
\Cref{t:thing} and \cref{t:another} are the subject of claims \cref{c:claim} and \cref{c:different}.
The desired output is the next sentence.
T1 and \textsc{t}2 are the subject of claims \textsc{c}1 and \textsc{c}2.
\end{document}


And here is the result of typesetting a single cross-reference type involving four cross-reference tokens: