1

I use cleveref package to format a special type of labels. Can I also use a special counter or counter formatting sequence in \cref. This is my example:

\documentclass[10pt,a4paper,twoside]{article}
\usepackage{chngcntr}
\usepackage{cleveref}
\usepackage{assoccnt}
\makeatletter
  \crefname{ap}{package}{packages}
  \Crefname{ap}{Package}{Packages}
  \creflabelformat{ap}{AP~#2#1#3}
  \crefformat{ap}{AP~#2#1#3}
\makeatother

\begin{document}

  \newcounter{ap}{}
  \DeclareAssociatedCounters{subsection}{ap}
  \counterwithout{ap}{section}

  \section{ some section  }
  \section{ special section on packages  }
  \setcounter{ap}{0}

      Here we describe \cref{sec:ap-one,sec:ap-two,sec:ap-three}  ....
      this will be presented in \cref{sec:ap-one,sec:ap-two} and 
      finally in \cref{sec:ap-three}we describe ....

    Preferably this should look like this:

      Here we describe package AP 1 to AP 3  .... 
      this will be presented in packages AP 1 and AP 2 and 
      finally in package AP 3 we describe ....

    but currently it looks like this:

      Here we describe package AP 2.1 to AP 2.3  .... 
      this will be presented in packages AP 2.1 and AP 2.2 and 
      finally in package AP 2.3 we describe ....

   \subsection{AP~\arabic{ap}: first }
   \label[ap]{sec:ap-one}
     bla bla bla
     Here we describe  ... 
     which will be used in \cref{sec:ap-two,sec:ap-three}  ....

   \subsection{AP~\arabic{ap}: second }
   \label[ap]{sec:ap-two}
     bla bla bla
     we described in \cref{sec:ap-one}  .... 
     follow in \cref{sec:ap-three}

     ap counter is \arabic{ap} or \theap.

     subsection counter is \arabic{subsection} 
     and ``thesection'' is \thesubsection.


   \subsection{AP~\arabic{ap}: three }
   \label[ap]{sec:ap-three}
     bla bla bla
     we described in \cref{sec:ap-one,sec:ap-two}  


  \section*{Appendix}
     The table of contents is fine as is 
  \tableofcontents

\end{document}
  • What kind of special counter are you trying to create? I'm not sure what you're trying to achieve. – Alenanno Nov 27 '15 at 10:17
  • @Alenanno: He uses a special associated counter, by application of assoccnt, a package written by a bloke who wrote a comment here ;-) – user31729 Nov 27 '15 at 10:24
  • \newcounter{ap}{} is wrong. Since ap is not in the reset list of section (it's an associated counter), counterwithout{ap}{section} is not necessary. cleveref should be the last package and makeatletter...\makeatotheris not necessary there! – user31729 Nov 27 '15 at 10:28
  • @Christian I just use \newcounter{ap} and ommit counterwithout, but it makes no difference to the output. What I want is, to use \theap instead of \thesubsection to be displayed by \cref, or something similar. – Gerald Nov 27 '15 at 10:38
  • @Gerald, yes,I suspect that the \cref formats are wrongly defined. My assoccnt package (please use xassoccnt, the newer,better one) works with cleveref normally – user31729 Nov 27 '15 at 10:41
1

Update: As of version 1.2. of xassoccnt supports the experimental feature of labels to associated counters. See the example for version 1.2 (and later) below

Amongst some 'wrong' usage of \newcounter etc. and the fact, that cleveref should be the last package to be included, the main cause that the wrong output is used has its origins in the fact \label[ap]{...} assuming, that the ap counter has been used with \refstepcounter, but this is not true.

I designed the assoccnt package (and its successor xassoccnt) to have associated counters which are stepped too when the master counter is stepped (subsection here), but declined to apply this feature for \refstepcounter too, since this would be ambiguous -- the associated counters would be stepped, but each one would set the \@currentlabel entry, thereby overwritting the master counter label. Since an associated counter has a totally different value than its master counter the \label command would write a wrong counter and reference value to the .aux file.

Now, \refstepcounter would use the subsection value and this is still in the 2.1 format, so \label[ap]{...} writes some {2.1} value as reference to the .aux file.

The solution is to use a different label command, which generates a fake label \@currentlabel suitable for cref and sets the values then.

\documentclass[10pt,a4paper,twoside]{article}
\usepackage{chngcntr}
\usepackage{xassoccnt}

\newcounter{ap}
\DeclareAssociatedCounters{subsection}{ap}
\counterwithout{ap}{section}

\usepackage{cleveref}
\makeatletter
% Generate a fake label
\newcommand{\labelap}[2][ap]{%
  \def\@tempa{#1}%
  \def\cref@result{2}%
  \protected@edef\cref@currentlabel{%
    [\@tempa][\arabic{#1}][\cref@result]%
    \csname p@#1\endcsname\csname the#1\endcsname}%
  \label[ap]{#2}%
}
\makeatother

\crefname{ap}{package}{packages}
\Crefname{ap}{Package}{Packages}
\creflabelformat{ap}{AP~#2#1#3}
\crefformat{ap}{AP~#2#1#3}

\begin{document}


  \section{ some section  }
  \section{ special section on packages  }
  \setcounter{ap}{0}

      Here we describe \cref{sec:ap-one,sec:ap-two,sec:ap-three}  ....
      this will be presented in \cref{sec:ap-one,sec:ap-two} and 
      finally in \cref{sec:ap-three}we describe ....

    Preferably this should look like this:

      Here we describe package AP 1 to AP 3  .... 
      this will be presented in packages AP 1 and AP 2 and 
      finally in package AP 3 we describe ....

    but currently it looks like this:

      Here we describe package AP 2.1 to AP 2.3  .... 
      this will be presented in packages AP 2.1 and AP 2.2 and 
      finally in package AP 2.3 we describe ....

   \subsection{AP~\arabic{ap}: first }
   \labelap{sec:ap-one}
     bla bla bla
     Here we describe  ... 
     which will be used in \cref{sec:ap-two,sec:ap-three}  ....

   \subsection{AP~\arabic{ap}: second }
   \labelap{sec:ap-two}
     bla bla bla
     we described in \cref{sec:ap-one}  .... 
     follow in \cref{sec:ap-three}

     ap counter is \arabic{ap} or \theap.

     subsection counter is \arabic{subsection} 
     and ``thesection'' is \thesubsection.


   \subsection{AP~\arabic{ap}: three }
   \labelap{sec:ap-three}
     bla bla bla
     we described in \cref{sec:ap-one,sec:ap-two}  


  \section*{Appendix}
     The table of contents is fine as is 
  \tableofcontents

\end{document}

Version 1.2 style of solution

\documentclass[10pt,a4paper,twoside]{article}
\usepackage{chngcntr}
\usepackage{xassoccnt}
\usepackage{hyperref}
\NewDocumentCounter{ap, apother}
\DeclareAssociatedCounters{subsection}{ap, apother}

\counterwithout{ap}{section}

\usepackage{cleveref}

\crefname{ap}{package}{packages}
\Crefname{ap}{Package}{Packages}
\creflabelformat{ap}{AP~#2#1#3}
\crefformat{ap}{AP~#2#1#3}

\begin{document}

See \cref{foosection}

  \section{ some section  } \label{foosection}
  \section{ special section on packages  }
  \setcounter{ap}{0}

      Here we describe \cref{ap::ap-one,ap::ap-two,ap::ap-three}  ....
      this will be presented in \cref{ap::ap-one,ap::ap-two} and 
      finally in \cref{ap::ap-three} we describe ....

    Preferably this should look like this:

      Here we describe package AP 1 to AP 3  .... 
      this will be presented in packages AP 1 and AP 2 and 
      finally in package AP 3 we describe ....

    but currently it looks like this:

      Here we describe package AP 2.1 to AP 2.3  .... 
      this will be presented in packages AP 2.1 and AP 2.2 and 
      finally in package AP 2.3 we describe ....

   \subsection{AP~\arabic{ap}: first } \label{ap-one}
     bla bla bla
     Here we describe  ... 
     which will be used in \cref{ap::ap-two,ap::ap-three}  ....


   \subsection{AP~\arabic{ap}: second }
   \label{ap-two}
     bla bla bla
     we described in \cref{ap::ap-one}  .... 
     follow in \cref{ap::ap-three}

     ap counter is \arabic{ap} or \theap.

     subsection counter is \arabic{subsection} 
     and ``thesection'' is \thesubsection.


   \subsection{AP~\arabic{ap}: three }
   \label{ap-three}
     bla bla bla
     we described in \cref{ap::ap-three,ap::ap-two} but look at \ref{ap-three}  


  \section*{Appendix}
     The table of contents is fine as is 
  \tableofcontents

\end{document}
  • Your answer works greatly using the assoccnt package, but I get an error Undefined control sequence \str_const:Nn from xassoccnt.sty:36 – Gerald Dec 3 '15 at 14:38
  • @Gerald: You need the newest expl3 version -- str_const:Nn was added about three months ago --> it will work with xassoccnt too then – user31729 Dec 3 '15 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.