## 2009-06-12

### Using \romannumeral in TeX to do multiple macro expansions

This blog post demonstrates a TeX macro hack: using \romannumeral to expand many macros in a single expansion. The example goal is to define a macro \stars which (when called properly) expands to the specified number of stars as a single expansion. Using a single expansion only makes the macro work in an \expandafter\def...{...} context. The similar macro given in The TeXbook doesn't work in this context because it needs multiple expansions.
\documentclass{article} %** Usage: \romannumeral\stars{NUMSTARS} %** This expands to a NUMSTARS stars (* tokens) in a single expansion step. \def\stars#1{%   \expandafter\mtostar\expandafter{\expandafter}\romannumeral\number#1 000z} \def\firstoftwo#1#2{#1}% \def\secondoftwo#1#2{#2}% \def\mtostar#1#2{%   \ifx#2z\expandafter\firstoftwo\else\expandafter\secondoftwo\fi   {0 #1}{\mtostar{#1*}}% } \begin{document} \expandafter\def\expandafter\mystars\expandafter{\romannumeral\stars{4}}% \texttt{(\meaning\mystars)} \end{document}