12

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}

\begin{equation}
    \psi,\Psi,\gb{\psi},\gb{\Psi}
\end{equation}

\end{document}

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

1

2 Answers 2

18

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

enter image description here

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


\begin{document}

\begin{equation}
    \psi, \uppsi, \Psi,\bm{\psi},\bm{\uppsi},\bm{\Psi}
\end{equation}

\end{document}
7
  • 1
    It does! But I want to try to avoid the explicite usage of \uppsi and always write \psi. Apr 1, 2013 at 16:02
  • @VladimirIvannikov just put \let\psi\uppsi in your preamble. Apr 1, 2013 at 16:04
  • 1
    This instruction will completely replace italic \psi, I want to have it italic by default, but upright for bold. Apr 1, 2013 at 16:10
  • 1
    @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. Apr 1, 2013 at 16:18
  • 3
    @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 Apr 1, 2013 at 17:16
4

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}

\begin{equation}
    \gb{\psi}
\end{equation}

\begin{equation}
    \gb{\Psi}
\end{equation}

\end{document}

Result: enter image description here

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

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .