I am trying to make a command which creates automatically incrementing IDs for entries in some lists I have to manage. They have to follow a certain formatting (for example the first character has to be the first letter of the current chapter). The IDs should be compatible with cleveref also, so I can \cref the according label and a customized reference appears.
The following code compiles:
\documentclass[12pt,a4paper]{report}
\usepackage{xstring}
\usepackage{fmtcount}
\usepackage{arrayjob}
\usepackage{etoolbox}
\usepackage{xpatch}
\usepackage{cleveref}
% entry counter
\newcounter{globalcounter}
\setcounter{globalcounter}{0}
\newcounter{localcounter}
\setcounter{localcounter}{0}
% chaptername tracker
\xpretocmd{\@chapter}
{%
\setcounter{localcounter}{0}%
\def\currentchapter{#1}%
%
}{}{}
% chapter number generator
\newcommand{\chapnumber}
{%
\StrLeft{\thesection}{1}\StrRight{\thesection}{1}
}
% entrycode
\newcommand{\entrycode}
{
\StrLeft{\currentchapter}{1}\chapnumber\padzeroes[2]{\decimal{localcounter}}
}
\newcommand{\cmust}[1]{%
\refstepcounter{globalcounter}%
\stepcounter{localcounter}%
\label{#1}%
\def\current{\entrycode}%
\current
}
\begin{document}
\chapter{Testchapter}
\cmust{test}
\cmust{test2}
\end{document}
and returns
which is basically what I want. But I dont know how to make cleveref use my "special" numbering system when referencing, so I tried to use the arrayjob package, creating an Array with the cleveref counter as an Index and my numbering as the value. But unfortunately the whole code breaks, when I try to evaluate my expression before the arrays saves the value (\expandarrayelementtrue). The same happens when I change the \def in the definition of \cmust below for a \edef but I just can't see why. Does anyone know
- why
edeforexpandarrayelementtruebreaks the code? - if there is another way to convince
cleverefto choose my style of referencing? - if there is an easier way to save the evaluated result of an expression rather than the evaluatable expression itself?
Thanks in advance,
Moraxno

\edefwith\padzeros. – Apr 28 '18 at 00:53cleverefpart, what have you tried? The manual covers customisation of the format - both high level and lower level changes. – cfr Apr 28 '18 at 01:44\StrLeftand\StrRight. Anyway, I fully agree with @cfr that you should use the cleveref package to adjust things to your needs. – Apr 28 '18 at 02:39\cmustlike\TheList(\value{globalcounter})={\entrycode}and later I defined a\crefformat{globalcounter}{#2 \TheList(#1) #3}but unfortunately these references never worked correctly, because the elements of the array where evaluated way to late and all the variables had already changed. – Moraxno Apr 28 '18 at 07:19\edefwith "unknown" tokens especially if you use unexpandable commands like those fromxstring– David Carlisle Apr 28 '18 at 07:42xstrings ? – Moraxno Apr 28 '18 at 07:53\creffor formatted lettered environments, for example, where the letter is determined on-the-fly when starting the environment. You basically have something not dissimilar here in terms of the chapter letter, for example: it is a fixed point, as far as the sections are concerned. But I've rarely usedxstring, if ever, and never used the array thing you're working with - just fiddled somewhat withcleveref. (Usually, I usefancyref.) – cfr Apr 29 '18 at 00:44