5

I would like to have an upright \hbar glyph in my documents and I'm using unicode-math. The example in https://tex.stackexchange.com/a/527104/218142 doesn't seem to work when I load unicode-math. The bar obviously moves relative to the h. Why does this happen, and how can I get the upright \hbar when loading unicode-math?

My MWE:

% !TEX program = lualatexmk
% !TEX encoding = UTF-8 Unicode

\documentclass{article}
\usepackage{unicode-math}

\begin{document}
\renewcommand*{\hbar}{{\mathpalette\hbaraux\relax\symup{h}}}
\newcommand*{\hbaraux}[2]{\sbox0{\mathsurround=0pt$#1\mathchar"AF$}\mkern-1mu\lower.07\ht0\box0\mkern-8mu}

\( \hbar \scriptstyle\hbar \scriptscriptstyle\hbar \)
\end{document}

This MWE produces the following output:

MWE output showing the bar doesn't stay put relative to the h

7

You don't need to construct things by hand, you want U+0127 ħ

enter image description here

\documentclass{article}
\usepackage{unicode-math}

\begin{document}
\renewcommand*{\hbar}{\mathrm{^^^^0127}}

 \( \hbar \scriptstyle\hbar \scriptscriptstyle\hbar \)
\end{document}
3
  • 1
    Blindingly simple. Aug 27 at 19:44
  • It doesn't work if I change \mathrm to \symup. Why is that? Aug 27 at 19:47
  • 2
    the latin modern math font hasn't got a glyph in slot 0127 you need to get it from the text font. @LaTeXereXeTaL Aug 27 at 19:48
3

You can compose the character, but you have to do it the proper way:

\documentclass{article}
\usepackage{unicode-math}

\makeatletter
\AtBeginDocument{%
  \DeclareRobustCommand{\hbar}{{\mathpalette\hbar@\relax\symup{h}}}%
}
\newcommand*{\hbar@}[2]{%
  \makebox[0pt][l]{\raisebox{-0.07\height}{$\m@th#1\mkern-1mu\mathchar"AF$}}%
}
\makeatother

\begin{document}

\( \hbar \scriptstyle\hbar \scriptscriptstyle\hbar \)

\end{document}

enter image description here

2
  • The box issue was confusing me. I still do not entirely understand it and need to study it more. I have never seen \m@th before. Where is it documented? Aug 27 at 20:47
  • 1
    @LaTeXereXeTaL \m@th is shorthand for \mathsurround=0pt. The idea here is to make a zero width box that protrudes left 1mu.
    – egreg
    Aug 27 at 20:57

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.