This might be a very basic problem (as a follow-up to this question) but I cannot figure out how to do it: I need a macro \mycmd{sometext}
that yields a string depending on the first character of its one argument. This resulting string should afterwards be capitalized by \makefirstuc
.
\documentclass{article}
\usepackage{xparse} % used in example below
\usepackage{pgffor} % used in example below
\usepackage{xstring} % used in example below
\newcommand{\mycmd}[1]{
% here, I define a list of letters {a,e,i,o,u,A,E,I,O,U}
% that will lead to the output "an " in case
% the argument string starts by one of them.
% Otherwise the output shall be "a ".
}
\begin{document}
\mycmd{somestring} % should print "a "
\makefirstuc{\mycmd{sometext}} % should print "A "
\mycmd{otherstring} % should print "an "
\makefirstuc{\mycmd{otherstring}} % should print "An "
\end{document}
I tried realizing this using the pfgfor
package for the loop and therein comparing the first character with \IfBeginWith
from the xstring
package. However, this apparently leads to unexpandable commands so that \makefirstuc
fails. How can one achieve an expandable command with this functionality?
So far I created the following unexpandable command:
\NewDocumentCommand{\mycmd}{m}{%
\def\undefart{a\space}%
\foreach \c in {a,e,i,o,u,A,E,I,O,U}{%
\IfBeginWith{#1}{\c}{\global\def\undefart{an\space}}{}%
}%
\undefart%
}