4

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 \AddIndexEntry and 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 to siunitx and 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}
2

As usual, it's an expansion problem; when the write happens, the error is that \bit is not defined, which it isn't unless we're processing \si or \SI.

I'm not sure why the declaration seems to need being required also in the preamble, but you get a better setup if you avoid expanding the index entry.

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

\NewDocumentCommand{\FormatIndexEntry}{mm}{%
    \textcolor{blue}{#1} #2%
}


%% Defer the binary units until AFTER \begin{document} as per
%% http://tex.stackexchange.com/questions/287579/how-to-overwrite-siunitxs-binary-prefixes
\sisetup{binary-units=true}
\AtBeginDocument{%
    \DeclareSIUnit\bit{\textcolor{red}{bit}}%
}

\NewDocumentCommand{\AddIndexEntry}{%
    O{}% #1 = index name
      m% #2 = word to index this under
      m% #3 = indexed term
      m% #4 = symbol
}{%
    \expandafter\IndexWithName\expandafter{%
        #2!#3@\FormatIndexEntry{#3}{\unexpanded{\unexpanded{#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}

Note that I made \FormatIndexEntry robust and also added #3@ for better collation of the entry.

enter image description here

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.