# Newcommand to Work for Each Digit in a Parameter

I was hoping someone might know how to modify the following to give the output in the second image without having to repeat the \myarc{~} for each digit.

I'm using LuaLaTeX.

\documentclass{article}
\usepackage{yhmath}

%underline
\newcommand{\myarc}[1]{\wideparen{#1}}

\begin{document}

$13\myarc{682}75$

\end{document}


Output:

Desired output:

It's very easy with xparse: we split the list “at nothing”.

\documentclass{article}
\usepackage{yhmath}
\usepackage{xparse}

\NewDocumentCommand{\myarc}{>{\SplitList{}}m}{%
\ProcessList{#1}{\wideparen}%
}

\begin{document}

$13\myarc{682}75$

\end{document}


The process is easy: each digit is fed to \wideparen, so that \myarc{123} becomes \wideparen{1}\wideparen{2}\wideparen{3}.

If you want to avoid overlapping, add a small space:

\documentclass{article}
\usepackage{yhmath}
\usepackage{xparse}

\NewDocumentCommand{\myarc}{>{\SplitList{}}m}{%
\ProcessList{#1}{\widerparen}%
}
\NewDocumentCommand{\widerparen}{m}{\mskip.3mu\wideparen{#1}\mskip.3mu}

\begin{document}

$13\myarc{682}75$

\end{document}


This is the same as before, but using \widerparen, that adds a bit of space on the left and right of the digit.

If you want to just put the arc over the first and the last digit in the argument, the code is a bit more complex. I've left \myarc* to print an arc over each digit.

\documentclass{article}
\usepackage{yhmath}
\usepackage{xparse}

\NewDocumentCommand{\widerparen}{m}{\mskip.3mu\wideparen{#1}\mskip.3mu}

\ExplSyntaxOn
\NewDocumentCommand{\myarc}{sm}
{
\IfBooleanTF{#1}
{ \enas_arc_all:n { #2 } } % the call is \myarc*{123}
{ \enas_arc_fl:n { #2 } }  % the call is \myarc{123}
}

\seq_new:N \l_enas_arc_input_seq

\cs_new_protected:Nn \enas_arc_all:n
{% this is essentially the same as the easy version
% split the input one digit at a time
\seq_set_split:Nnn \l_enas_arc_input_seq { } { #1 }
% apply \widerparen to each digit
\seq_map_inline:Nn \l_enas_arc_input_seq
{
\widerparen{##1}
}
}

\cs_new_protected:Nn \enas_arc_fl:n
{% we only want the arc on the first and last digit
% split the input one digit at a time
\seq_set_split:Nnn \l_enas_arc_input_seq { } { #1 }
% get the first item and store it
\seq_pop_left:NN \l_enas_arc_input_seq \l_enas_arc_digit_tl
% apply \wideparen to it
\wideparen { \l_enas_arc_digit_tl }
\seq_if_empty:NF \l_enas_arc_input_seq
{% there are other digits, get the last one
\seq_pop_right:NN \l_enas_arc_input_seq \l_enas_arc_digit_tl
\seq_if_empty:NTF \l_enas_arc_input_seq
{% they were only two, add some space
\mskip .6mu
}
{% there are digits in between, print them
\seq_use:Nn \l_enas_arc_input_seq {}
}
% print the last digit
\wideparen { \l_enas_arc_digit_tl }
}
}
\ExplSyntaxOff

\begin{document}

$13\myarc{6}8275$

$13\myarc{68}275$

$13\myarc{682}75$

$13\myarc*{682}75$

\end{document}


• It would be very helpful if you would explain that recently added code for me in detail. I'm new to using macros, and it would be great if I could understand what I'm doing. – ένας Apr 9 '17 at 9:20
• @EnasM I added some more comments, but explaining all of expl3 would be too much – egreg Apr 9 '17 at 9:30

Here, I set up an iteration, taking a token at a time from the argument and applying the \wideparen to it.

\documentclass{article}
\usepackage{yhmath}

%underline
\newcommand{\myarc}[1]{\myarchelp#1\relax}
\def\myarchelp#1#2\relax{\wideparen{#1}\ifx\relax#2\relax\else\myarchelp#2\relax\fi}

\begin{document}

$13\myarc{682}75$

\end{document}


The OP asks in a comment if the action can be limited to the first and last characters of the argument. Something like this seems to work

\documentclass{article}
\usepackage{yhmath}
\newcommand{\myarc}[1]{\myarcaux#1\relax}
\def\myarcaux#1#2\relax{\wideparen{#1}\ifx\relax#2\relax\else%
\myarchelp#2\relax\relax\fi}
\def\myarchelp#1#2#3\relax{\ifx\relax#2\wideparen{#1}\else#1\fi\ifx\relax#2\relax\else%
\myarchelp#2#3\relax\fi}

\begin{document}

$13\myarc{682}75$

$13\myarc{645682}75$

$13\myarc{62}75$

$13\myarc{6}75$

\end{document}


• What if I want to have only the first and last digits under the arc no matter how many digits I have? Ex, $13\myarc{6827}5$ should give output equivalent to $13\wideparen{6}82\wideparen{7}5$, $13\myarc{682}75$ should give output equivalent to $13\wideparen{6}8\wideparen{2}75$, and $13\myarc{68}275$ should give output equivalent to $13\wideparen{6}\wideparen{8}275$ – ένας Apr 9 '17 at 7:46
• @EnasM See my edit. – Steven B. Segletes Apr 10 '17 at 3:57