As per How to overwrite siunitx's binary prefixes, any changes to the binary prefixes need to be deferred to be after \begin{document}. However, it seems that if I am using those same binary prefixes in the index that I need to repeat the same declarations in the preamble. Trying to determine what is the correct way to deal with this issue and not have to repeat the same declarations.
To see the problem comment out the line denoted by
< ---- This needs to be repeated
If instead the \AtBeginDocument declaration is removed, then the unit bit is no longer displayed in red.
Notes:
- I have several other issues with math content in the last parameter of
\AddIndexEntryand am pretty sure it is related to "robustification"/expansion so if it is obvious form this example it will save me time posting further similar questions. This specific question is distinct in that it is specifically related tosiunitxand binary-prefixes.
Code:
\documentclass{article}
\usepackage{imakeidx}
\usepackage{xparse}
\usepackage{siunitx}
\usepackage{xcolor}
\usepackage{hyperref}
\newcommand*\lettergroup[1]{\subsection{#1}}
\newcommand*{\IndexWithName}[2]{%
% #1 = word to index
% #2 = index name
\index[#2]{#1}%
}%
\newcommand*{\FormatIndexEntry}[2]{%
\textcolor{blue}{#1} #2%
}%
%% Defer the binary units until AFTER \begin{document} as per
%% https://tex.stackexchange.com/questions/287579/how-to-overwrite-siunitxs-binary-prefixes
\sisetup{binary-units=true}%
\AtBeginDocument{%
\DeclareSIUnit\bit{\textcolor{red}{bit}}%
}
\DeclareSIUnit\bit{\textcolor{red}{bit}}% < ---- This needs to be repeated
\NewDocumentCommand{\AddIndexEntry}{%
O{}% #1 = index name
m% #2 = word to index this under
m% #3 = indexed term
m% #4 = symbol
}{%
\expandafter\IndexWithName\expandafter{%
#2!\FormatIndexEntry{#3}{#4}%
}{#1}%
}
\newcommand{\indexopt}[2]{\index[#2]{#1}}
\makeindex[title={Main Index},columns=1,program=texindy]
\makeindex[title={Name Index},columns=1,program=texindy,name=Name]
\indexsetup{level=\section}
\begin{document}
\SI{1}{\bit}
\AddIndexEntry[Name]{Bytes}{Bit}{symbol: $\si{\bit}$}
\clearpage
\setcounter{secnumdepth}{0}
\printindex[Name]
\end{document}
