0

I am fighting with this problem for a while now and all my partial solutions just don't fit together. I have to manage quite a few lists in a latex document and each entry has a certain ID like [X1234] where X is the first letter of the current chapter name, 1 is the chapter number, 2 is the section number and 34 is a two digit counter which resets every chapter. I have a command which creates these numbers for me, adds a label via a parameter and increments a counter via \refstepcounter, because I want them to be linked to references, when I use a \cref on the labels.

\documentclass[12pt,a4paper]{report}
\usepackage{etoolbox}
\usepackage{xpatch}
\usepackage{cleveref}
\usepackage{xstring}

\newcounter{allIDs}
\setcounter{allIDs}{0}

\newcounter{chapIDs}
\setcounter{chapIDs}{0}

\newcommand{\currentchapter}{NOT SET}

\newcommand{\twodigit}[1]{\ifnum#1<10 0#1\else#1\fi}

\newcommand{\chapnumber}{\StrLeft{\thesection}{1}\StrRight{\thesection}{1}}

\newcommand{\chapletter}
{%
    \StrLeft
    {%
        \currentchapter{}%
    }{1}%
}

\newcommand{\entrycode}
{%
    \chapletter%
    \chapnumber%
    \twodigit{\thechapIDs}%
}

\xpretocmd{\@chapter}
{%
    \setcounter{chapIDs}{0}%
    \renewcommand{\currentchapter}{#1}%
    %
}%
%

\newcommand{\makeID}[1]{%
    \refstepcounter{allIDs}%
    \stepcounter{chapIDs}%
    \label{#1}%
    $[$\entrycode$]$%
}%
%

\crefformat{allIDs}{#2Reference to{} #1#3}

\begin{document}

\chapter{First}
\section{Some}

\makeID{label:Hello}

\cref{label:Hello}

\end{document}

Result

Unfortunately \cref decides to put the value of the counter in and not the ID as described above. How can I tell \cref to use these instead? Also, when adding the hyperref package my code for getting the chaptername seems to break, it displays #1 as if the argument would be read as a String. How can I avoid that?

  • Why did you switch from the koma class to report? The koma classes have much more inbuilt hooks that one can use for this type of tasks without having to patch internal commands. – Ulrike Fischer Apr 28 '18 at 14:13
  • @Ulrike Fischer Well actually, it was an error in my MWE, it should have been a report all along. But maybe I can switch to koma. How would I solve this problem then? – Moraxno Apr 28 '18 at 14:16
  • Regarding the label you must set the correct \@currentlabel definition, but beside this I would a lot things differently (e.g. not use \Strleft and certainly not on commands like \thesection and chapter titles which can contain arbitrary things, I would probably also use zref to set the labels). – Ulrike Fischer Apr 28 '18 at 14:20
  • 1
    @UlrikeFischer I am afraid that I am not quite able to understand your suggestion. What should a 'correct' definition of \@currentlabel look like? Why is it bad to use \Strleft? And what are the advantages of zref labels? I hope you you can shed a bit of light on these things for me. – Moraxno Apr 28 '18 at 14:49
  • @Moraxno It seems a strange idea to take the first letter of a chapter name as part of an ID. Have two chapters beginning with an »A« and your readers will be confused. And why manage lists with IDs inside of chapters? What about telling us more precisely, what you'd like to do in the end. And »zref« refers to the zref package. Type texdoc zref + RET on the command line and you'll get the manual. – Keks Dose May 7 '18 at 11:28

Your Answer

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

Browse other questions tagged or ask your own question.