For some reason the question Reference to a counter was marked as a duplicate of this one.
There you can read:
Certainly a stupid question. I have a newcounter defined normaly, that is
\newcounter{sharc}
How can I call a specific value of this counter with a command like label/ref ?
Exemple
blabla `\thesharc` blala
blabla `\thesharc` blabla
blabla `\thesharc` blabla <- I want to refer to this value
blabla `\thesharc` blabla
Seems finding questions marked as duplicate and answers to them is not easy in StackExchange. Therefore I post my answer to that question here as well:
What do you mean by "refer to a value"?
Do you wish to obtain only the plain counter value in arabic numerals even if the counter in question is subordinated to whatsoever other counters and thus \the<counter>
is defined to prepend whatsoever prefix and apply whatsoever formatting when printing the value?
What about automatic creation of hyperlinks in case of using the hyperref-package?
Be that as it may.
For obtaining only the plain value in arabic numerals of the last counter that was incremented via \refstepcounter
, Heiko Oberdiek's zref package might be of interest.
(Within almost all the sectioning commands of documentclasses for LaTeX, \refstepcounter
is used for stepping counters. That's because \refstepcounter
is the macro which makes values of counters available to the \label
..\ref
-mechanism.)
For obtaining the expansion of \the<counter>
without hyperlinks in situations where the hyperref bundle/package is in use, Heiko Oberdiek's refcount package might be of interest.
(The example below requires to be compiled at least twice/requires at least two LaTeX-runs between which auxiliary files are not deleted.)
\documentclass[a4paper]{article}
% --- Layout of MWE.Has nothing to do with
% referencing-techniques.
\setlength\textwidth{\paperwidth}
\setlength\oddsidemargin{1.5cm}
\setlength\marginparwidth{\oddsidemargin}
\addtolength\marginparwidth{-2\marginparsep}%
\setlength\topmargin{\oddsidemargin}
\addtolength\textwidth{-1.5\oddsidemargin}
\addtolength\oddsidemargin{-1in}
\addtolength\oddsidemargin{-\hoffset}
\setlength\evensidemargin{\oddsidemargin}
\setlength\textheight\paperheight
\setlength\footnotesep{.333\topmargin}
\addtolength\textheight{-2\topmargin}
\addtolength\topmargin{-1in}
\addtolength\topmargin{-\voffset}
\setlength\headheight{0ex}
\setlength\headsep{0ex}
\pagestyle{empty}
\parindent=0ex
\parskip=.85\ht\strutbox
\topskip=0ex
% --- Layout done. ---
\usepackage{hyperref}
\usepackage[counter,user]{zref}
\usepackage{refcount}
\makeatletter
%% This will establish a new zref-property holding just the
%% plain value of the counter that was \refstep'ped as the
%% last one in arabic numerals:
%% (\refstepcounter is used internally by all the
%% sectioning-commands of LaTeX-documentclasses.)
\zref@newprop{MyPlainCntValue}{\the\value{\zref@getcurrent{counter}}}%
%
%% This will place both a normal label and a zref-label.
%% The zref-label will store the value of the new property.
%% Both kinds of labels are needed as zref does not yet
%% have hyperref-features.
\newcommand\Mylabel[1]{%
\zref@labelbyprops{#1}{MyPlainCntValue}%
\label{#1}%
}%
%
%% This is used for referencing saved MyPlainCntValue-property-values
%% of zref-labels:
\newcommand\MyPlainCntValueRef[1]{%
\zref[MyPlainCntValue]{#1}%
}%
\makeatother
\newcounter{sharc}[section]
\renewcommand\thesharc{\thesection.\arabic{sharc}}
\begin{document}
\section{Within the preamble define the label-placement- and
referencing-macros for referencing the plain values
of counters in arabic numerals.}
\begin{verbatim}
\usepackage{hyperref}
\usepackage[counter,user]{zref}
\usepackage{refcount}
\makeatletter
%% This will establish a new zref-property holding just the
%% plain value of the counter that was \refstep'ped as the
%% last one in arabic numerals:
%% (\refstepcounter is used internally by all the
%% sectioning-commands of LaTeX-documentclasses.)
\zref@newprop{MyPlainCntValue}{\the\value{\zref@getcurrent{counter}}}%
%
%% This will place both a normal label and a zref-label.
%% The zref-label will store the value of the new property.
%% Both kinds of labels are needed as zref does not yet
%% have hyperref-features.
\newcommand\Mylabel[1]{%
\zref@labelbyprops{#1}{MyPlainCntValue}%
\label{#1}%
}%
%
%% This is used for referencing saved MyPlainCntValue-property-values
%% of zref-labels:
\newcommand\MyPlainCntValueRef[1]{%
\zref[MyPlainCntValue]{#1}%
}%
\makeatother
\end{verbatim}
\section{Define the \texorpdfstring\texttt\empty{sharc}-counter
and just for fun bind it to the
\texorpdfstring\texttt\empty{section}-counter}
\begin{verbatim}
\newcounter{sharc}[section]
\renewcommand\thesharc{\thesection.\arabic{sharc}}
\end{verbatim}
\section{Within the document-environment step the
\texorpdfstring\texttt\empty{sharc}-counter via
\texorpdfstring{\protect\newline}\empty
\texorpdfstring{\texttt{\string\refstepcounter}}%
{\textbackslash refstepcounter},
and via
\texorpdfstring{\texttt{\string\Mylabel}}%
{\textbackslash Mylabel}
place the referencing-labels.}%
(\verb|\refstepcounter| is used internally by all the
sectioning-commands of \LaTeX-documentclasses.)
\verb|blabla \refstepcounter{sharc}\thesharc\ blabla|:\\
blabla \refstepcounter{sharc}\thesharc\ blabla
\verb|blabla \refstepcounter{sharc}\thesharc\ blabla|:\\
blabla \refstepcounter{sharc}\thesharc\ blabla
\verb|blabla \refstepcounter{sharc}\thesharc\Mylabel{INeedReferenceToThis} blabla|:\\
blabla \refstepcounter{sharc}\thesharc\Mylabel{INeedReferenceToThis} blabla
\\\emph{(This is to be referenced/linked. Therefore the}
\verb|\Mylabel|\emph{-command was used for placing both a
normal label and a} \textsf{\textbf{zref}}\emph{-label.
For testing the links view the resulting pdf at a
magnification/zoom-factor where scrolling within the window
where the pdf is displayed is needed.)}
\verb|blabla \refstepcounter{sharc}\thesharc\ blabla|:\\
blabla \refstepcounter{sharc}\thesharc\ blabla
\newpage
\section{Referencing the label}%
\verb|\ref{INeedReferenceToThis}|
yields:
\ref{INeedReferenceToThis}
{(\verb|\ref| comes from the \LaTeXe-kernel but may be
redefined by some package. This produces the number with all
prefixes from other counters. When using the
\textsf{\textbf{hyperref}}-bundle, this also does
produce a hyperlink to the anchor automatically produced by
\verb|\refstepcounter|.)}
\hrulefill
\verb|\makeatletter|\\
\verb|\getrefbykeydefault{INeedReferenceToThis}%|\\
\verb| {}%|\\
\verb| {\refused{INeedReferenceToThis}\nfss@text{\reset@font\bfseries??}}%|\\
\verb|\makeatother|
yields:
\makeatletter
\getrefbykeydefault{INeedReferenceToThis}%
{}%
{\refused{INeedReferenceToThis}\nfss@text{\reset@font\bfseries??}}%
\makeatother
{(\verb|\getrefbykeydefault| comes from the
\textsf{\textbf{refcount}}-package. This produces the number
with all prefixes from other counters. Even when using the
\textsf{\textbf{hyperref}}-bundle, this does not produce a
hyperlink to the anchor automatically produced by
\verb|\refstepcounter|.)}
\hrulefill
\verb|\MyPlainCntValueRef{INeedReferenceToThis}|
yields:
\MyPlainCntValueRef{INeedReferenceToThis}
{(\verb|\MyPlainCntValueRef| is defined by
means of macros from the \textsf{\textbf{zref}}-package.
This produces just the plain number in arabic numerals without
any prefixes from other counters. Even when using the
\textsf{\textbf{hyperref}}-bundle, this does not produce
a hyperlink to the anchor automatically produced by
\verb|\refstepcounter|.)}
\hrulefill
\verb|\hyperref[INeedReferenceToThis]{\MyPlainCntValueRef{INeedReferenceToThis}}|
yields:
\hyperref[INeedReferenceToThis]{\MyPlainCntValueRef{INeedReferenceToThis}}
{\sloppy(\verb|\hyperref| comes from the
\textsf{\textbf{hyperref}}-bundle. Therefore this only works
when the \textsf{\textbf{hyperref}}-bundle is loaded.
\verb|\MyPlainCntValueRef| is defined by means of macros
from the \textsf{\textbf{zref}}-package. This produces just
the plain number in arabic numerals without any prefixes from
other counters. This does also produce a hyperlink to the
anchor automatically produced by
\verb|\refstepcounter|.)
}
\hrulefill
In expansion contexts you can use \textsf{\textbf{zref}}'s \verb|\zref@extractdefault|
for obtaining just the plain number - here the plain number delivered by
\verb|\zref@extractdefault| will be used by \verb|\romannumeral| for delivering
roman numerals instead of arabic numerals:
\verb|\makeatletter|\\
\verb|\expandafter\@firstofone|\\
\verb|\expandafter{%|\\
\verb| \romannumeral|\\
\verb| \zref@extractdefault{INeedReferenceToThis}%|\\
\verb| {MyPlainCntValue}%|\\
\verb| {0 \zref@refused{INeedReferenceToThis}\nfss@text{\reset@font\bfseries??}}%|\\
\verb|}%|\\
\verb|\makeatother|
yields:
\makeatletter
\expandafter\@firstofone
\expandafter{%
\romannumeral
\zref@extractdefault{INeedReferenceToThis}%
{MyPlainCntValue}%
{0 \zref@refused{INeedReferenceToThis}\nfss@text{\reset@font\bfseries??}}%
}%
\makeatother
\end{document}

1
when it is the second implication? Shouldn't it be2
?