使用 pgffor 循環希臘字母

使用 pgffor 循環希臘字母

我目前正在結合提出的解決方案這裡for 指令的循環定義和這裡對於大寫字母,強制在循環中定義數學符號。

目標是為拉丁字母和希臘字母的所有字母提供粗體和大寫/小寫的快捷方式。

對於拉丁字母,目前的解決方案工作正常,但對於希臘字母,它不起作用。

所以我其實有兩個問題:

  • 1:是否可以使用 pgffor 自動循環希臘字母,就像拉丁字母一樣
  • 2:我目前的解決方案是否有理由不使用希臘字母

像往常一樣,答案可能很短或很長,所以提前致謝。

\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}

答案1

您必須自己定義該列表,但這是一項一次性工作。此外,定義\Alphamatas\bm{\Alpha}不會做任何明智的事情,因為 as\Alpha沒有定義。

我相信直接使用會更簡單,expl3而不是\foreach\csname,\noexpand和朋友一起尷尬。

像往常一樣,我\ensuremath在這裡省略了沒有任何作用的:\Amat是一個數學符號的命令。

我不確定使用一個\Avec以粗體打印小寫“a”的命令的理由是什麼。

\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}

在此輸入影像描述

綜合考慮,我相信您在定義循環上損失的時間比手動定義所有命令花費的時間更多。;-)但是,當然,學術興趣有其作用。

答案2

相關內容