0

I would like to have a precise control on references to custom counters.

For instance,

  • I would like to give a reference to a series of two independent counters
  • I would like to choose the formatting of the reference

I have looked around but I haven't found a solution.

Here is a MWE.

\documentclass{article}

\newcounter{counterOne}
\newcounter{counterTwo}
\renewcommand{\thecounterTwo}{\alph{counterTwo}}

\begin{document}
\refstepcounter{counterOne}
\refstepcounter{counterTwo}

\label{here} Here

It was \ref{here}.

\bigskip

I would like that it displays : It was \textbf{1.(a)}
\end{document}

enter image description here


EDIT:

I've tried David's answer but it doesn't work. Here is a MWE. Unfortunately, as I don't understand the commands used in the answer, I can't fix by myself the errors.

\documentclass{article}

\newcounter{counterOne}
\newcounter{counterTwo}
\renewcommand{\thecounterTwo}{\alph{counterTwo}}

\makeatletter
\def\labelformat#1{\expandafter\def\csname p@#1\endcsname##1}
\makeatother
\labelformat{counterTwo}{\textbf{\thecounterOne.(#1)}}

\begin{document}
\refstepcounter{counterOne}
\refstepcounter{counterTwo}

\label{here} Here

It was \ref{here}.

\bigskip

I would like that it displays : It was \textbf{1.(a)}
\end{document}

The error is

Argument of @firstofone has an extra }. \par l.23 \end{document}

5
  • 1
    The edited example works in texlive 2019 and 2020 but fails in 2018 so you have an old latex, I'll see if there is a simple version that works in the old formats. Jan 29, 2021 at 15:08
  • 1
    Remove the \makeatletter..\makeatother block (the form in my question which works for current latex) then add \usepackage{varioref} to define \labelformat in older releases. Jan 29, 2021 at 15:10
  • @DavidCarlisle Thanks! It works with \usepackage{varioref}
    – Colas
    Jan 29, 2021 at 15:39
  • 1
    the \labelformat mechanism came from varioref you probably don't need the entire package but apparently you need more than the one line, but loading the package seems easier than picking out exactly what lines are needed in old releases. Jan 29, 2021 at 15:43
  • Yes, I just hope that it will not be in conflict with the other packages I load, especially hyperref. Anyway, thanks again for sharing your knowledge.
    – Colas
    Jan 29, 2021 at 15:55

1 Answer 1

1

enter image description here

\documentclass{article}

\newcounter{counterOne}
\newcounter{counterTwo}
\renewcommand{\thecounterTwo}{\alph{counterTwo}}

% \labelformat is defined in current latex to customise the output from \ref
% For older formats load the package \usepackage{varioref} from the core
% tools bundle which originally defined this mechanism.

\labelformat{counterTwo}{\textbf{\thecounterOne.(#1)}}

\begin{document}
\refstepcounter{counterOne}
\refstepcounter{counterTwo}

\label{here} Here

It was \ref{here}.

\bigskip

I would like that it displays : It was \textbf{1.(a)}
\end{document}
5
  • Thanks David, I've tried to compile with \makeatletter \def\labelformat#1{\expandafter\def\csname p@#1\endcsname##1} \makeatother \labelformat{compteurExercices}{\textbf{\thecompteurChapitre.(#1)}} but I have the following error: Undefined control sequence. \label #1->\@bsphack \begingroup \def \label@name {#1}\label@hook \protected...
    – Colas
    Jan 28, 2021 at 21:12
  • @Colas not from the above example presumably. \label@name isn't used in the definition of \label as defined by latex, so you are presumably using some non standard definition from some package. Perhaps ask a new question with an example producing that error. Jan 28, 2021 at 23:57
  • Your example does not work in a MWE. See my edit. Thanks 🙏
    – Colas
    Jan 29, 2021 at 14:09
  • Would like to update your answer with the varioref package? I think it deserves to be said in an answer. Thanks.
    – Colas
    Jan 31, 2021 at 12:33
  • 1
    @Colas OK I'll delete my first comment suggesting defining labelformat by hand and mention varioref in the answer Jan 31, 2021 at 12:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .