Fonts & Character Setsbeamer + dchem.sty font problem

Information and discussion about fonts and character sets (e.g. how to use language specific characters)
Post Reply
m4cph1sto
Posts: 6
Joined: Sat Sep 05, 2009 1:35 am

beamer + dchem.sty font problem

Post by m4cph1sto »

Using Beamer and Donald Arseneau's dchem.sty package (my favorite package for typesetting chemistry), the fonts in dchem's chemistry environments are Beamer's default CM sans font, not the font specified in the preamble (like txfonts). For example, in:

Code: Select all

\documentclass{beamer}
\usepackage{txfonts}
\usepackage{dchem}
\begin{document}
\begin{frame}
    H$_2$O $\ch H_2O$
\end{frame}
\end{document}
The first "H2O" is in the proper Times sans serif, while the second "H2O" is in what appears to be Beamer's default CM sans serif. I'm presuming this is because either Beamer doesn't know what font to put into dchem's chemistry environments and so reverts to default, or dchem.sty is grabbing the wrong font somehow. This is way beyond my LaTeX ability to resolve. I've attached dchem.sty, since it is not a common package.

This may help a bit with the troubleshooting/debugging:
\usefonttheme[onlymath]{serif} gives proper Times serif in both math and chemistry environments, but \usefonttheme[stillsansserifmath]{serif} gives Times sans in math and CM sans in chemistry.

Hopefully someone can figure this out, and suggest a fix - maybe by editing dchem.sty? Thanks!
Attachments
dchem.sty
(19.98 KiB) Downloaded 485 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

beamer + dchem.sty font problem

Post by localghost »

Your example is not compilable for me because there occurs an error.

Code: Select all

! Undefined control sequence.
<recently read> \ch 
                    
l.7 \end{frame}
Looking at the STY file I found the right command. The modified and compilable version of your code is the following.

Code: Select all

\documentclass{beamer}
\usepackage{txfonts}
\usepackage{dchem}
\begin{document}
\begin{frame}
  H$_2$O $\chem{H_2O}$
\end{frame}
\end{document}
This works fine for me. Perhaps you should take a look at the mhchem package. This package seems more sophisticated to me.


Best regards and welcome to the board
Thorsten
m4cph1sto
Posts: 6
Joined: Sat Sep 05, 2009 1:35 am

Re: beamer + dchem.sty font problem

Post by m4cph1sto »

Thorsten, your results are interesting because $\ch H_2O$ and $\chm{H_2O}$ are equivalent commands according to dchem.sty, and both give the same result for me, with no errors reported but the problem of being typeset in CM font regardless of what font package I load. This problem with the fonts only occurs with the Beamer class. Regular classes (e.g. article) result in perfect handling of fonts.

Anyway I will keep searching for an answer, since for whatever reason I have always preferred dchem over mhchem, but in the mean time using mhchem is a good workaround. Thanks for your help!
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

beamer + dchem.sty font problem

Post by localghost »

m4cph1sto wrote:[...] your results are interesting because $\ch H_2O$ and $\chm{H_2O}$ are equivalent commands according to dchem.sty, and both give the same result for me, with no errors reported but the problem of being typeset in CM font regardless of what font package I load. This problem with the fonts only occurs with the Beamer class. Regular classes (e.g. article) result in perfect handling of fonts. [...]
I couldn't find any hint of the declaration for the \ch command you are using. Thus there have to be some errors which should be found in the log file. I can only say that I get some.

For the font problem the mhchem package offers some useful options which I used in the following example.

Code: Select all

\documentclass[professionalfonts]{beamer}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage[version=3]{mhchem}
%\usepackage{cmbright}

\mhchemoptions{%
  textfontname=sffamily,
  mathfontname=mathsf
}

\begin{document}
  \begin{frame}
    \begin{align}
      \cee{Al2O3 + 3H2O &<=> 2Al^3+ + 6OH-} \\
      \cee{Al2O3 + 3H2O &<=> 2[Al(OH)4]- + 2H3O+}
    \end{align}
  \end{frame}
