1

I would like to apply a formatting to some of the page numbers in an index. In the MWE below, when I use \index{} the page number should be in black and when I use \MyIndex{} the page number should be formatted with \FormatPageNumber{}:

The MWE below produces the results on the left. What changes do I need to make to the \MyIndex{} macro to achieve the results on the right:

enter image description here

Code:

\documentclass{article}

\usepackage{xcolor}

\usepackage{imakeidx}
\makeindex

\newcommand*{\FormatPageNumber}[1]{\textcolor{red}{\bfseries#1}}
\newcommand*{\MyIndex}[1]{%
    \begingroup
        % Want to apply \FormatPageNumber{} to this page number ONLY in the index.
        \index{#1}%
    \endgroup
}%

\begin{document}

stuff\index{test}
\clearpage
more stuff\MyIndex{test}

\printindex

\end{document}
2

Formatting commands for the page number can be added using the syntax term|formatting, see page 5 of the manual.

MWE:

\documentclass{article}
\usepackage{xcolor}\pagecolor{white}

\usepackage{imakeidx}
\makeindex
\newcommand*{\FormatPageNumber}[1]{\textcolor{red}{\bfseries#1}}
\newcommand*{\MyIndex}[1]{%
    \begingroup
        \index{#1|FormatPageNumber}%
    \endgroup
}%

\begin{document}
stuff\index{test}
\clearpage
more stuff\MyIndex{test}

\printindex

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.