I would like to extract the first character. Using xstring
's \StrChar
works fine for simple cases. However, when the paramter has an additional brace group, I need some expansion magic to get that to work.
My naive attemps to 1) Test the first char with \IfBeginWith{#1}{\{}{}{}
and 2) use \fullexpandarg
didn't resolve this issue.
The MWE below produces
where as rhe desired output is a
for both cases.
Related Questions:
Code:
\documentclass{article}
\usepackage{xstring}
\newcommand*{\ExtractFirstChar}[1]{%
%% #1 = string to extract first char from
%\fullexpandarg
%\IfBeginWith{#1}{\{}{%
% \StrChar{#1}{2}[\FirstChar]%
%}{%
\StrChar{#1}{1}[\FirstChar]%
%}%
First char of '#1' is '\FirstChar'\par
}
\begin{document}
\ExtractFirstChar{abc}
\ExtractFirstChar{{a,b,c}}
\end{document}
\detokenize{#1}
gives an interesting result, but nota
.{\'{e}abc}
or{éfg}
or\textbf{x}yz
or\hspace{1em}xyz
? neither "first" nor "character" are well defined terms here.\StrRemoveBraces
seems to work for my case.