# Create and use macro names dynamically within a loop for redefining lowercase greek letters

This is follow-up question to this one. My aim is to let greek letters behave the same like normal latin letters, when it comes to get them upright. At the moment I'm using \mathrm to get letters upright in math mode.

So \mathrm{\delta d} should return an upright d and an upright delta. A custom command \upright would be an option too, but I wouldn't know where to start. The important part is, that I could use that command for both, greek and latin letters, equally - without additional commands like \deltaup or \updelta. The reason is that I need to work with different classes, different fonts and even different compilers, so I need to get a most generic solution. Using fontspec/unicode-math (which would solve the problem, is not an option).

From my previous question I learned by Ulrike Fischer's answer, that there would be no way around using \deltaup for my case. And she offered the neat solution:

\let\deltait\delta\renewcommand\delta{\ifnum\fam=0 \deltaup\else \deltait\fi}


In case some of my colleagues uses the upgreek package and \updelta I wrapped an if condition around:

\makeatletter
\ifcsname deltaup\endcsname%
\let\deltait\delta\renewcommand\delta{\ifnum\fam=0 \deltaup\else \deltait\fi}
\else
\ifcsname updelta\endcsname
\let\deltait\delta\renewcommand\delta{\ifnum\fam=0 \updelta\else \delta\fi}
\fi
\fi%


Now I wonder whether I could put this into a loop to deal with all lowercase greek letters. I thought about a list with all letters, but I don't know how to get dynamic input arguments in cases like \let\deltait\delta.

Of course I could just write the same lines for every letter, but maybe another font than pxfonts later requires another solution and always redefining the whole alphabet seems cumbersome.

Furthermore jfbu mentioned in his answer/comments that using \mathrm wouldn't be a good option at all, as I'm changing the font family, which I shouldn't.

## MWE

\documentclass{article}

\usepackage[utf8x]{luainputenc}
\usepackage[T1]{fontenc}
\usepackage{pxfonts}
\usepackage{siunitx}
%\usepackage{isomath}
%\usepackage{upgreek}
\usepackage[ISO]{pxgreeks}

\makeatletter
\ifcsname deltaup\endcsname%
\let\deltait\delta\renewcommand\delta{\ifnum\fam=0 \deltaup\else \deltait\fi}
\else
\ifcsname updelta\endcsname
\let\deltait\delta\renewcommand\delta{\ifnum\fam=0 \updelta\else \deltait\fi}
\fi
\fi%

\begin{document}
$$\Phi_{\mathrm{\delta}} = \SI{42}{\micro\Omega} \cdot \delta_{\mathrm{\Phi}}$$
$$P_{\mathrm{d}} = \SI{42}{\nano\ampere} \cdot d_{\mathrm{P}}$$
\end{document}

• It doesn't matter for the greek that \mathrm switch to another font, the \delta will switch back. It is not good practice to use symbols in a \mathXX command but it works. \mathbf{a\int \delta b} will make the a and b bold, but "ignore" the \int and the \delta. And please: Don't use \Omega for \ohm, that's not semantic. – Ulrike Fischer Nov 10 '15 at 9:46
• when I said \mathrm was not ideal, it was simply to recall that its meaning from LaTeX NFSS is to switch to some font, not to set attributes. This of course is explained by the prevailing fonts at that time with their at most 256 glyph slots. – user4686 Nov 10 '15 at 10:07

If I understand what do you need, you want to spare macro space and type only:

\setsmartgreek \alpha\beta\gamma\delta\epsilon \relax


instead to re-defining \alpha, then \beta, then \gamma etc. You can try this:

\def\setsmartgreek{%
\expandafter\ifx\csname deltaup\endcsname\relax
\else \def\stgreekC##1##2;{##2up}\fi
\expandafter\ifx\csname updelta\endcsname\relax
\else \def\stgreekC##1##2;{up##2}\fi
\ifx\stgreekC\undefined \errmessage{upright greek letters are not available}%
\else \expandafter\setsmartgreekA \fi
}
\def\setsmartgreekA#1{\ifx#1\relax \else
\expandafter \let \csname it:\string#1\endcsname = #1%
\edef#1{\noexpand\ifnum\fam=0
\expandafter\noexpand\csname\expandafter\stgreekC\string#1;\endcsname
\noexpand\else \expandafter\noexpand\csname it:\string#1\endcsname
\noexpand\fi}%
\expandafter\setsmartgreekA \fi
}

\setsmartgreek\alpha\beta\gamma\delta\epsilon\relax

\message{\meaning \gamma}

• That's the only answer working out of the box also in my beamer class. Thank you! – thewaywewalk Nov 10 '15 at 11:22
• @thewaywewalk there is absolutely no reason this code should work any better then the others under beamer class as it does about the same thing. And indeed tested under the same conditions it does not work any better. What you are lacking is \usefonttheme{professionalfonts} after \documentclass{beamer}. – user4686 Nov 10 '15 at 11:32
• Yes I know about professionalfonts, the other answers work for a minimal beamer example but not for my custom beamer class. And breaking down the code to find out why would cost me hours again... and as your answer works... – thewaywewalk Nov 10 '15 at 11:45

Here is a possibility. A bit painful, but now done. Notice that the code assumes the Greek letters have been set-up to be italic, nothing breaks if not, but defined macros \alphait, \Gammait will then not do what their names indicate.

