Reference to a counter

1,789

Solution 1

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}

page 1 of resulting output page 2 of resulting output

Solution 2

You are on the right way,

\newcounter{sharc}

is the right start. Then you can set it to val (a number, not a counter) with:

\setcounter{sharc}{val}

If you want to set it to a counter cnt, you can use:

\setcounter{sharc}{\value{cnt}}

To increase it, use:

\stepcounter{sharc}

In order to reference it, too, please use, as mentioned already in some comments by NBur and Andrew,

\refstepcounter{sharc}
\label{labelname}

However, this does not display the counter. So you need to display it by using one of the following:

\arabic{sharc}
\alph{sharc}
\Alph{sharc}
\roman{sharc}
\Roman{sharc}

All of these infos you can find in the wikibook here.

Combining it all:

result

\documentclass{article}
\newcounter{sharc}
\setcounter{sharc}{2} % set initial value
\renewcommand{\thesharc}{\roman{sharc}} % set references to \roman
\newcommand{\sharcref}[1]{[\ref{#1}]} % reference in brackets

\begin{document}
    initial value: \arabic{sharc} (\Roman{sharc})

    \stepcounter{sharc} % add one
    next value: \arabic{sharc} (\alph{sharc})

    \refstepcounter{sharc} % add one and reference this one
    labeled value: \arabic{sharc} [\roman{sharc}]
    \label{l:sharc}

    this is the label: \ref{l:sharc}\par
    or with the defined command: \sharcref{l:sharc}
\end{document}
Share:
1,789

Related videos on Youtube

cyrille piatecki
Author by

cyrille piatecki

Updated on June 28, 2020

Comments

  • cyrille piatecki
    cyrille piatecki over 3 years

    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
    
    • NBur
      NBur over 5 years
      To help us help you, please provide a minimal working example (MWE)! You have to use \refstepcounter to increase the counter value.
    • Admin
      Admin over 5 years
      Use \refstepcounter{sharc} followed by \label{mylabel} and then \ref{mylabel}...
    • Ulrich Diez
      Ulrich Diez over 5 years
      @cyrille.piatecki Seems the question Reference an environment counter through label in another counter referenced by others is about prepending a reference to a specific counter value when printing another counter value. Your question is about creating a reference. As prepending a reference implies creating a reference, one can consider your question a duplicate. Nonetheless in these questions different goals are expressed. Therefore I do not take your question for a duplicate.
  • Phelype Oleinik
    Phelype Oleinik over 5 years
    It's worth mentioning for newbies that in \setcounter{sharc}{val}, val is a number, not a counter named val.
  • nox
    nox over 5 years
    @PhelypeOleinik you're right, thanks for mentioning, I edited the answer to include setting it to a counter and clarified that.