5

Using the command (defined in units.sty) \unit[value]{dimensions}, the dimensions are set using \mathrm. I am working on a presentation that uses the standard Beamer sans serif font (can't remember name), which means that whenever I use the \unit command, the dimensions stand out because of a different font. Is there a way to redefine what font to use for the dimensions?

By the way, I am using LyX, so the \unit command is invoked by calling \unittwo and then inserting value in first box and dimensions in second box. I can't seem to find a way to change the font, except by avoiding the \unit commands altogether.

I was looking for something like (in the preamble) \newcommand{\unit}{\sffamily\unit} but that doesn't work. (I am very inexperienced with using newcommand)

Kaspar H
  • 155
  • 4
    Where is \unit defined? Or what package are you loading that provides \unit? – Werner Aug 01 '13 at 00:01
  • I believe it is defined in units.sty, see http://www.ctan.org/tex-archive/macros/latex/contrib/units – Kaspar H Aug 01 '13 at 09:48
  • The SI standard says "Print unit symbols in roman (upright) type regardless of the type style used in the surrounding text." LaTeX is trying to help you do the right thing...you should let it. – Joe Hass Aug 01 '13 at 11:06
  • It seems weird to me. I agree that the units should always be upright, and apparently a half space next to the value. But can it really be the case that the SI system has defined which font to use for units? So that if surrounding text is sans serif, then the units will ALWAYS be WITH serif anyway? – Kaspar H Aug 01 '13 at 18:37
  • According to the manual, the units are typeset in the font of the surrounding text when in text mode, but in roman font when in math mode, so I guess you're writing the units as part of a math expression. If so, a tedious workaround is to place all the units in a \text{} (requires amsmath). – Torbjørn T. Aug 02 '13 at 12:35

1 Answers1

6

The units package comments (in units.dtx, lines 328-329):

%    The use of upright fonts for the dimension is forced in math
%    mode.  Units in text mode are typeset with the currently active font.

Here's a MWE that highlights this output:

enter image description here

\documentclass{beamer}% http://ctan.org/pkg/beamer
\usepackage{units}% http://ctan.org/pkg/units
\begin{document}
some text \unit[17]{mm} some more text

some text $\unit[17]{mm}$ some more text
\end{document}

To avoid this, you have a number of choices:

  1. Redefine the way \unit works to insert \mathsf instead of \mathrm. You can copy the definition from the source and update it accordingly. Here's an update that just fixes the use of \mathrm:

    \makeatletter
    \DeclareRobustCommand*{\unit}[2][]{%
      \begingroup
        \def\0{#1}%
        \expandafter
      \endgroup
      \ifx\0\@empty
      \else
        #1%
        \ifthenelse{\boolean{B@UnitsLoose}}{~}{\,}%
      \fi
      \ifthenelse{\boolean{mmode}}{\mathsf{#2}}{#2}% <---- Fixed
    }
    \makeatletter
    

    Place the above in your document preamble (Document > Settings... > LaTeX Preamble). For an explanation of why the macro is wrapped within a \makeatletter...\makeatother pair, see What do \makeatletter and \makeatother do?

  2. Use etoolbox's cousin xpatch to patch \unit in a similar way to the above suggestion:

    \usepackage{xpatch}% http://ctan.org/pkg/xpatch
    % \xpatchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
    \xpatchcmd{\unit}{\mathrm}{\mathsf}{}{}
    
  3. Make a global change in your document, letting all \mathrm be equivalent to \mathsf:

    \let\mathrm\mathsf% \mathrm is equivalent to \mathsf
    

In all of the above instances, the MWE now outputs:

enter image description here

Nowadays it is advised to use the siunitx package instead, since it allows for more control over unit formatting (globally and locally).

Werner
  • 603,163
  • Perfect! Great explanation and examples.I went with solution 2. which worked right away. – Kaspar H Aug 02 '13 at 20:21
  • Great! I also applied solution 2 to \unitfrac: \xpatchcmd{\unitfrac}{\mathrm}{\mathsf}{}{}. In my use case I transfered an article to beamer and therefore I needed sans serif math fonts. – Dr. Manuel Kuehner Aug 27 '14 at 07:18