I am trying to create a reliable command that turns the first character of it's input to uppercase. So far I have no success, especially since it needs to work when the inputstring contains commands itself. Most notably, my attempts fail when the command contains itself. Appearently, the following does not work:
\documentclass{scrartcl}
\usepackage{xstring}
\usepackage{etoolbox}
\begin{document}
\newcommand{\singleupper}[1]{\uppercase{\StrLeft{#1}{1}}\StrGobbleLeft{#1}{1}}
\newcommand{\optcap}[2]{\ifstrequal{cap}{#2}{\singleupper{#1}}{#1}}
\optcap{\optcap{asdf}{}}{cap}
\end{document}
resulting in:
! Undefined control sequence. \@xs@StrLeft@@ ...\@xs@arg@ii {#2}\edef \@xs@call {\noexpand \@testopt {\noe...
How can I do this properly an why is my attempt failing?
edit
I have seen some nice answers all in some sense involving a secondary language (latex3, lua...) is there no fully expandable method for this in latex2e (possibly using packages)?


\StrLeft .... That's the next thing TeX sees, so that's what it feeds to\uppercase. At least, I think so. TeX doesn't work from the inside out. It works from left to right. – cfr Jan 29 '18 at 04:32\StrLeft{\StrLeft{asdf}{3}}{1}fails with basically the same error. See section 3.2 of thexstringdocumentation: The macros of this package are not purely expandable, i.e. they cannot be put in the argument of an\edef. Nestling macros is not possible neither. (You can also see the alternatives proposed.) – ShreevatsaR Jan 29 '18 at 07:30