\documentclass{article}
\usepackage[utf8x]{luainputenc}
\usepackage[T1]{fontenc}
\usepackage{pxfonts}
\usepackage{siunitx}
%\usepackage{isomath}
%\usepackage{upgreek}
\usepackage[ISO]{pxgreeks}

% for what comes next we need that Greek letters are set-up to all be in
% italic by previous packages (nothing will break if not, but "\alphait" then
% would not say what it names indicate).

% The code checks existence of \alphaup, \upalpha and \otheralpha. The last
% wins on the previous.

\makeatletter
\@tfor\@tempa:={Gamma}{Delta}{Theta}{Lambda}{Xi}{Pi}{Sigma}{Upsilon}
{Phi}{Psi}{Omega}{alpha}{beta}{gamma}{delta}{epsilon}{zeta}{eta}
{theta}{iota}{kappa}{lambda}{mu}{nu}{xi}{pi}{rho}{sigma}{tau}
{upsilon}{phi}{chi}{psi}{omega}{varepsilon}{vartheta}{varpi}
{varrho}{varsigma}{varphi}\do
{%
\let\TWWW@next\@gobble
\ifcsname\@tempa up\endcsname
\expandafter\let\csname\@tempa it\expandafter\endcsname
\csname\@tempa\endcsname
\edef\@tempb
{\noexpand\noexpand\expandafter\noexpand\csname\@tempa up\endcsname}%
\edef\@tempc
{\noexpand\noexpand\expandafter\noexpand\csname\@tempa it\endcsname}
\let\TWWW@next\@firstofone
\fi
\ifcsname up\@tempa\endcsname
\expandafter\let\csname\@tempa it\expandafter\endcsname
\csname\@tempa\endcsname
\edef\@tempb
{\noexpand\noexpand\expandafter\noexpand\csname up\@tempa\endcsname}%
\edef\@tempc
{\noexpand\noexpand\expandafter\noexpand\csname\@tempa it\endcsname}
\let\TWWW@next\@firstofone
\fi
\ifcsname other\@tempa\endcsname
\expandafter\let\csname\@tempa it\expandafter\endcsname
\csname\@tempa\endcsname
\edef\@tempb
{\noexpand\noexpand\expandafter\noexpand\csname other\@tempa\endcsname}%
\edef\@tempc
{\noexpand\noexpand\expandafter\noexpand\csname\@tempa it\endcsname}
\let\TWWW@next\@firstofone
\fi
\TWWW@next{\expandafter\edef\csname\@tempa\endcsname
{\noexpand\ifnum\fam=\z@\@tempb\noexpand\else\@tempc\noexpand\fi}}%
}
\makeatletter

% to check the redefinitions in the log
\@tfor\@tempa:=\Gamma \Delta \Theta \Lambda \Xi \Pi \Sigma \Upsilon
\Phi \Psi \Omega \alpha \beta \gamma \delta \epsilon \zeta \eta
\theta \iota \kappa \lambda \mu \nu \xi \pi \rho \sigma \tau
\upsilon \phi \chi \psi \omega \varepsilon \vartheta \varpi
\varrho \varsigma \varphi\do {\typeout{\expandafter\meaning\@tempa}}

\makeatother

\begin{document}\thispagestyle{empty}
$\Gamma \Delta \Theta \Lambda \Xi \Pi \Sigma \Upsilon \Phi \Psi \Omega \alpha \beta \gamma \delta \epsilon \zeta \eta \theta \iota \kappa \lambda \mu \nu \xi \pi \rho \sigma \tau \upsilon \phi \chi \psi \omega \varepsilon \vartheta \varpi \varrho \varsigma \varphi$

$\mathrm{\Gamma \Delta \Theta \Lambda \Xi \Pi \Sigma \Upsilon \Phi \Psi \Omega \alpha \beta \gamma \delta \epsilon \zeta \eta \theta \iota \kappa \lambda \mu \nu \xi \pi \rho \sigma \tau \upsilon \phi \chi \psi \omega \varepsilon \vartheta \varpi \varrho \varsigma \varphi}$

\end{document}


The simplest way is using expl3 (fill in the missing names):

\documentclass{article}

\usepackage{amsmath}
\usepackage{pxfonts}
\usepackage{siunitx}
%\usepackage{isomath}
%\usepackage{upgreek}
\usepackage[ISO]{pxgreeks}
\usepackage{expl3}

\ExplSyntaxOn
\cs_if_exist:NTF \alphaup
{
\cs_new:Nn \thewaywewalk_greek:n
{
\int_compare:nTF { \fam = 0 } { \use:c { #1up } } { \use:c { #1it } }
}
}
{
\cs_new:Nn \thewaywewalk_greek:n
{
\int_compare:nTF { \fam = 0 } { \use:c { up#1 } } { \use:c { #1it } }
}
}

\clist_map_inline:nn
{
alpha,beta,gamma,delta,epsilon,zeta,...
}
{
\cs_set_eq:cc { #1it } { #1 }
\cs_set:cpn { #1 } { \thewaywewalk_greek:n { #1 } }
}
\ExplSyntaxOff

\begin{document}

\begin{gather}
\alpha\beta\gamma\delta\ne\mathrm{\alpha\beta\gamma\delta} \\
\Phi_{\mathrm{\delta}} = \SI{42}{\micro\Omega} \cdot \delta_{\mathrm{\Phi}} \\
P_{\mathrm{d}} = \SI{42}{\nano\ampere} \cdot d_{\mathrm{P}}
\end{gather}
\end{document}