Trying to find a solution for How to set kerning in a combination of macros with a fixed character in between, I came across a related problem: when used as a superscript (in a footnote marker), certain characters will be too close to the main character, e.g. in combinations like o and superscript j.
To tackle both problems, I wanted to create a function that returns a kerning value depending on the first character of the marker’s “string value”.
My first attempts were to simply use \tl_head:n, then I read Convert string to token list? – both give the same, yet not the required result.
\documentclass{book}
\usepackage{refcount}
\usepackage{xstring}
\usepackage{xparse}
\usepackage{expl3}
\ExplSyntaxOn
\makeatletter
\DeclareDocumentCommand \aalph {m}
{
\IfInteger { #1 }
{
\myAlph:n { #1 }
}{
\@ifundefined { r@#1 }
{ yyyy }
{
\IfInteger { \getrefnumber { #1 } }
{ \myAlph:n { \getrefnumber { #1 } } }
{ zzzz }
}
}
}
\cs_new_protected:Npn \myAlph:n #1
{
\int_to_alph:n { #1 }
}
\DeclareDocumentCommand \alphKern {m}
{
\l_alphKern:n { #1 }
}
\cs_new:Npn \l_alphKern:n #1
{
\str_clear_new:N \l_alph_str
\tl_clear_new:N \l_tmp_tl
\str_set:Nn \l_alph_str { \aalph { #1 } }
\tl_set_rescan:Nno \l_tmp_tl { } { \l_alph_str }
\tl_head:n \l_tmp_tl
}
\ExplSyntaxOff
\newcounter{test}
\begin{document}
\setcounter{test}{1}
>\aalph{\thetest}<>\alphKern{\thetest}< (exp.: >a<>a<)\\
>\aalph{27}<>\alphKern{27}< (exp.: >aa<>a<)\\
>\aalph{53}<>\alphKern{53}< (exp.: >ba<>b<)\\
>\aalph{283}<>\alphKern{283}< (exp.: >jw<>j<)\\
\end{document}
The result:
>a<>a< (exp.: >a<>a<)
>aa<>aa< (exp.: >aa<>a<)
>ba<>ba< (exp.: >ba<>b<)
>jw<>jw< (exp.: >jw<>j<)
I might be overlooking sth. really obvious – any help is appreciated!
(PS: for this one, I am using XeTeX and as the project is nearly done, I cannot change to anything else)
#1is\thetest,\str_set:Nn \l_alph_str { \aalph { #1 } }defines\l_alph_stras string\aalph {\thetest }, not the "return value" of\aalph{\thetest}. For fully expandable macros, you can use latex3e-type expansion to get its expansion result. But unfortunately, thexstringmacro\IfIntegeris not fully expandable, hence\aalphis not fully expandable. – muzimuzhi Z Jun 10 '20 at 06:09\IfIntegerby other means. Could you make an answer so I can accept? – dariok Jun 10 '20 at 13:23