\makeatletter
%% Code for \ExtractKthArg
%%=============================================================================
%% Paraphernalia:
%% \UD@firstoftwo, \UD@secondoftwo, \UD@PassFirstToSecond, \UD@Exchange,
%% \UD@stopromannumeral, \UD@CheckWhetherNull
%%=============================================================================
\newcommand\UD@firstoftwo[2]{#1}%
\newcommand\UD@secondoftwo[2]{#2}%
\newcommand\UD@PassFirstToSecond[2]{#2{#1}}%
\newcommand\UD@Exchange[2]{#2#1}%
\@ifdefinable\UD@stopromannumeral{\chardef\UD@stopromannumeral=`\^^00}%
%%-----------------------------------------------------------------------------
%% Check whether argument is empty:
%%.............................................................................
%% \UD@CheckWhetherNull{<Argument which is to be checked>}%
%% {<Tokens to be delivered in case that argument
%% which is to be checked is empty>}%
%% {<Tokens to be delivered in case that argument
%% which is to be checked is not empty>}%
%%
%% The gist of this macro comes from Robert R. Schneck's \ifempty-macro:
%% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
\newcommand\UD@CheckWhetherNull[1]{%
\romannumeral\expandafter\UD@secondoftwo\string{\expandafter
\UD@secondoftwo\expandafter{\expandafter{\string#1}\expandafter
\UD@secondoftwo\string}\expandafter\UD@firstoftwo\expandafter{\expandafter
\UD@secondoftwo\string}\expandafter\UD@stopromannumeral\UD@secondoftwo}{%
\expandafter\UD@stopromannumeral\UD@firstoftwo}%
}%
%%=============================================================================
%% Extract K-th inner undelimited argument:
%%
%% \ExtractKthArg{<TeX-<number>-quantity of value K>}%
%% {<tokens in case list of undelimited args doesn't have a k-th argumnent>}%
%% {<list of undelimited args>} %
%%
%% In case there is no K-th argument in <list of indelimited args> :
%% Does deliver <tokens in case list of undelimited args doesn't have a k-th argumnent.
%% In case there is a K-th argument in <list of indelimited args> :
%% Does deliver that K-th argument with one level of braces removed.
%%
%% Examples:
%%
%% \ExtractKthArg{0}{not available}{ABCDE} yields: not available
%%
%% \ExtractKthArg{3}{not available}{ABCDE} yields: C
%%
%% \ExtractKthArg{3}{not available}{AB{CD}E} yields: CD
%%
%% \ExtractKthArg{4}{not available}{{001}{002}{003}{004}{005}} yields: 004
%%
%% \ExtractKthArg{6}{not available}{{001}{002}{003}} yields: not available
%%
%%=============================================================================
\newcommand\ExtractKthArg[2]{%
\romannumeral%
% #1: <integer number K>
% #2: <action if there is no K-th argument>
\expandafter\UD@ExtractKthArgCheck
\expandafter{\romannumeral\number\number#1 000}{#2}%
}%
\newcommand\UD@ExtractKthArgCheck[3]{%
\UD@CheckWhetherNull{#1}{\UD@stopromannumeral#2}{% empty
\expandafter\UD@ExtractKthArgLoop\expandafter{\UD@firstoftwo{}#1}{#2}{#3}%
}%
}%
\begingroup
\def\UD@ExtractFirstArgLoop#1{%
\endgroup
\@ifdefinable\UD@RemoveTillFrozenrelax{%
\long\def\UD@RemoveTillFrozenrelax##1##2#1{{##1}}%
}%
\newcommand\UD@ExtractKthArgLoop[3]{%
\expandafter\UD@CheckWhetherNull\expandafter{\UD@firstoftwo##3{}.}{\UD@stopromannumeral##2}{%
\UD@CheckWhetherNull{##1}{%
\UD@ExtractFirstArgLoop{##3#1}%
}{%
\expandafter\UD@PassFirstToSecond\expandafter{\UD@firstoftwo{}##3}%
{\expandafter\UD@ExtractKthArgLoop\expandafter{\UD@firstoftwo{}##1}{##2}}%
}%
}%
}%
}%
\expandafter\expandafter\expandafter\UD@ExtractFirstArgLoop
\expandafter\expandafter\expandafter{%
\expandafter\expandafter\ifnum0=0\fi}%
%% Usage of frozen-\relax as delimiter is for speeding things up by reducing the
%% amount of iterations needed. I chose frozen-\relax because David Carlisle
%% pointed out in <https://tex.stackexchange.com/a/578877>
%% that frozen-\relax cannot be (re)defined in terms of \outer and cannot be
%% affected by \uppercase/\lowercase.
%%
%% \UD@ExtractFirstArg's argument may contain frozen-\relax:
%% The only effect is that internally more iterations are needed for
%% obtaining the result.
\newcommand\UD@ExtractFirstArgLoop[1]{%
\expandafter\UD@CheckWhetherNull\expandafter{\UD@firstoftwo{}#1}%
{\expandafter\UD@stopromannumeral\UD@firstoftwo#1{}}%
{\expandafter\UD@ExtractFirstArgLoop\expandafter{\UD@RemoveTillFrozenrelax#1}}%
}%
%% End of code for \ExtractKthArg.
\makeatother
%==============================================================================
% \SelectAccordingToStyleFlag
%==============================================================================
%
% Syntax:
%
% \SetStyleFlag{1} or \SetStyleFlag{2} or \SetStyleFlag{3} or ....
%
% \SelectAccordingToStyleFlag{<Default-Tokens>}{%
% {<Tokens if value of \StyleFlag is 1>}%
% {<Tokens if value of \StyleFlag is 2>}%
% {<Tokens if value of \StyleFlag is 3>}%
% ...%
% }%
%
\makeatletter
\newcommand\StyleFlag{1}%
\newcommand\SetStyleFlag[1]{@bsphack\def\StyleFlag{#1}@esphack}%
\newcommand\SelectAccordingToStyleFlag{%
\ExtractKthArg{\StyleFlag}%
}%
\makeatother
%==============================================================================
\documentclass{article}
\usepackage{indentfirst}
\usepackage[hyper]{philex}
\usepackage[hidelinks]{hyperref}
\usepackage{cleveref}
\usepackage{xstring}
%% Enable the following lines to see the coming into being of duplicate
%% destination/target/anchor-names more explicitly in the console-output - the
%% problem about warnings of pattern
%% pdfTeX warning (ext4): destination with the same identifier (name{altsubsub.1})
%% has been already used, duplicate ignored
%% still needs to be fixed by fixing what the macros coming from the philex-package
%% do. Probably by taking \theH<counter>-macros associated to philex's counters
%% into account:
%\makeatletter
%\AtBeginDocument{%
% \let\oldhypertarget\hypertarget
% \def\hypertarget#1{\message{^^J!!!Target-Name: #1^^J}\oldhypertarget{#1}}%
% \let\oldrefstepcounter\refstepcounter
% \def\refstepcounter{\message{^^J!!!Href: \detokenize\expandafter{@currentHref}^^J}\oldrefstepcounter}%
%}%
%\makeatother
%MyStripToColon (requires xstring)
\newcommand{\mystriptocolon}[2]{\StrCut{#1}{.}{\myonea}{\myoneb}%
\StrCut{#2}{.}{\mytwoa}{\mytwob}%
\IfStrEq{\myonea}{\mytwoa}{\mytwob}{#2}%
}
% Cleveref Label Formatting
\crefname{ExNo}{}{}
\crefname{SubExNo}{}{}
\crefname{SubSubExNo}{}{}
\creflabelformat{ExNo}{#2#1#3} % The output of the \cref command doesn't include parentheses.
\creflabelformat{SubExNo}{#2#1#3}
\creflabelformat{SubSubExNo}{#2#1#3}
\crefrangelabelformat{ExNo}{#3#1#4--#5#2#6}
\crefrangelabelformat{SubExNo}{#3#1#4--#5\crefstripprefix{#1}{#2}#6}
\crefrangelabelformat{SubSubExNo}{%
\SelectAccordingToStyleFlag{#3#1#4--#5\mystriptocolon{#1}{#2}#6}{% <- default-format
{#3#1#4--#5\mystriptocolon{#1}{#2}#6}% <- Format if value of \StyleFlag is 1
{#3#1#4--#5\crefstripprefix{#1}{#2}#6}% <- Format if value of \StyleFlag is 2
% Append more if \StyleFlag can also be 3 or 4 or etc
}%
}%
\newcommand{\crefrangeconjunction}{–}
\begin{document}
% Philex Label Formatting
\phildashes{}{.}
\subformat{a}{}{.}
\subsubformat{i}{(}{)}
\lb{main1}{This is main1.
\lba{sub1}{This is sub1.
\lba{subsub11}{This is subsub11.}
\lbz{subsub12}{This is subsub12.}}
\lbz{sub2}{This is sub2.
\lba{subsub21}{This is subsub21.}
\lbz{subsub22}{This is subsub22.}}}
\lb{main2}{This is main2.}
\verb|\mystriptocolon| works like a charm:
\vspace{6pt}
\SetStyleFlag{1}%
\verb|\crefrange{sub1}{sub2}| \textrightarrow{} \crefrange{sub1}{sub2}
\vspace{6pt}
\verb|\crefrange{subsub11}{subsub12}| \textrightarrow{} \crefrange{subsub11}{subsub12}
\vspace{6pt}
\textbf{The philex-formatting is changed:}
\phildashes{.}{}
\subformat{1}{}{.}
\subsubformat{a}{}{.}
\lb{main3}{This is main3.
\lba{sub31}{This is sub31.
\lba{subsub311}{This is subsub311.}
\lbz{subsub312}{This is subsub312.}}
\lbz{sub32}{This is sub32.}}
\vspace{6pt}
\textbf{The Style-flag is changed from 1 to 2 to have other branch of
\texttt{\string\crefrangelabelformat\string{SubSubExNo\string}} in effect:}
\vspace{6pt}
\verb|\SetStyleFlag{2}|\SetStyleFlag{2}%
\verb|\crefrange{subsub311}{subsub312}| \textrightarrow{} \crefrange{subsub311}{subsub312}
\vspace{6pt}
As you can see, you obtain 3.1a--b without the 1 being repeated.
\end{document}
zref. If anyone wants to quickly see how this jewel works: https://tex.stackexchange.com/a/644002/262813 – Vincent Krebs May 15 '22 at 21:34