I am currently writing a kind of requirement analysis and want to enumerate my sections and subsections in a certain style:
My sections, should look like normal so e.g. "1.1 Import and Export of Files", but the should define a macro "\startLetter" to be the first letter in the title (here "I").
The following subsection's enumeration should start with the current \startLetter and have an increasing three digit identifier, which resets at the start of the next section, e.g. "I001 Text Files Import". When referencing these, I want only the Letter + ID to be printed, so "I001" and it also still referencing there via hyperref.
My current setup is in the MWE below. The problems are
- I don't know how to shorten the reference to only the ID without breaking the hyperref link and
- The ID counter still counts up when referencing. I pretty sure that is because the macro becomes part of the section name and therefore gets reevaluate every time. How do I stop that?
I had a look into titlesec, xparse and etoolbox's pretocmd and apptocmd, but this would require to patch the @ssect command (?) and I am to inexperienced to just edit in the underlying commands of Latex sections. Is there a "noob-understandable" way to solve this problem?
Thanks in advance :)
MWE:
\documentclass[10pt,a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{xstring}
\usepackage{nameref}
\usepackage{hyperref}
\newcommand{\startLetter}{}
\newcounter{reqID}
\newcommand{\reqSection}[1]{%
\section{#1}
\renewcommand{\startLetter}{\StrMid{#1}{0}{1}}
\setcounter{reqID}{1}
}
\newcommand{\leadingZerosID}{%
\ifnum\value{reqID}<100 0\fi\ifnum\value{reqID}<10 0\fi\arabic{reqID}
}
\newcommand{\reqSubsection}[1]{%
\subsection*{\startLetter\leadingZerosID{} #1}
\stepcounter{reqID}
}
\parindent0mm
\begin{document}
\chapter{Any chapter}
\reqSection{Import Export}
\reqSubsection{Text Files Import}
\label{lbl:import-text}
Some useless blindtext. Some useless blindtext. Some useless blindtext. Some useless blindtext.
\reqSubsection{Text Files Export}
\label{lbl:export-text}
Some useless blindtext. Some useless blindtext. Some useless blindtext. Some useless blindtext.
\reqSubsection{Video Files Import}
\label{lbl:import-video}
Some useless blindtext. Some useless blindtext. Some useless blindtext. Some useless blindtext.
\vspace{1cm}
I am refering to requirement \nameref{lbl:export-text}.
\end{document}