I'm trying to make a package-free command that applies a command across a comma-separated list, and then adds the word ``and'' to the output as is grammatically appropriate. I've already gotten some help with building an \apply
command, and now I'm working on an \addandtolist
command. The \apply
and \addandtolist
commands seem to be working separately, but not in combination. Specifically, in the following test document, test 3 fails, but if you remove test 3 from the doc, tests 1 and 2 succeed:
\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
\def\zz{\itemsep#2}%
\@for \listelement:=#3\do{\expandafter\zz\expandafter{\listelement}}%
}
\makeatother
% The following command is for adding an "and" to a comma-separated list, if grammatically appropriate:
\newcommand{\addandtolist}[1]{%
\directlua{
s = string.explode("#1","")
commacounter=0
lastcomma=-1
for key, value in pairs(s) do
if value=="," then
commacounter=commacounter+1
lastcomma=key
end
end
if commacounter==1 then
s[lastcomma]=" and "
elseif commacounter>1 then
s[lastcomma]=", and "
end
t=""
for key, value in pairs(s) do
t=t..value
end
tex.sprint(t)
}
}
\begin{document}
\noindent Test 1 (succeeds): As hoped, these letters print in lowercase:\\
\apply{\lowercase}{U,V,W,X,Y}\\
\newcommand{\ComplicatedCommand}[1]{\lowercase{#1}}
\apply{\ComplicatedCommand}{U,V,W,X,Y}\\
\
\noindent Test 2 (succeeds): These lists get an ``and'' added to them if appropriate:\\
\addandtolist{u}\\
\addandtolist{u, v}\\
\addandtolist{u, v, w, x, y}\\
\noindent Test 3 (fails): Calling the ``addandtolist" command on the output of the apply command returns at error:\\
\addandtolist{\apply{\lowercase}{U,V,W,X,Y}}\\
\addandtolist{\apply{\ComplicatedCommand}{U,V,W,X,Y}}\\
\end{document}
Results when test 3 is excluded:
Error when test 3 is included:
It would be cool to get the final result working with Lua, but I would be okay with non-Lua solutions as well. I do want to keep the result free of any \usepackage
calls, though, because I want to avoid calling packages that could end up interfering with other packages.
Thanks a bunch for any help with this!