Do all reference-able commands/environments have counters?

Most of the things that can be referenced, e.g. sectioning commands, figures, tables, theorems, etc, appear to have counters associated with them that the LaTeX referencing system uses to keep track of everything. Is this the case for all reference-able things?

Following on from my earlier questions, I'm trying to think of a way I could collect a list of environments in a manner that would allow them to be referenced later on in the document. One of the requirements for the environment definition is—and this is the difficult bit—that the numbering needs to be configurable by the package user.

I'm contemplating having two counter-like things, one of which is the "real" counter used for references, and the other is the "display" counter that the user will customise. When a reference is made to an instance of one of these environments, internally LaTeX should use the former counter, but display the latter one in labels. It's all a bit vague at the moment, and admittedly I don't have an MWE yet, but 90% of coding is thinking and planning, and I don't know enough to know if what I seek is possible.

• No, as soon you define \@currentlabel or \@currentlabelname and place a \label 'right' after it, you will have a valid label, that has nothing to do with a counter – user31729 Dec 18 '17 at 6:07

The usual mechanism for labels exploit \@currentlabel, a 'temporary' macro that is redefined each time when \refstepcounter is used. Basically \@currentlabel contains the expanded \theX value after stepping the counter X.

But \@currentlabel can be used basically anywhere to contain 'anything' and a following \label (inside the same group) will grab this \@currentlabel value, in the very end it is no good idea to rely that a label name is really connected to a counter.

The same is true for \@currentlabelname which holds usually the name of a section when \nameref is used.

The \autoref and all other hyperref related macros however relies on hyper anchors, so using a \phantomsection here is appropriate.

For more information on extraction of counter information you can use refcount, zref from Heiko Oberdiek or my 'short' package crossreftools which will be updated within the next days.

The example below is rather 'academic', of course:

\documentclass{article}

\usepackage{blindtext}
\usepackage{hyperref}
\makeatletter
\newcommand{\fakelabel}[2]{%
\phantomsection%
\protected@edef\@currentlabel{#1}%
\protected@edef\@currentlabelname{#2}#1%
}
\makeatother

\begin{document}

\begin{tabular}{ll}
\fakelabel{This is a table cell}{But is given a different name}\label{table:cell}
\end{tabular}

\clearpage
See \ref{table:cell} or \nameref{table:cell}

\end{document}


With crossreftools v0.4

There is a macro \crtcrossreflabel that does the fake labeling at 'once':

\documentclass{article}

\usepackage{blindtext}
\usepackage{hyperref}
\usepackage{crossreftools}

\begin{document}

\begin{tabular}{ll}
\crtcrossreflabel[But is given a different name]{This is a table cell}[table:cell]
\end{tabular}

\clearpage
See \ref{table:cell} or \nameref{table:cell}
\end{document}


crossreftools v0.8 is available from CTAN, TL and MikTeX.