I need chapter/section specific labels to be generated dynamically for each chapter and section. I have modified the chapter and section command to do that. I use a regex based string manipulation and the following scheme to achieve it (MWE uses an article class to illustrate) -
\documentclass{article}
\usepackage{expl3,l3regex}
\ExplSyntaxOn
\NewDocumentCommand{\compactlabel}{m}
{
\tl_set:Nn \l_tmpa_tl { #1 }
\regex_replace_all:nnN {[^a-zA-Z]} {\ } \l_tmpa_tl
\regex_replace_all:nnN {\ ([a-zA-Z]{1,2}\ )*} {\ } \l_tmpa_tl
\regex_replace_all:nnN {^[a-zA-Z]{1,2}\ } {\ } \l_tmpa_tl
\regex_replace_all:nnN {\ [a-zA-Z]{1,2}$} {\ } \l_tmpa_tl
\regex_replace_all:nnN {^\ +} {} \l_tmpa_tl
\regex_replace_all:nnN {\ +$} {} \l_tmpa_tl
\regex_replace_all:nnN {\ +} {\ } \l_tmpa_tl
\regex_replace_all:nnN {\ } {_} \l_tmpa_tl
\regex_replace_all:nnN {(^\w{1,20})\w*} {\1} \l_tmpa_tl
\regex_replace_all:nnN {_$} {} \l_tmpa_tl
\tl_use:N \l_tmpa_tl
}
\ExplSyntaxOff
\begin{document}
% \sidebyside is a temporary command just to display and illustrate the point
\newcommand*{\sidebyside}[1]{
#1
\compactlabel{#1}
}
\sidebyside{A 1st Mathematical Primer for the universe of chimpmunks}
\end{document}
My objectives :
- The argument string must be cleared of all one and two letter words as well as all numbers and non-characters.
- All multiple or single spaces the in the string must be replaced by an underscore.
- The last or first character of the output must not be an underscore.
- The string size should be curtailed at 20**.
So in the MWE, I see this as output -
A 1st Mathematical Primer for the universe of chimpmunks
mathematical_primer
** Notice the magic number 20 in the line
\regex_replace_all:nnN {(^\w{1,20})\w*} {\1} \l_tmpa_tl
above.
** Ideally I want this number to be a variable, default set at 20. So if I call \compactlabel{#1}
it should mean 20 as above. But I would like to also call something like \compactlabel[10]{#1}
and get the string size to be curtailed by 10.
PS: To be honest I do not claim to completely understand the expl3 or l3regex syntax. I copied and adapated it from tex.stackexchage forum.
\detokenize
the title and the number and use this as the label? – Henri Menke Jun 22 '17 at 6:15\str_range:Nnn
. – Bruno Le Floch Jul 12 '17 at 21:40