1

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}
Janosh
  • 4,042
  • You should be able to do this by renewing the \index macro. That's a little tricky. Here's an answer that shows you how: http://tex.stackexchange.com/questions/267366/renewcommand-index – Ethan Bolker Sep 15 '15 at 18:03

2 Answers2

4

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}
egreg
  • 1,121,712
Ethan Bolker
  • 9,333
  • 3
  • 42
  • 69
  • 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. –  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! –  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? – Janosh Sep 16 '15 at 08:07
  • 1
    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
  • @EthanBolker Thanks for your constructive advice. As there were only few instances where I needed this, I did in fact implement it by hand each time. I didn't know if this would be easy or hard to implement before asking. I just asked in case it was easy. – Janosh Sep 16 '15 at 16:37
3

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 –  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.. – Janosh Sep 16 '15 at 07:56
  • @PacificOrion: Now you are editing the question away from your initial version. There was nothing of \macro{...} in your post above. –  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. – Janosh 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? –  Sep 16 '15 at 16:27
  • I have a macro to typeset 'C++'. It is implemented as \newcommand{\cpp}{C\texttt{++}}. I stumbled upon this error when trying to index \index{\cpp standard library}. – Janosh Sep 16 '15 at 16:32
  • @PacificOrion: I'll try –  Sep 16 '15 at 16:33
  • Thanks but see the comments below Ethan's answer before spending too much time with the problem. Since there weren't that many of them, I avoided these problems by tampering on a case by case basis. – Janosh Sep 16 '15 at 16:41
  • @PacificOrion: I've added the \expandarg in front of StrBehind! The \cpp... stuff must be expanded before \StrBehind can act on it –  Sep 16 '15 at 16:41
  • Unfortunately, I'm still getting the error Use of \\indexbold doesn't match its definition.. – Janosh Sep 16 '15 at 16:46
  • @PacificOrion: well, I can compile my example above –  Sep 16 '15 at 16:49
  • Yes, but I don't see where it uses \cpp or a similar macro inside \indexbold. – Janosh Sep 16 '15 at 17:12
  • @PacificOrion: I don't understand –  Sep 16 '15 at 18:49