Make macro with string variable and returning array

arraysmacrosmath-mode

I want to make a macro "\mymacro" which has two string variables and returns array.

The two variables are arbitrary string characters and has the same length.
The length of the strings are also arbitrary.
"\mymacro" returns an array whose rows are the string variables.
For example,

\mymacro{abc}{def}

should return

\begin{array}{ccc}
   a&b&c\\\hline
   d&e&f
\end{array}

Can I make such macro?
Thank you for your cooperation.

Best Answer

You can iterate over the list the tricky bit is to avoid adding a &at the end here I do add it but use \omit to avoid generating a visible empty column (with an over-long \hline)

enter image description here

\documentclass{article}

\usepackage{amsmath}

\newcommand\mymacro[2]{\begin{matrix}\zz#1\zzz\\\hline\zz#2\zzz\end{matrix}}


\def\zz#1{#1&\zz}
\def\zzz#1#2{\omit}

\begin{document}


$\mymacro{abc}{def}$

$\mymacro{123456}{aaabbb}$

\end{document}
Related Question