[Tex/LaTex] Consistent macro for bold upright vectors in both latin and greek

boldgreekromanvector

I’m using pdfLaTeX, and I want all vectors I typeset to be in bold, upright font. Unfortunately, based on whether the symbol is latin, lowercase greek, or uppercase greek, there are different commands necessary to make it bold and upright. I’d like to be able to define a single macro — say, \vector — such that it does what I want no matter the argument and I don’t have to think about it:

\vector{a}       =>  \mathbf{a}
\vector{\alpha}  =>  \boldsymbol{\upalpha}
\vector{\Omega}  =>  \boldsymbol{\Omega}

This popular question deals with some of this issue, but the solutions there still have the lowercase greek vectors in bold italic, not bold upright. Plenty of other questions touch on this issue in various ways, but I haven't been able to find a definitive answer to this exact variant. Perhaps the answer is “you can’t” — but I haven't seen that definitively either.


Edited \Alpha, which doesn't exist, to \Omega.

Best Answer

A modification of my answer to Automatically check if a math character is greek or latin should do:

\documentclass{article}
\usepackage{xparse}
\usepackage{bm,upgreek}

\ExplSyntaxOn
\NewDocumentCommand\Vector{m}
 {
  \commexo_vector:n { #1 }
 }

\cs_new_protected:Npn \commexo_vector:n #1
 {
  \tl_map_inline:nn { #1 }
   {
    \commexo_vector_inner:n { ##1 }
   }
 }

\cs_new_protected:Npn \commexo_vector_inner:n #1
 {
  \tl_if_in:VnTF \g_commexo_latin_tl { #1 }
   {% we check whether the argument is a Latin letter
    \mathbf { #1 } % a Latin letter
   }
   {% if not a Latin letter, we check if it's an uppercase Greek letter
    \tl_if_in:VnTF \g_commexo_ucgreek_tl { #1 }
     {
      \bm { #1 } % a Greek uppercase letter
     }
     {% if not, we check if it's a lowercase Greek letter
      \tl_if_in:VnTF \g_commexo_lcgreek_tl { #1 }
       {
        \commexo_makeboldupright:n { #1 }
       }
       {% none of the above, just issue #1
        #1 % fall back
       }
     }
   }
 }

\cs_new_protected:Npn \commexo_makeboldupright:n #1
 {
  \bm { \use:c { up \cs_to_str:N #1 } }
 }

\tl_new:N \g_commexo_latin_tl
\tl_new:N \g_commexo_ucgreek_tl
\tl_new:N \g_commexo_lcgreek_tl
\tl_gset:Nn \g_commexo_latin_tl
 {
  ABCDEFGHIJKLMNOPQRSTUVWXYZ
  abcdefghijklmnopqrstuvwxyz
 }
\tl_gset:Nn \g_commexo_ucgreek_tl
 {
  \Gamma\Delta\Theta\Lambda\Pi\Sigma\Upsilon\Phi\Chi\Psi\Omega
 }
\tl_gset:Nn \g_commexo_lcgreek_tl
 {
  \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\varphi\varsigma\varrho
 }

\ExplSyntaxOff
\begin{document}
$\Vector{X}\Vector{\Lambda}\Vector{\alpha}\Vector{\beta}$

$\Vector{X\Lambda}$
\end{document}

enter image description here

What does \commexo_makeboldupright:n do? It takes its input, say \alpha, strips away the backslash from the name with \cs_to_str:N and builds the control sequence \upalpha via \use:c: saying \use:c{upalpha} is equivalent to typing \upalpha.