6

I need to distinguish between italic and upright greek letters. I use the alphabeta package for that. This allows me to write e.g. an upright \mu in mathmode by typing $\text\mu$. Can I extend this in such a way that also \mathrm produces the upright \mu?

\documentclass{article}

\usepackage{amsmath}
\usepackage{alphabeta}

\begin{document}

\begin{align}
\text{is italic mu: } & \mu \mathit \mu\\
\text{should be upright mu: } & \text \mu \mathrm \mu
\end{align}

\end{document}
3

4 Answers 4

3

A solution for pdftex. Wildly untested, so there might be issues I haven't thought of. Note that alphabeta cannot be loaded, but its basic functionality is provided.

\documentclass{article}

\usepackage{amsmath}% for \text
\usepackage{bm}
\usepackage{textalpha}

\makeatletter

\def\@tempa#1#2#3{%
   \let#2=#1
   \def#1{\TextOrMath{#3}{\ifnum\fam=0 \text{#3}\else#2\fi}}%
}

\@tempa\alpha\mathalpha\textalpha
\@tempa\beta\mathbeta\textbeta
\@tempa\gamma\mathgamma\textgamma
\@tempa\delta\mathdelta\textdelta
\@tempa\epsilon\mathepsilon\textepsilon
\@tempa\zeta\mathzeta\textzeta
\@tempa\eta\matheta\texteta
\@tempa\theta\maththeta\texttheta
\@tempa\vartheta\mathvartheta\texttheta
\@tempa\iota\mathiota\textiota
\@tempa\kappa\mathkappa\textkappa
\@tempa\lambda\mathlambda\textlambda
\@tempa\mu\mathmu\textmu
\@tempa\mnu\mathmnu\textmnu
\@tempa\xi\mathxi\textxi
\@tempa\rho\mathrho\textrho
\@tempa\varrho\mathvarrho\textrho
\@tempa\sigma\mathsigma\textsigma
\@tempa\tau\mathtau\texttau
\@tempa\upsilon\mathupsilon\textupsilon
\@tempa\phi\mathphi\textphi
\@tempa\chi\mathchi\textchi
\@tempa\psi\mathpsi\textpsi
\@tempa\omega\mathomega\textomega

\makeatother

\begin{document}

See \alpha\beta\gamma, but \theta\vartheta\ or \rho\varrho.\par
Normal: $a\alpha l\lambda r\rho\varrho x_{\mu}$\par
With \verb+\mathrm+: $\mathrm{a\alpha l\lambda r\rho\varrho x_{\mu}}$\par
With \verb+\bm+: $\bm{\alpha}$, but $\bm{\mathrm{\alpha}} $

\end{document}

enter image description here

1
  • This solution works really well. I did not know about the \fam=0 trick. So far I only managed to break it by trying to use the bm package.
    – Jan Hajer
    Aug 10, 2021 at 13:21
6

This is easiest with unicode-math and lualatex or xelatex as a Unicode math font already contains both styles of mu:

enter image description here

\documentclass{article}

\usepackage{amsmath}
\usepackage{unicode-math}

\begin{document}

\begin{align}
\text{is italic mu: } & \mu \\
\text{should be upright mu: } & \symup{\mu}
\end{align}

\end{document}
1
  • I would love to use modern latex, but apparently it is still too new for my collaborators, so I am stuck with good old pdflatex.
    – Jan Hajer
    Aug 10, 2021 at 12:37
3

Since you appear to use Greek letters mainly (exclusively?) in math mode, you may want to acquaint yourself with the upgreek package, which offers macros called \upalpha, \upbeta, etc.

enter image description here

\documentclass{article}
\usepackage{upgreek}
\begin{document}
\begin{tabular}{ll}
slanted greek math-mode letters: & $\alpha\beta\gamma\dots\mu\nu\dots\omega$ \\
upright greek math-mode letters: & $\upalpha\upbeta\upgamma\dots\upmu\upnu\dots\upomega$
\end{tabular}
\end{document}
1
  • Thanks, i know this package but prefer the alphabeta approach. I want to use greek letters in math context only, and switch between italic and upright greek letters similarly to roman letters by using \text and \mathrm and not by typing different command like \upmu.
    – Jan Hajer
    Aug 10, 2021 at 12:35
3

In a comment, I had mentioned the possibility of using \unslant on the Greeks, as done here: Upright Greek font fitting to Computer Modern. However, the OP wants a macro that does it automatically.

