1

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

Compiled Doc

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 edef or expandarrayelementtrue breaks the code?
  • if there is another way to convince cleveref to 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

Moraxno
  • 33
  • To the best of my knowledge it is not possible to use \edef with \padzeros. –  Apr 28 '18 at 00:53
  • But why is this? Are there alternatives or workarounds? – Moraxno Apr 28 '18 at 01:18
  • 2
    For the cleveref part, 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
  • 2
    Depends on how you define "why". The reason why I think this is true is that it has been noted here. I also think that if you solve this problem, you will have an analogous issue with \StrLeft and \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
  • @cfr Well, I defined an array like \newarray TheList and added a line to \cmust like \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
  • @marmot Well it seems like you are right about that. I haven't found a formating style for cref which allows this much customization, how would this be done? – Moraxno Apr 28 '18 at 07:29
  • it is always wrong to use \edef with "unknown" tokens especially if you use unexpandable commands like those from xstring – David Carlisle Apr 28 '18 at 07:42
  • @DavidCarlisle How do you recognize an unexpandable command? Are there expandable commands which provide string manipulation like xstrings ? – Moraxno Apr 28 '18 at 07:53
  • I've certainly used \cref for 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 used xstring, if ever, and never used the array thing you're working with - just fiddled somewhat with cleveref. (Usually, I use fancyref.) – cfr Apr 29 '18 at 00:44
  • @cfr This sounds exactly like my problem. Could you go more into detail please? How exactly did you create the letter from the environment? I just don't know, how to get this done without some overcomplicated redefining of commands and list keeping... – Moraxno Apr 29 '18 at 09:02

0 Answers0