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}

Jan Hajer
  • 864
  • 1
    While it won't have your desired syntax, and is limited to pdflatex, the approach here, https://tex.stackexchange.com/questions/145926/upright-greek-font-fitting-to-computer-modern/230220#230220, permits the unslanting of slanted Greek glyphs. – Steven B. Segletes Aug 10 '21 at 10:24
  • 1
    An idea: https://tex.stackexchange.com/a/277576/82917 – campa Aug 10 '21 at 10:31
  • Also relevant: https://tex.stackexchange.com/questions/277571/create-and-use-macro-names-dynamically-within-a-loop-for-redefining-lowercase-gr – Steven B. Segletes Aug 24 '21 at 13:05

4 Answers4

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}

David Carlisle
  • 757,742
  • 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 '21 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}
Mico
  • 506,678
  • 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 '21 at 12:35
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

campa
  • 31,130
  • 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 '21 at 13:21
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}