數學模式下帶有條件的直立粗體希臘語

數學模式下帶有條件的直立粗體希臘語

再會!

我用谷歌搜尋了整個 stackechange,但找不到答案。我對 Latex 很陌生,所以我的方法不正確。我嘗試定義一個宏,該宏應將希臘字母格式化為直立粗體。我知道有幾種方法可以做到這一點,我想避免使用 isomath 並堅持使用上希臘文包裹。

我希望能夠寫

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

在這兩種情況下都會得到直立的粗體字元。所以,這是我的程式碼:

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

我試圖獲取控制序列的第一個字符,如果它是大寫字符,我會直接調用\boldsymbol而不進行任何修改,否則我想通過放置\up在開頭來修改控制序列。所以,當我這樣做時,它會產生以下錯誤

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

看起來“if”條件總是給出假,即“P”不被識別為大寫,因此下面的MWE將寫兩次“小寫”:

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

誰能給我一個想法,為什麼它不能按預期工作?

答案1

bm就是做你想做的事而不做任何進一步的重新定義嗎?

在此輸入影像描述

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


\begin{document}

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

\end{document}

答案2

非常感謝大衛卡萊爾!

關於catcodes的想法是完全正確的。由於缺乏經驗,我對catcodes一無所知。當我嘗試的時候我也很困惑電子工具箱函數\ifstrequal來比較正常P(catcode 11)和去標記化P(catcode 12)並且它有效!在我決定查看工具箱的源代碼後,我發現它\detokenize適用於兩個操作數。所以我以類似的方式修改了我的宏,因此它現在按預期工作。

宏:

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

結果: 在此輸入影像描述

也許,這不是最好的解決方案,我相信對於所提出的問題效率不高,但這是有用的經驗!

相關內容