I want to make an apply
function that can apply any single-argument command to a comma-separated list of arguments, and print the results separated by commas. (ETA: I want to make the command from scratch, i.e., without using another package dependency, as a way of understanding how all the subroutines work.)
I have a partially working solution, but it doesn't work when the command to be applied is complicated. See the following example, where \lowercase
can be effectively applied across a list, but \ComplicatedCommand
and \MoreComplicatedCommand
cannot:
\RequirePackage{luatex85}
\documentclass[letterpaper]{article}
% This command is for applying a single command to a comma-separated list of tokens, and listing the results separated by ", "
\makeatletter
\newcommand{\apply}[3][, ]{
% #1: optional separator to print between applications; default=[, ]
% #2: command to apply;
% #3: list to apply command to
\def\itemsep{\def\itemsep{#1}} % first call to \itemsep prints nothing; later calls print #1
\@for \listelement:=#3\do{\itemsep#2\expandafter{\listelement}}%
}
\makeatother
\begin{document}
\apply{\lowercase}{THESE,WORDS,PRINT,IN,LOWERCASE.}
\newcommand{\ComplicatedCommand}[1]{\lowercase{#1}}
\apply{\ComplicatedCommand}{WHY,DO,THESE,WORDS,NOT,PRINT,IN,LOWERCASE?}
\newcommand{\MoreComplicatedCommand}[1]{\lowercase{\uppercase{{\lowercase{#1}}}}}
\apply{\MoreComplicatedCommand}{ULTIMATELY,I,WANT,APPLY,TO,MAKE,THESE,LOWERCASE,AS,WELL.}
\end{document}
The result:
Can anyone fix my \newcommand{\apply}
so that it can work for any \ComplicatedCommand
as argument #2
? If so, please provide a full working example, all the way from \documentclass
to \end{document}
, so anyone who reads the answer in the future can copy and paste it to see if it still works for their version of (Lua)TeX.