# Upright bold greek with condition in math mode

Good day!

I googled the whole stackechange, but was not able to find the answer. I'm quite new to Latex, so my approach is not proper. I try to define a macro, that should format greek letter as upright bold. I know there are several ways to do it, I would like to avoid usage of isomath and stick to upgreek package.

I want to be able to write

\gb{\alpha}
\gb{\Alpha}


and in both cases get upright bold characters. So, here is my code inside:

\newcommand{\gb}[1]{ % Imagine #1=\Psi
\StrGobbleLeft{\detokenize{#1}}{1}[\chrcodet] % variable "\Psi"
\StrGobbleRight{\chrcodet}{1}[\chrcode] % variable "Psi"
\StrLeft{\chrcode}{1}[\chrfirst] % first character "P"
\IfSubStr{ABCDEFGHIJKLMNOPQRSTUVWXYZ}{\chrfirst} % Is it capital?
{\boldsymbol{#1}} % Yes - no modification
{\boldsymbol{\csname up\chrcode\endcsname}} % No - glue \up+Psi (that what happens!)
}


I'm trying to get the first character of the control sequence and if it's capital, I call just \boldsymbol with no modifications, otherwise I want to modify control sequence by placing \up at the beginning. So, when I do that, it produces the following error

! Undefined control sequence.
\bm@command ->\upPsi


It looks like the "if" condition always gives false, i.e. "P" is not recognized as capital, thus the following MWE will write twice "lowercase":

\documentclass{article}
\usepackage{bm,upgreek}
\usepackage{etoolbox}
\usepackage{xstring}

\newcommand{\gb}[1]{
\StrGobbleLeft{\detokenize{#1}}{1}[\chrcodet]
\StrGobbleRight{\chrcodet}{1}[\chrcode]
\StrLeft{\chrcode}{1}[\chrfirst]
#1 - \IfSubStr{ABCDEFGHIJKLMNOPQRSTUVWXYZ}{\chrfirst}{uppercase}{lowercase}
}

\begin{document}

$$\psi,\Psi,\gb{\psi},\gb{\Psi}$$

\end{document}


Can anyone give me an idea, why it does not work as expected?

-
–  Claudio Fiandrino Apr 1 '13 at 16:08

Does not bm just do what you want without any further redefinitions?

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

\begin{document}

$$\psi, \uppsi, \Psi,\bm{\psi},\bm{\uppsi},\bm{\Psi}$$

\end{document}

-
It does! But I want to try to avoid the explicite usage of \uppsi and always write \psi. –  Vladimir Ivannikov Apr 1 '13 at 16:02
@VladimirIvannikov just put \let\psi\uppsi in your preamble. –  David Carlisle Apr 1 '13 at 16:04
This instruction will completely replace italic \psi, I want to have it italic by default, but upright for bold. –  Vladimir Ivannikov Apr 1 '13 at 16:10
@VladimirIvannikov You can do it locally \def\gbm#1{{\let\psi\uppsi \bm{#1}}} even if you add all the lowercase greek aliases it's likely to be quicker than detokenising and string comparisons. –  David Carlisle Apr 1 '13 at 16:18
@VladimirIvannikov aaaaah confused me for a while:-) the detokenize dance that you do produces a P of catcode 12 which is not a substring of the catcode 11 A-Z –  David Carlisle Apr 1 '13 at 17:16
show 2 more comments

Great thanks to David Carlisle!

It was exactly correct idea about catcodes. Due to lack of experience, I had no idea about catcodes. Making my attempts I was also confused, when I tried etoolbox function \ifstrequal to compare normal P (catcode 11) and detokenized P (catcode 12) and it worked! After I decided to take a look inside the source code of the toolbox and I found there \detokenize applied to both operands. So I modified my macro in a similar fashion, thus it works as expected now.

Macro:

\documentclass{article}

\usepackage{bm,upgreek}
\usepackage{etoolbox}
\usepackage{xstring}

\newcommand{\gb}[1]{
\StrGobbleLeft{\detokenize{#1}}{1}[\chrcode]
\StrGobbleRight{\chrcode}{1}[\chrcode]
\StrLeft{\chrcode}{1}[\chrfirst]
\edef\tempa{\detokenize{ABCDEFGHIJKLMNOPQRSTUVWXYZ}}
#1 - no\;detokenize: \IfSubStr{ABCDEFGHIJKLMNOPQRSTUVWXYZ}{\chrfirst}{upper}{lower}, \,
detokenize\;both: \IfSubStr{\tempa}{\chrfirst}{upper}{lower}, \,
etoolbox: \expandafter\ifstrequal\expandafter{\chrfirst}{P}{upper}{lower}
}

\begin{document}

$$\gb{\psi}$$

$$\gb{\Psi}$$

\end{document}


Result:

Maybe, it's not the best solution and I do believe not efficient for the posed problem, but it was useful experience!

-
+1 glad you figured it out:-) –  David Carlisle Apr 1 '13 at 23:16