1

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:

enter image description here

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.

Rain
  • 194
  • 1
    unrelated but don't load graphics and as you are using xetex don't use the pdftex option to graphicx so \usepackage{graphics} \usepackage[pdftex]{graphicx} should be just \usepackage{graphicx} – David Carlisle Jan 03 '19 at 16:13
  • 1
    replace \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:15
  • @DavidCarlisle Thanks for the tips. Why shouldn’t I use graphics and the pdftex option? – Rain Jan 03 '19 at 16:52
  • 1
    graphics is loaded by graphicx so explicitly loading it is just harmless but does nothing useful, as you are not using pdftex then specifying [pdftex] is just wrong, Nothing would work at all if it was activated, you just got lucky that via an accidental code path graphics defaulted to xetex and then the unused pdftex option wasn't flagged as an error. – David Carlisle Jan 03 '19 at 17:02
  • I'm not sure what's the reason for redefining \hbar, as the standard definition yields a better result. In the picture (click) you see on the left your redefined \hbar and on the right the standard. – egreg Jan 03 '19 at 17:51
  • @egreg Font reasons. The font I'm using doesn't have a symbol for the reduced Planck constant, so it uses the default font for it, which looks awful in the middle of a wall of text which is in a different font. – Rain Jan 03 '19 at 18:14
  • Neither has the Computer Modern font. It also uses a displaced bar accent over the h. – egreg Jan 03 '19 at 18:18
  • OK, but if I call the default \hbar command while using another font it uses Computer Modern for the h for some reason, hence the redefinition. – Rain Jan 03 '19 at 18:21

2 Answers2

3

You have to use \DeclareRobustCommand{\hbar}{h\nhphantom{h}\bar{\phantom{n}}} instead of \renewcommand

See What is the difference between Fragile and Robust commands? for further information on why.

Raven
  • 3,023
  • Thanks, will try this as soon as I get home. I didn’t understand everything in that link, but I think I get the basics. :) – Rain Jan 03 '19 at 17:05
  • @Rain Don't worry - I haven't fully understood the whole robust/fragile stuff either. It's sufficient to know that it exists and how to fix it :) – Raven Jan 03 '19 at 17:24
  • 1
    OK, just tried this and it works. It's also the answer which most concisely solves my problem and the one that most directly addresses it, so I've accepted it. Thanks! – Rain Jan 03 '19 at 18:07
3

Using negative phantoms is not the best way to deal with the problem; it's simpler to typeset the bar in a zero width box (I also added a slight backing up, which you can tailor to your needs).

The main issue is that \hbar should be made robust.

\documentclass[11pt]{book}
\usepackage{amsmath,mathtools}

% PACKAGES
% graphics packages
\usepackage{graphicx}

\renewcommand{\hbar}{{%
  \mathrlap{\mspace{-1mu}\bar{\phantom{x}}}h%
}}
\MakeRobust{\hbar}

\begin{document}

\begin{figure}
\centering

\includegraphics[width=0.5\linewidth]{example-image}

\caption{$\hbar$}

\end{figure}

\end{document}

enter image description here

egreg
  • 1,121,712