# Make indexed words appear bold in body text

I would like to have every word that appears in the index to be set in boldface in the main text, sort of the opposite of this question (where everything typed in bold should be indexed).

Here's an MWE:

\documentclass{article}

\usepackage{lipsum}

\usepackage{imakeidx}
\makeindex

\begin{document}

\section{Minimal Working Example}

\lipsum[1-3]
minimal\index{minimal}
\lipsum[4-6]
working\index{working}
\lipsum[7-9]
example\index{example}

\printindex

\end{document}


I think this does what you want. Of course it doesn't boldface a word that appears in the index when that word appears in the text somewhere it's not indexed. That would be much harder ...

\documentclass{article}

\newcommand{\boldindex}[1]{%
\textbf{#1}\index{#1}%
}

\usepackage{lipsum}

\usepackage{imakeidx}
\makeindex

\begin{document}

\section{Minimal Working Example}

\lipsum[1-3]
\boldindex{minimal}
\lipsum[4-6]
\boldindex{working}
\lipsum[7-9]
\boldindex{example}

\printindex

\end{document}

• This would not work for the case when imakeidx's feature of \index[someotherindex]{foo} is used. And what is with \index{foo!bar}. This would appear as well. – user31729 Sep 15 '15 at 18:13
• @ChristianHupfer True enough. Making the fancy features work would require more argument parsing somewhere (maybe already done in the \index code). That's beyond my pay grade - someone else will have to step up if the OP needs those features. – Ethan Bolker Sep 15 '15 at 18:30
• Yes parsing is ... weird. There is also the @ character for sorting and | etc. (+1) anyway! – user31729 Sep 15 '15 at 18:37
• @EthanBolker Subindices would certainly be appreciated. Also, is there a way to make this work with indices containing \lstinline elements from the listings package? – Casimir Sep 16 '15 at 8:07
• You seem to be asking for more and more features. If you use those features rarely, consider coding them by hand each time. If you want them automated, please edit your question to include an MWE that illustrates all your needs. Perhaps someone will be able to help. – Ethan Bolker Sep 16 '15 at 14:23

This is some extension to Ethan Bolker's answer, which takes imakeidx into account and prints the last index entry in a foo!bar!stuff index list.

\documentclass{article}

\usepackage{blindtext}

\usepackage{etoolbox}
\usepackage{imakeidx}
\usepackage{xstring}

\AtBeginDocument{%
\newcommand{\indexbold}[2][]{%
\StrCount{#2}{!}[\mycount]% Count the ! in the string
\ifnum\mycount = 0
\textbf{#2}%
\else%
\expandarg\StrBehind[\mycount]{#2}{!}[\myrestindex]%  get the last entry
\textbf{\myrestindex}%
\fi%
\ifblank{#1}{%
\index{#2}%
}{%
\index[#1]{#2}%
}%
}%
}

\makeindex
\begin{document}

\indexbold{foo}
\blindtext[5]

\indexbold{foo again}
\blindtext[5]

\indexbold{foo!bar}
\printindex

\end{document}

• Of course my approach won't work if the ! is part of the index entry itself – user31729 Sep 15 '15 at 18:37
• I had hoped that there was some implementation of this that would allow me to continue typesetting indexed words in a different face. Using your \indexbold on something like \newcommand{\macro}{\texttt{macro}} throws an error Use of \\indexbold doesn't match its definition.. – Casimir Sep 16 '15 at 7:56
• @PacificOrion: Now you are editing the question away from your initial version. There was nothing of \macro{...} in your post above. – user31729 Sep 16 '15 at 16:13
• It's true that I did not initially mention macros. But I don't think this counts as 'editing' my question away from the original. I'm still posing the same question but asking for a more robust solution. – Casimir Sep 16 '15 at 16:17
• @PacificOrion: Well, yes and no. What do you mean by using \indexbold on \newcommand{` etc.? What do you want to index there? – user31729 Sep 16 '15 at 16:27