Here, I create a macro \mathgr{} that employs a token cycle to check each token in the argument...if they are \up<greek> able, it uses the \unslant upon them (i.e., it only uses upgreek to determine the token's greek-ness).

Because tokcycle collects the tokens before typesetting them, things like macros and grouped arguments pose no difficulty. For example, in the MWE, in the \mathgr argument, I use non-greek macros, I use groupings around an exponent, I use non greek characters, I use an active ~ token, as well as a nested \textrm.

The \ThisStyle{...\SavedStyle...} syntax of the scalerel package is used to preserve smaller math styles for the unslanted characters.

This approach only works in pdflatex.

\documentclass{article}
\usepackage{upgreek}% ONLY USED TO DEFINE NAMES \up...
\usepackage{tokcycle,scalerel}
\newsavebox{\foobox}
\newcommand{\slantbox}[2][0]{\mbox{%
        \sbox{\foobox}{#2}%
        \hskip\wd\foobox
        \pdfsave
        \pdfsetmatrix{1 0 #1 1}%
        \llap{\usebox{\foobox}}%
        \pdfrestore
}}
\newcommand\unslant[2][-.25]{\ThisStyle{\slantbox[#1]{$\SavedStyle#2$}}}

\makeatletter
\newcommand\mathgr[1]{\tokcycle
  {\addcytoks{##1}}
  {\processtoks{##1}}
  {\ifcsname up\expandafter\@gobble\string##1\endcsname
   \addcytoks{\unslant{##1}}%
    \else\addcytoks{##1}\fi}
  {\addcytoks{##1}}{#1}%
  \expandafter\mathrm\expandafter{\the\cytoks}%
}
\makeatother
\begin{document}
\begin{tabular}{ll}
slanted greek math-mode letters: & 
  $ab c \alpha\beta\gamma^{\pi+1}\dots\mu~\nu\dots\omega\textrm{a b c}$ \\
tokcycle + unslant & 
  $\mathgr{ab c \alpha\beta\gamma^{\pi+1}\dots\mu~\nu\dots\omega\textrm{a b c}}$
\end{tabular}
\end{document}

enter image description here

If one was happy with the look of upgreek and wanted to avoid the vagaries of \unslant, then the token cycle can be adjusted to do so:

\documentclass{article}
\usepackage{upgreek}%
\usepackage{tokcycle}

\makeatletter
\newcommand\mathgr[1]{\tokcycle
  {\addcytoks{##1}}
  {\processtoks{##1}}
  {\ifcsname up\expandafter\@gobble\string##1\endcsname
   \addcytoks[1]{\csname up\expandafter\@gobble\string##1\endcsname}%
    \else\addcytoks{##1}\fi}
  {\addcytoks{##1}}{#1}%
  \expandafter\mathrm\expandafter{\the\cytoks}%
}
\makeatother
\begin{document}
\begin{tabular}{ll}
slanted greek math-mode letters: & 
  $ab c \alpha\beta\gamma^{\pi+1}\dots\mu~\nu\dots\omega\textrm{a b c}$ \\
tokcycle + unslant & 
  $\mathgr{ab c \alpha\beta\gamma^{\pi+1}\dots\mu~\nu\dots\omega\textrm{a b c}}$
\end{tabular}
\end{document}

enter image description here


SUPPLEMENT

The OP seems determined to "override" \mathrm. While I don't recommend replacing it, nonetheless, it can be done to automatically include the token cycle with

\documentclass{article}
\usepackage{upgreek}% ONLY USED TO DEFINE NAMES \up...
\usepackage{tokcycle,scalerel}
\newsavebox{\foobox}
\newcommand{\slantbox}[2][0]{\mbox{%
        \sbox{\foobox}{#2}%
        \hskip\wd\foobox
        \pdfsave
        \pdfsetmatrix{1 0 #1 1}%
        \llap{\usebox{\foobox}}%
        \pdfrestore
}}
\newcommand\unslant[2][-.25]{\ThisStyle{\slantbox[#1]{$\SavedStyle#2$}}}

\makeatletter
\newcommand\mathgr[1]{\tokcycle
  {\addcytoks{##1}}
  {\processtoks{##1}}
  {\ifcsname up\expandafter\@gobble\string##1\endcsname
   \addcytoks{\unslant{##1}}%
    \else\addcytoks{##1}\fi}
  {\addcytoks{##1}}{#1}%
  \expandafter\svmathrm\expandafter{\the\cytoks}%
}

\let\svmathrm\mathrm
\renewcommand\mathrm[1]{\mathgr{#1}}
\makeatother
\begin{document}
\begin{tabular}{ll}
slanted greek math-mode letters: & 
  $ab c \alpha\beta\gamma^{\pi+1}\dots\mu~\nu\dots\omega\textrm{a b c}$ \\
mathrm (tokcycle + unslant) & 
  $\mathrm{ab c \alpha\beta\gamma^{\pi+1}\dots\mu~\nu\dots\omega\textrm{a b c}}$
\end{tabular}
\end{document}
2
  • I like the idea of unslating the usual italic Greek letters. But my main concern is to override \mathrm.
    – Jan Hajer
    Aug 11, 2021 at 7:50
  • @JanHajer I have added a SUPPLEMENT to my answer, showing how to override \mathrm, even though I still don't recommend it. Aug 11, 2021 at 14:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .