In my document, i want to highlight specific words and add them to the index simualtaneously. The idea is to introduce key words with \foo{word}, making it appear typeset differently and appear in the index, so that one can quickly find the corresponding definition. Additionally, i want an optional argument for long key words, so that i can write \foo[Word]{word with many adjectives} to have word with many adjectives printed in the document and 'Word' appear in the index.
However, when not specifying the optional argument, i want to uppercase the first letter, as i want a capitalized index. This is useful when having a \foo{adjective} in the text (that should be typeset small), but that should appear capitalized in the index. For that i use the package mfirstuc providing \makefirstuc{foo} to output Foo.
I know that \foo[Adjective]{adjective} would do the job, but this is very tedious to write out each time.
I also know that
\NewDocumentCommand{\foo}{O{#2} m}{
\textbf{#2}
\index{\makefirstuc{#2}}
}
will not work, as it puts \makefirstuc{<#2>} in the .ind file, making it alphabetically ordered under \ which is not what i want. This is also mentioned in the imakeidx documentation, but no fix is given there.
So i tried to use \expandafter giving me
\NewDocumentCommand{\foo}{O{#2} m}{
\textbf{#2}
\index\expandafter{\makefirstuc{#2}}
}
but then the argument is printed twice in the text and does not appear in the index. Also, this question has not solved my issues, i don't (really) understand it, and i did not get expl3 to run properly.
So, how can i make \index expand its entry before writing to the .ind file, while not expanding it to plain text (like my second macro did)?
Here is a MWE:
\documentclass{article}
\usepackage{xparse}
\usepackage{mfirstuc}
\usepackage{imakeidx}
\makeindex[title = Index, columns =1]
\NewDocumentCommand{\fooa}{O{#2} m}{
\textbf{#2}
\index{\makefirstuc{#2}}
}
\NewDocumentCommand{\foob}{O{#2} m}{
\textbf{#2}
\index\expandafter{\makefirstuc{#2}}
}
\begin{document}
In this document i \fooa{highlight} some \foob{stuff} that i want to appear in the index in
uppercase.
\index{Alphabetical order}
\printindex
\end{document}
Note how the resulting order is not alphabetic.

\text_titlecase:nhas the side effect that it decapitalises other words, turning\foo{adjective Noun}intoAdjective Nounin the index. I really want to only change the first letter and let the rest untouched. In theexpl3documentation I can't find\text_titlecase:nor similar commands, where can I find them? – Maximilian Keßler May 21 '21 at 09:14text_titlecase_first:nseems to do the job (at least for now it does what I want``` So thank you! – Maximilian Keßler May 21 '21 at 09:39\fooanor\foobuse/deliver their first (optional) argument?\foobseems to have the same definition as\fooa? – Ulrich Diez May 26 '21 at 20:07