# Serif \text in formulas in Beamer

In my Beamer presentation, I specify the Palatino font as a math font. However, in the formulas I can see that a sans-serif font is used inside \text macros.

I use pdflatex for compilation.

How to force \text macros produce serif text?

Minimal Working Example (MWE):

\documentclass{beamer}
\usepackage{amsmath}
\usetheme{metropolis}
\usepackage[T1]{fontenc}
\usepackage{mathpazo}
\usepackage{textcomp}
\usefonttheme{professionalfonts}
\usefonttheme[onlymath]{serif}

\DeclareMathOperator*{\argmin}{arg\,min}

\begin{document}

\begin{frame}{Optimization problem}
We arrive at the optimization problem
\begin{align}
\sum_{i=1}^N \big[u_i - u_{\text{NN}}(x_i, t_i; \theta)\big]^2  \\
&\text{subject to } \ \ u_{\text{NN}, t}  + N(u_\text{NN}) = 0
\end{align}
\end{frame}

\end{document}

• This is the wrong usage of \text anyways. Remember that\text switches to the text font. Use mathrm instead. – daleif Jul 26 '19 at 9:22
• Off-topic: the package beamer doesn't exist. In your code remove line \usepackage{beamer}. – Zarko Jul 26 '19 at 10:31
• @daleif, thanks, I have managed to solve the problem by using \textrm instead of \text. – Dmitry Kabanov Jul 26 '19 at 11:26
• @Zarko, thank you, it was wrong line, deleted it. – Dmitry Kabanov Jul 26 '19 at 11:26
• @DmitryKabanov, not \textrm but \mathrm! – Zarko Jul 26 '19 at 11:29

As explain @daleif in his comment, use of \text is switch between fonts used in math environments and in a text. Correct way in your case (and all similar cases) is to use \mathrm command (I also suggest to use alignat environment instead of align, code is slightly simpler):

\documentclass{beamer}
\usetheme{metropolis}
\usepackage[T1]{fontenc}
\usepackage{mathpazo}
\usepackage{textcomp}
\usefonttheme{professionalfonts}
\usefonttheme[onlymath]{serif}

\usepackage{amsmath}
\DeclareMathOperator*{\argmin}{arg\,min}

\begin{document}
\begin{frame}{Optimization problem}
We arrive at the optimization problem
\begin{alignat}{3}
&\argmin_{\theta}
&\quad  &   \sum_{i=1}^N \big[u_i - u_{\mathrm{NN}}(x_i, t_i; \theta)\big]^2  \\
&\text{subject to}
&       &   u_{\mathrm{NN}, t}  + N(u_\mathrm{NN}) = 0
\end{alignat}
\end{frame}
\end{document}


Note: redefining basic command from amsmath packages is dangerous. Never do this!

• One comment: to enforce serif font on "subject to", one can use \textrm macro in formulas. \mathrm swallows spaces in the text. – Dmitry Kabanov Jul 28 '19 at 21:34
• @DmitryKabanov, "subject to" is ordinary text (in equation), so the use of the \text{...} is correct in this case. Similarly you will have ordinary text at using \intertext{...}or \shortintertext{...} for text between multi line equation. – Zarko Jul 28 '19 at 21:50

UPDATE on 28 July 2019: redefining \text macro can be dangerous as it is one of the basic macros. This was is not recommended, therefore.

To ensure serif text font in math formulas, the command \textrm should be used, as the command \text matches normal text, which in Beamer by default is sans serif.

Alternatively, one can renew command \text such that it becomes an alias for \textrm:

\renewcommand{\text}[1]{\textrm{#1}}

• Don't do this!It is very dangerous to redefine basic latex commands! Please consider @daleif comment and stay on safe side of using latex. Replacing \text with \mathrm is not big deal fore an editor. – Zarko Jul 26 '19 at 11:36