0

Getting inspired by this Have reference in section with custom counter, I've developed the following new environment. All is fine except when I call \prettyref{dog:1} output is just showing 1 instead of DOG1. After some amount of debugging, it looks like \newrefformat inside the definition of the environment is not being executed.

\newenvironment{animals}[1][dog]%
{
    \newrefformat{#1}{\hyperref[{##1}]{\uppercase{#1} \ref*{##1}}}
    
    \newcount\IndexNum
    \IndexNum = 1
    \newcounter{count#1}%
    \setcounter{count#1}{0}%
    
    \let\olditem\item%
    \begin{enumerate}[label={\texttt{\uppercase{#1}\arabic*}}, ref={\arabic*}]%
    
        \setcounter{enumi}{\value{count#1}}%
        \renewcommand{\item}[1][]{%
            \refstepcounter{count#1}\olditem%
            \label{#1:\the\IndexNum} \global\advance\IndexNum by 1
        }%

}%
{\end{enumerate}}

I've also attached an MWE for your reference.

\documentclass{article}

\usepackage{prettyref}
\usepackage{hyperref}
\usepackage{enumitem}

\newenvironment{animals}[1][dog]%
{
    \newrefformat{#1}{\hyperref[{##1}]{\uppercase{#1} \ref*{##1}}}
    
    \newcount\IndexNum
    \IndexNum = 1
    \newcounter{count#1}%
    \setcounter{count#1}{0}%
    
    \let\olditem\item%
    \begin{enumerate}[label={\texttt{\uppercase{#1}\arabic*}}, ref={\arabic*}]%
        
        \setcounter{enumi}{\value{count#1}}%
        \renewcommand{\item}[1][]{%
            \refstepcounter{count#1}\olditem%
            \label{#1:\the\IndexNum} \global\advance\IndexNum by 1
        }%
        
    }%
    {\end{enumerate}}

\begin{document}
    \begin{animals}
        \item Pitbull
        \item Boxer
    \end{animals}

\prettyref{dog:1} is Pitbull. 
\end{document}

Current output is

Current output

while the expected output is

Expected output

1 Answer 1

0

The \newrefformat command effect is limited in scope by the environment.

Looking at the source code in prettyref package, one discovers it is possible to prefix it by \global to overcome the scope limitation.

Besides, despite its name, it will not as \newcommand raise an error if reused with same argument. So one can have multiple such environments with same optional argument.

\documentclass{article}

\usepackage{prettyref}
\usepackage{hyperref}
\usepackage{enumitem}

\newenvironment{animals}[1][dog]{%
    \global\newrefformat{#1}{\hyperref[{##1}]{\uppercase{#1}\ref*{##1}}}%
%    
    \newcount\IndexNum
    \IndexNum = 1
    \newcounter{count#1}%
    \setcounter{count#1}{0}%
%    
    \let\olditem\item
    \begin{enumerate}[label={\texttt{\uppercase{#1}\arabic*}}, ref={\arabic*}]
        
        \setcounter{enumi}{\value{count#1}}%
        \renewcommand{\item}[1][]{%
            \refstepcounter{count#1}\olditem%
            \label{#1:\the\IndexNum} \global\advance\IndexNum by 1
        }%
        
    }%
    {\end{enumerate}}

\begin{document}
    \begin{animals}
        \item Pitbull
        \item Boxer
    \end{animals}

\prettyref{dog:1} is Pitbull. 
\end{document}

enter image description here

3
  • Thanks a lot.!!
    – ShastriD
    Commented May 19, 2023 at 7:19
  • @ShastriD The code has important faults in particular \newcount\IndexNum should better NOT be part of environment, you only need to reset the count to 1 at each use. As per the \newcounterit is surprising to include it ias this will limit to one use only of environment with given argument: LaTeX Error: Command \c@countdog already defined. It looks very strange to have to use both a \count and a LaTeX counter, something clearly is sub-optimal. An "environment" that one can use only once, is not really a genuine environment, it does not look like the correct structure for your task
    – user691586
    Commented May 19, 2023 at 7:28
  • Thank you for pointing the mistake. I've now put an if condition to check if the counter is already defined. If so, I'll only reset the counter. Now it lets using the environment multiple tiles for the same argument.
    – ShastriD
    Commented May 19, 2023 at 8:24

You must log in to answer this question.

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