\end{document}
You may omit the professionalfonts option for the beamer class with the cmbright font package. This package provides a sans serif font which is in my opinion ideal for presentations.
m4cph1sto
Posts: 6
Joined: Sat Sep 05, 2009 1:35 am

beamer + dchem.sty font problem

Post by m4cph1sto »

localghost wrote: I couldn't find any hint of the declaration for the \ch command you are using. Thus there have to be some errors which should be found in the log file. I can only say that I get some.
The \ch command is defined at the very beginning of dchem.sty:

Code: Select all

\@ifundefined{frozen@everymath}{% Old LaTeX; use \everymath
 \newcommand\ch{\fam\z@\everymath{\fam\z@}}% like \rm but don't have to repeat
 \newcommand\chemistry{\ch\everydisplay{\ch}%
   \let\theequation\thereaction \let\c@equation\c@reaction
   \let\p@equation\p@reaction}% declares all math to be chemistry
 \newcommand\ma{\fam\m@ne\everymath{}}% brings back regular math in chem
 \newcommand\mathematics{\ma\everydisplay{}}% declares normal math
}{% New LaTeX, with subverted \everymath
 \def\dch@ch{\fam\z@}
 \let\dch@everymath\@empty
 \let\dch@everydisp\@empty
% If existing every-math hook is the usual, insert my hook at the best place;
% otherwise, just append my hook.  Then do the same for every-display.
 \def\@tempa{\check@math@fonts\the\everymath}%
 \edef\@tempb{\the\frozen@everymath}%
 \ifx\@tempa\@tempb
  \frozen@everymath{\check@math@fonts\dch@everymath\the\everymath}%
 \else
  \addto@hook\frozen@everymath{\dch@everymath}%
 \fi
 \def\@tempa{\check@math@fonts\the\everydisplay}%
 \edef\@tempb{\the\frozen@everydisplay}%
 \ifx\@tempa\@tempb
  \frozen@everydisplay{\check@math@fonts\dch@everydisp\the\everydisplay}%
 \else
  \addto@hook\frozen@everydisplay{\dch@everydisp}%
 \fi
 \newcommand\ch{\fam\z@\let\dch@everymath\dch@ch}% like \rm
 \newcommand\chemistry{\ch\let\dch@everydisp\ch
   \let\theequation\thereaction \let\c@equation\c@reaction
   \let\p@equation\p@reaction}% all math to be chemistry
 \newcommand\ma{\fam\m@ne\let\dch@everymath\@empty}% restore regular math
 \newcommand\mathematics{\ma\let\dch@everydisp\@empty}% declares normal math
}
\newcommand\chm[1]{\begingroup\ensuremath{\ch#1}\endgroup}
At the end you'll see the \chm environment simply calls \ch. There are several lines in the above code dealing with fonts, so my guess is at least one of those lines isn't proper with Beamer, since Beamer handles fonts differently than the standard document classes.

Anyway, your example using mhchem instead of dchem solves the font problem, as does the standard loading of mhchem without the extra options.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

beamer + dchem.sty font problem

Post by localghost »

Now I know the cause. For some incomprehensible reason I had another and perhaps older version of the package.

In opposite to you I don't get any output with Times style from the txfonts although they are embedded. I compiled on my Linux system². For the output see the attached file. Checking the file reveals that there are all fonts embedded.

Code: Select all

name                                 type              emb sub uni object ID
------------------------------------ ----------------- --- --- --- ---------
RZLATQ+NimbusSanL-Regu               Type 1            yes yes no      37  0
XILGLE+CMSS10                        Type 1            yes yes no      38  0
NKIGCX+CMSS8                         Type 1            yes yes no      39  0
The first entry comes from the txfonts, the other ones are Computer Modern in 10pt (normal size) and 8pt (subscripts). I looked at the output with all viewers I have installed. No appearance of Times.
Attachments
dchem-prblm.pdf
The output from the provided code as is.
(14.02 KiB) Downloaded 339 times
Post Reply