0

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.

1 Answers1

2

\makefirstuc is not expandable, but you can use \text_titlecase:n:

\documentclass{article}

\usepackage{xparse}

\usepackage{imakeidx} \makeindex[title = Index, columns =1]

\ExplSyntaxOn \NewDocumentCommand{\fooa}{O{#2} m}{ \textbf{#2} \index{\text_titlecase:n{#2}} }

\NewDocumentCommand{\foob}{O{#2} m}{ \textbf{#2} \index{\text_titlecase:n{#2}} } \ExplSyntaxOff

\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}

enter image description here

Ulrike Fischer
  • 327,261
  • 1
    This partially works, but \text_titlecase:n has the side effect that it decapitalises other words, turning \foo{adjective Noun} into Adjective Noun in the index. I really want to only change the first letter and let the rest untouched. In the expl3 documentation I can't find \text_titlecase:n or similar commands, where can I find them? – Maximilian Keßler May 21 '21 at 09:14
  • The commands are documented in interface3.pdf – Ulrike Fischer May 21 '21 at 09:29
  • I can look later at the first word question, I'm out now. – Ulrike Fischer May 21 '21 at 09:38
  • text_titlecase_first:n seems 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
  • @UlrikeFischer Do I get this right?: Neither \fooa nor \foob use/deliver their first (optional) argument? \foob seems to have the same definition as \fooa? – Ulrich Diez May 26 '21 at 20:07