Test first and last digit of a (text) number

macros

Various fonts have some built-in fractions and/or the ability to construct fractions with the +frac font feature. They tend to look great, but are so small that they are hard to read.

So I'm trying to spin my own fraction macro, and find that I need to make kerning adjustments around the solidus. I can test the value of the entire numerator or denominator, but I really want to test just the last character of the numerator and the first character of the denominator.

How can I do this? What I have follows, but only works for single-digit numerators and denominators.

\documentclass{article}
\usepackage{fontspec}

%\setmainfont{STIXTwoText-Regular}
%\newfontfamily\fracfigs{STIXTwoText-Medium}[% compensate weight for scaling
%  Numbers = {Proportional,Lining},
%  Scale   = 0.75]

% Actual kerns wrong for this font, but everyone will have it:
\setmainfont{Latin Modern Roman}
\newfontfamily\fracfigs{Latin Modern Roman}[Scale = 0.75]

\newcommand*\myFii{2}
\newcommand*\myFiv{4}
\newcommand*\myFvi{6}
\newcommand*\myFvii{7}
\newcommand*\myFix{9}
\newcommand*\myfrac[2]{%
  \def\myfractmpn{#1}% Really want just last char
  \def\myfractmpd{#2}% Really want just first char
  \raisebox{0.5ex}{{\fracfigs#1}}%
  \ifx\myfractmpn\myFii       \kern 0.1em%
  \else\ifx\myfractmpn\myFvii \kern -0.05em%
  \else\ifx\myfractmpn\myFix  \kern 0pt%
  \else                       \kern 0.05em%
  \fi\fi\fi%
  \textfractionsolidus%
  \ifx\myfractmpd\myFiv       \kern -0.05em%
  \else\ifx\myfractmpd\myFvi  \kern 0pt%
  \else                       \kern 0.05em%
  \fi\fi%
  {\fracfigs#2}}

\begin{document}
\myfrac{1}{1}\quad \myfrac{11}{11}\par
\myfrac{2}{2}\quad \myfrac{22}{22}\par
\myfrac{3}{3}\quad \myfrac{33}{33}\par
\myfrac{4}{4}\quad \myfrac{44}{44}\par
\myfrac{5}{5}\quad \myfrac{55}{55}\par
\myfrac{6}{6}\quad \myfrac{66}{66}\par
\myfrac{7}{7}\quad \myfrac{77}{77}\par
\myfrac{8}{8}\quad \myfrac{88}{88}\par
\myfrac{9}{9}\quad \myfrac{99}{99}\par
\end{document}

Best Answer

Just introduce expandable macros to grab the first or the last digit of the string.

\documentclass{article}
\usepackage{fontspec}

%\setmainfont{STIXTwoText-Regular}
%\newfontfamily\fracfigs{STIXTwoText-Medium}[% compensate weight for scaling
%  Numbers = {Proportional,Lining},
%  Scale   = 0.75]

% Actual kerns wrong for this font, but everyone will have it:
\setmainfont{Latin Modern Roman}
\newfontfamily\fracfigs{Latin Modern Roman}[Scale = 0.75]
\def\getfirst#1#2\relax{#1}
\def\getlast#1#2\relax{\if\relax#2\relax#1\else\getlast#2\relax\fi}

\newcommand*\myFii{2}
\newcommand*\myFiv{4}
\newcommand*\myFvi{6}
\newcommand*\myFvii{7}
\newcommand*\myFix{9}
\newcommand*\myfrac[2]{%
  \edef\myfractmpn{\getlast#1\relax}% Really want just last char
  \edef\myfractmpd{\getfirst#2\relax}% Really want just first char
  \raisebox{0.5ex}{{\fracfigs#1}}%
  \ifx\myfractmpn\myFii       \kern 0.1em%
  \else\ifx\myfractmpn\myFvii \kern -0.05em%
  \else\ifx\myfractmpn\myFix  \kern 0pt%
  \else                       \kern 0.05em%
  \fi\fi\fi%
  \textfractionsolidus%
  \ifx\myfractmpd\myFiv       \kern -0.05em%
  \else\ifx\myfractmpd\myFvi  \kern 0pt%
  \else                       \kern 0.05em%
  \fi\fi%
  {\fracfigs#2}}

\begin{document}
\myfrac{1}{1}\quad \myfrac{11}{11}\par
\myfrac{2}{2}\quad \myfrac{22}{22}\par
\myfrac{3}{3}\quad \myfrac{33}{33}\par
\myfrac{4}{4}\quad \myfrac{44}{44}\par
\myfrac{5}{5}\quad \myfrac{55}{55}\par
\myfrac{6}{6}\quad \myfrac{66}{66}\par
\myfrac{7}{7}\quad \myfrac{77}{77}\par
\myfrac{8}{8}\quad \myfrac{88}{88}\par
\myfrac{9}{9}\quad \myfrac{99}{99}\par
\end{document}

enter image description here

Related Question