# Loop over greek letters with pgffor

I am currently combining solutions proposed here for the loop definition of commands and here for the upper case letter forcing to define mathematical notations in a loop.

The goal is to have a shortcut for all letters of the latin and greek alphabets in bold font and upper/lower case.

For the latin alphabet the current solution works fine, but for the greek one it won't work.

So I actually have two questions here:

• 1: Is it possible to automatically loop over greek letters with pgffor, just like with latin letters
• 2: Is there a reason for my current solution not to work with greek letters

As usual, the answer may be very short or very long, so thanks in advance.

\documentclass{article}

% Command forcing 1st letter of argument to be capital one
\usepackage{xparse}
\ExplSyntaxOn
\NewExpandableDocumentCommand \firstcap { m } { \tl_mixed_case:n {#1} }
\ExplSyntaxOff

% Loop over latin alphabet (working)
\usepackage{pgffor}
\foreach \x in {a,...,z}{%
\expandafter\xdef\csname \firstcap{\x}mat\endcsname{\noexpand\ensuremath{\noexpand\mathbf{\firstcap{\x}}}}
}
\foreach \x in {a,...,z}{%
\expandafter\xdef\csname \firstcap{\x}vec\endcsname{\noexpand\ensuremath{\noexpand\mathbf{\x}}}
}
% Loop over greek alphabet (non working)
%\foreach \x in {alpha,zeta}{%
%\expandafter\xdef\csname \firstcap{\x}mat\endcsname{\noexpand\ensuremath{\noexpand\mathbf{\firstcap{\x}}}}
%}
%\foreach \x in {\alpha,...,\zeta}{%
%\expandafter\xdef\csname \firstcap{\x}vec\endcsname{\noexpand\ensuremath{\noexpand\mathbf{\x}}}
%}

\begin{document}
$\Amat \Bmat \Cmat \Avec \Bvec \Cvec$
%$\Alphamat \Betamat \Alphavec \Betavec$
\end{document}

• You can't use e.g. \alpha in a csname, try out \expandafter\def\csname abc\alpha xxx\endcsname{blub}. If you want to build command only with Alpha you will have to write out the list explicitly. Btw: it is dangerous to define commands without checking if they are already defined. – Ulrike Fischer Feb 28 '19 at 17:21
• @UlrikeFischer I understand the warning, but what command are you referring to precisely ? Up to now I have met no incompatibility with these commands, I am just trying to avoid doing it explicitly for each letter. – BambOo Feb 28 '19 at 17:39
• Before doing \xdef\foo, you should always first do \newcommand\foo or add another test so that you get an error if you overwrite an existing command. – Ulrike Fischer Feb 28 '19 at 17:46
• You have to define the list of names yourself, as it's not supported by \foreach like other easy loops. – egreg Feb 28 '19 at 21:10
• @UlrikeFischer Thanks for the advice, I will add such tests. Before using the loop, I was directly using the \newcommand\foo syntax. I will try with the explicit list – BambOo Feb 28 '19 at 21:45

You have to define the list yourself, but it's a one-time job. Besides, defining \Alphamat as \bm{\Alpha} will do nothing sensible, as \Alpha is not defined.

I believe it's simpler to directly use expl3 instead of awkward \foreach with \csname, \noexpand and friends.

As usual, I omitted \ensuremath that does nothing good here: \Amat is a command for a math symbol.

I'm not sure what's the rationale for having a command \Avec that prints a lowercase “a” in boldface.

\documentclass{article}
\usepackage{amsmath,bm}

% Command forcing 1st letter of argument to be capital one
\usepackage{xparse}

\ExplSyntaxOn

\cs_new_protected:Nn \bamboo_define:nnnnN
{
\cs_new_protected:cpx { #1 #3 } { \exp_not:N #4{#5{#2}} }
}

\int_step_inline:nnn { A } { Z }
{
\bamboo_define:nnnnN
{ \char_generate:nn { #1 } { 11 } } % character
{ \char_generate:nn { #1 } { 11 } } % character
{ mat }                             % suffix
{ \mathbf }                         % decoration
\use:n                              % just the argument
}
\int_step_inline:nnn { a } { z }
{
\bamboo_define:nnnnN
{ \char_generate:nn { #1 -32 } { 11 } } % character
{ \char_generate:nn { #1 } { 11 } }     % uppercase variant
{ vec }                                 % suffix
{ \mathbf }                             % decoration
\use:n                                  % just the argument
}
\clist_map_inline:nn
{
Gamma,Delta,Theta,Lambda,Xi,Pi,Sigma,Phi,Psi,Omega
}
{
\bamboo_define:nnnnN
{ #1 }  % the Greek letter name with first uppercase
{ #1 }  % the Greek letter name with first uppercase
{ mat } % suffix
{ \bm } % decoration
\use:c  % make a control sequence
}
\clist_map_inline:nn
{
alpha,beta,gamma,delta,epsilon,zeta,eta,theta,iota,kappa,
lambda,mu,nu,xi,pi,rho,sigma,tau,phi,chi,psi,omega
}
{
\bamboo_define:nnnnN
{ \tl_mixed_case:n { #1 } } % the Greek letter name with first uppercase
{ #1 }                      % the Greek letter name
{ vec }                     % suffix
{ \bm }                     % decoration
\use:c                      % make a control sequence
}

\ExplSyntaxOff

\begin{document}
$\Amat \Bmat \Cmat \Avec \Bvec \Cvec$
$\Gammamat \Deltamat \Alphavec \Betavec$
\end{document}


All considered, I believe you lost more time in defining the loops than in defining all commands manually. ;-) But, of course, academic interest has its role.

The unicode-math package defines them for you, in math mode. 𝚨 is \mbfAlpha, 𝛍 is \mbfmu, 𝜽 is \mbfittheta, and so on.

• Thought this dates back, I thougt again about your proposal, but I can't figure how define shortcuts to \mbf... constructs using a loop. You answer is good, but I am too much used to my shortcuts now ^^ – BambOo Jun 25 '20 at 12:53
• @BambOo Without having tested this solution: I think the only thing that doesn’t work is {alpha,...,omega}? You should be able to take the existing code and write out the list explicitly: {alpha,beta,gamma,delta,epsilon}, but for every letter of the Greek alphabet, and use that in addition to {a,...,z}. – Davislor Jun 25 '20 at 18:48
• thanks for the quick reply. Do you mean that e.g. \mbfalpha is an alias of \mathbf{alpha} when unicode-math is loaded ? – BambOo Jun 25 '20 at 19:19
• @BambOo Close. \symbfup{\alpha}. – Davislor Jun 25 '20 at 19:22