(This is a somewhat of a continuation of this question of mine, which @egreg provided an easy solution to.)
In the following (slight variation of egreg's) MWE, three commands are defined. \mylabel creates \label (to be used by \Cref in the next two commands) and a \RecordProperties that will be used to record other info. Then there are \mychapref and \mysecref that produce the "conditional"/"relative" references.
\documentclass{book}
\usepackage{hyperref}
\usepackage{cleveref}
\NewProperty{chapter}{now}{\bfseries??}{\Roman{chapter}}
\NewProperty{section}{now}{\bfseries??}{\arabic{section}}
\NewDocumentCommand{\mylabel}{m}{%
\label{#1}%
\RecordProperties{#1@recProp}{chapter,section,target}%
}
\ExplSyntaxOn
\NewDocumentCommand{\myref}{m}{
% If \Cref{#1} begins with "Chapter", then execute \mychapref{#1}.
% If \Cref{#1} begins with "Section", then execute \mysecref{#1}.
}
\NewDocumentCommand{\mychapref}{m}{
\str_if_eq:eeTF { \RefProperty{#1@recProp}{chapter} } { \Roman{chapter} } {
% we are in the same chap
this~chapter
} {
% in a diff chap
\Cref{#1}
}
}
\NewDocumentCommand{\mysecref}{m}{
\str_if_eq:eeTF { \RefProperty{#1@recProp}{chapter} } { \Roman{chapter} } {
% we are in the same chap
\str_if_eq:eeTF{ \RefProperty{#1@recProp}{section} } { \arabic{section}} {
% in the same sec
this~section
} {
%same chap diff sec
\Cref{#1}
}
}
{
% in a diff chap
\Cref{#1},\nobreakspace Chapter\nobreakspace\RefProperty{#1@recProp}{chapter}
}
}
\ExplSyntaxOff
\begin{document}
\chapter{First chapter}\mylabel{CHAP: I}
\mychapref{CHAP: I}
\section{abc}\mylabel{SEC: I.1}
\mysecref{SEC: I.1}
\section{efg}
\mysecref{SEC: I.1}
\chapter{Second chapter}
\mychapref{CHAP: I}
\mysecref{SEC: I.1}
\end{document}
What is wanted: I want to "combine" \mychapref and \mysecref in a command \myref as described in the comment above. However, I came across the fact that \Cref* is not expandable and hence I can't assign it to string variables using \str_set:Ne, or try \exp_args:Ne, and then procees with the logic.
Is there a way to implement what is intended here?
Edit:
So, I tried to define \myref via \mycreftype that @gusbrs defines here, like so:
\NewDocumentCommand{\myref}{m}{
\str_if_eq:eeTF { \mychapref{#1} } { chapter } { \mychapref{#1} } {}
\str_if_eq:eeTF { \mycreftype{#1} } { section } { \mysecref{#1} } {}
}
However, running the MWE (with just the above and @gusbrs' definition of \mycreftype added to the preamble, along with replacing \mychapref{CHAP: I} with \myref{CHAP: I}), I get error messages, the first being
Argument of \@firstoftwo has an extra }. \myref{CHAP: I}
Any help?
\crefis not expandable, and the very link you provided, also shows thatcrossreftoolshas a simplified expandable one\crtcref, which you could use. But, in your case, you might prefer to get the type directly with https://tex.stackexchange.com/a/692754/105447. – gusbrs Jan 29 '24 at 18:36counterproperty in your\RecordProperties{#1@recProp}call, then use it to assume the type. – gusbrs Jan 29 '24 at 18:40typebest solves my case, for I plan to have theorem environments as well. Thanks tons! – Atom Jan 29 '24 at 19:08\mylabelby just setting your properties on thelabelhook (with\AddToHookWithArguments). – gusbrs Jan 29 '24 at 19:22\AddToHookWithArguments. Btw, something is not working when I incorporated your\mycreftypein my MWE to define\myref. I have edited my question. Can you have a look? Many thanks! – Atom Jan 29 '24 at 19:42\mycreftypeitself is non expandable since it performs an assignment, but you can use the assignment it does to store the value of interest in a variable, and then compare that. I'll add an answer, it may be simpler. – gusbrs Jan 29 '24 at 20:06