I have the following preamble:
\documentclass[11pt]{book}
% PACKAGES
% graphics packages
\usepackage{graphics}
\usepackage[pdftex]{graphicx}
% negative phantoms (\nphantom, \nhphantom, \nvphantom)
\catcode`@=11
\def\nvphantom{\v@true\h@false\nph@nt}
\def\nhphantom{\v@false\h@true\nph@nt}
\def\nphantom{\v@true\h@true\nph@nt}
\def\nph@nt{\ifmmode\def\next{\mathpalette\nmathph@nt}\else\let\next\nmakeph@nt\fi\next}
\def\nmakeph@nt#1{\setbox\z@\hbox{#1}\nfinph@nt}
\def\nmathph@nt#1#2{\setbox\z@\hbox{$\m@th#1{#2}$}\nfinph@nt}
\def\nfinph@nt{\setbox\tw@\null \ifv@ \ht\tw@\ht\z@ \dp\tw@\dp\z@\fi\ifh@ \wd\tw@-\wd\z@\fi \box\tw@}
% frequently used symbols
\renewcommand{\hbar}{h\nhphantom{h}\bar{\phantom{n}}}
The negative phantoms code does just what it says: it allows me to use \nhphantom (for example) to create a \phantom with negative width. I've then redefined \hbar to use a \nhphantom (I know there's already an \hbar command built into one of the mathematical symbols packages; that's beside the point.)
The following codes work just fine:
\begin{document}
$\hbar$
\end{document}
and
\begin{document}
\begin{figure}
{\includegraphics[width=\linewidth]{fig-Raman-spontaneous.pdf}}
\caption{$h$}
\end{figure}
\end{document}
However, if I add an $\hbar$ in the figure caption, the code doesn't compile (I'm using XeLaTeX). Looking at the compilation log, it seems the \fi in the definition of the negative phantoms is what's causing the problem:
Is there a way around this, preferably one that'll allow \fi to be used inside figure captions instead of forcing me to redefine the negative phantoms or the \hbar command?
Thanks in advance.


graphicsand as you are using xetex don't use the pdftex option tographicxso\usepackage{graphics} \usepackage[pdftex]{graphicx}should be just\usepackage{graphicx}– David Carlisle Jan 03 '19 at 16:13\renewcommand{\hbar}{h\nhphantom{h}\bar{\phantom{n}}}by\protected\def\hbar{h\nhphantom{h}\bar{\phantom{n}}}– David Carlisle Jan 03 '19 at 16:15graphicsand thepdftexoption? – Rain Jan 03 '19 at 16:52graphicsis loaded bygraphicxso explicitly loading it is just harmless but does nothing useful, as you are not usingpdftexthen specifying[pdftex]is just wrong, Nothing would work at all if it was activated, you just got lucky that via an accidental code pathgraphicsdefaulted toxetexand then the unusedpdftexoption wasn't flagged as an error. – David Carlisle Jan 03 '19 at 17:02\hbar, as the standard definition yields a better result. In the picture (click) you see on the left your redefined\hbarand on the right the standard. – egreg Jan 03 '19 at 17:51\hbarcommand while using another font it uses Computer Modern for the h for some reason, hence the redefinition. – Rain Jan 03 '19 at 18:21