4

Is it possible to change the font that chemfig uses (for example to Helvetica) without using XeLaTeX or LuaTeX, but keep the normal math font throughout the document?

I tried \usepackage[helvet]{sfmath} and redefining \printatom as \renewcommand*\printatom[1]{\ensuremath{\mathsf{#1}}} but of course all the math text in the document switches to the Helvetica font (as expected):

\documentclass{article}
\usepackage{chemfig}
\usepackage[helvet]{sfmath}

\renewcommand*\printatom[1]{\ensuremath{\mathsf{#1}}}

\begin{document}
    Text text text
    \chemfig{-CO_2H}
    More text, and the following equation should use the normal math font $E = mc^2$
\end{document}
ralk912
  • 478

2 Answers2

5

Based on the suggestion by @marmot I eventually did the following:

\documentclass{article}
\usepackage{chemfig}

\makeatletter
\def\Hv@scale{.95}
\makeatother
\DeclareMathAlphabet{\foo}{OT1}{phv}{m}{n}
\renewcommand*\printatom[1]{\ensuremath{\foo{#1}}}

\begin{document}
    Text text text\\

    \chemfig{-CO_2H}\\

    More text, and the following equation should use the normal math font $E = mc^2$. Serif in math works with the default font as well $\mathsf{CO_2H}$.
\end{document}

Which is the desired output. I am not sure if there are situations where it may not work as I honestly have no idea what \DeclareMathAlphabet does, but it works so far. Any comments, suggestions, correct answers based on this, or explanations as to how is it that this works well are appreciated.

Output: enter image description here

ralk912
  • 478
  • If chemname is used the name uses the normal math font. – grsousajunior Apr 05 '19 at 15:50
  • I'm not sure what you mean. I switched for \chemname{\chemfig{-CO_2H}}{test} in the code above and "test" shows up in normal text font, as expected. – ralk912 Apr 05 '19 at 16:06
  • I would like the molecule name show up in helvetica font instead normal font. @ralk912 – grsousajunior Apr 05 '19 at 16:24
  • Ah, well that's a different question. To do it globally I think you can create a command based on chemname that switches the font as suggested in the answer that was linked to. If you ask the question I can post the code there. – ralk912 Apr 05 '19 at 16:41
0

Based on the @ralk912 answer and How do I use a particular font for a small section of text in my document? I did the following:

\documentclass{article}
\usepackage{chemfig}

\makeatletter
\def\Hv@scale{0.90}
\makeatother
\DeclareMathAlphabet{\foo}{OT1}{phv}{m}{n}
\renewcommand*\printatom[1]{\ensuremath{\foo{#1}}}

\newcommand*{\myfont}{\fontfamily{phv}\selectfont}
\DeclareTextFontCommand{\textHv}{\myfont}

\begin{document}

    Text in the default font. \textHv{Text in the new font.} Again text in the default font.\\

    \chemname{\chemfig{H_3C-CO_2H}}{acetic acid}\quad
    \chemname{\chemfig{H_3C-CO_2H}}{\textHv{acetic acid}}

    \bigskip

    Normal math font: $E = mc^2$. Serif in math with default font: $\mathsf{E = mc^2}$.
\end{document}

The output:

Output