I am using the siunitx pacakge and trying to detect if a unit is defined. My initial assumption was that this would simply be:
\ifcsdef{#1}{#1 exists.}{#1 does NOT exist.}
This works fine if the unit declaration is not deferred until \AtBeginDocument. So, with the units defined as
\DeclareSIUnit\milliliter{\textnormal{mL}}% <--- Can detect this just fine
\AtBeginDocument{%
\DeclareSIUnit\centiliter{\textnormal{cL}}% <--- But fails with this?
}%
detecting \milliliter is fine but detecting \centiliter is not so easy. The MWE below yields:
Notes:
- I need to defer some definitions until
\AtBeginDocumentas per How to overwrite siunitx's binary prefixes and would like to have a single method to detect them.
Code:
\documentclass{article}
\usepackage{etoolbox}
\usepackage{siunitx}
\DeclareSIUnit\milliliter{\textnormal{mL}}% <--- Can detect this just fine
\AtBeginDocument{%
\DeclareSIUnit\centiliter{\textnormal{cL}}% <--- But fails with this?
}
\newcommand*{\TestIfUnitExists}[1]{%
\par
\ifcsdef{#1liter}{%
#1liter exists.
}{%
#1liter does NOT exist.
}%
}%
\begin{document}
\TestIfUnitExists{giga}%
\TestIfUnitExists{milli}% <--- milliliter exists
\TestIfUnitExists{centi}% <--- centiliter also exists (but test fails)
\TestIfUnitExists{}% <--- liter exists as well
\end{document}


