自動檢查數學字元是希臘語還是拉丁語

自動檢查數學字元是希臘語還是拉丁語

我的問題類似於粗體數學:在拉丁和希臘符號的 \mathbf 和 \boldsymbol 之間自動選擇?,但略有不同。我的不可編譯的最小示例如下:

\documentclass{book}
\usepackage{amssymb}
\newcommand{\tensor}[1]{if latin alphabet \mathbb{#1} else \mathbf{#1}}
\begin{document}
$\tensor{A}$ $\tensor{\Lambda}$
\end{document}

建議的內容是否可以實現?從下面的例子中,我了解到如果使用 pdflatex,此時拉丁語和希臘語數學字母之間沒有有效的切換。我還了解到,一種解決方案是切換到 lualatex,其中拉丁字母和希臘字母的處理方式不同。

答案1

我沒有mtpro2字體,所以我將使用不同的字體選擇。

這個想法是拉丁字母由它們本身(即字元)調用,而希臘字母則由控制序列調用。所以

\documentclass{article}
\usepackage{bm}
\newcommand\tensor[1]{%
  \ifcat\noexpand#1\relax % check if the argument is a control sequence
    \bm{#1}% probably Greek
  \else
    \textsf{#1}% single character
  \fi
}

\begin{document}
$\tensor{X}\tensor{\Lambda}$
\end{document}

在此輸入影像描述

局限性。只能給一個標記作為 的參數\tensor。其中一個\tensor{AB}\tensor{A\Lambda}多個令牌變體都會失敗。


基於相同想法的多令牌巨集:

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

\ExplSyntaxOn
\NewDocumentCommand\tensor{m}
 {
  \pluton_tensor:n { #1 }
 }

\cs_new_protected:Npn \pluton_tensor:n #1
 {
  \tl_map_inline:nn { #1 }
   {
    \token_if_cs:NTF ##1 { \bm { ##1 } } { \textsf { ##1 } }
   }
 }
\ExplSyntaxOff
\begin{document}
$\tensor{X}\tensor{\Lambda}$

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

當然,如果使用任意輸入,這仍然會失敗。

也許是更強大的版本,具有未知令牌的後備版本。

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

\ExplSyntaxOn
\NewDocumentCommand\tensor{m}
 {
  \pluton_tensor:n { #1 }
 }

\cs_new_protected:Npn \pluton_tensor:n #1
 {
  \tl_map_inline:nn { #1 }
   {
    \pluton_tensor_inner:n { ##1 }
   }
 }

\cs_new_protected:Npn \pluton_tensor_inner:n #1
 {
  \tl_if_in:VnTF \g_pluton_latin_tl { #1 }
   {
    \textsf { #1 } % a Latin letter
   }
   {
    \tl_if_in:VnTF \g_pluton_greek_tl { #1 }
     {
      \bm { #1 } % a Greek letter
     }
     {
      #1 % fall back
     }
   }
 }

\tl_new:N \g_pluton_latin_tl
\tl_new:N \g_pluton_greek_tl
\tl_gset:Nn \g_pluton_latin_tl
 {
  ABCDEFGHIJKLMNOPQRSTUVWXYZ
  abcdefghijklmnopqrstuvwxyz
 }
\tl_gset:Nn \g_pluton_greek_tl
 {
  \Gamma\Delta\Theta\Lambda\Pi\Sigma\Upsilon\Phi\Chi\Psi\Omega
 }

\ExplSyntaxOff
\begin{document}
$\tensor{X}\tensor{\Lambda}$

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

答案2

如果你想要更靈活的東西,也許這在將來會有用:

\documentclass{book}
\usepackage{expl3,xparse}
\ExplSyntaxOn
\keys_define:nn { pluton / tensor } {
  latin          .tl_set:N = \pluton_tensor_latin_tl,
  greek          .tl_set:N = \pluton_tensor_greek_tl,
  latin-alphabet .tl_set:N = \pluton_tensor_latin_alphabet_tl,
  greek-alphabet .tl_set:N = \pluton_tensor_greek_alphabet_tl,
}
\cs_new:Nn \pluton_tensor:n {
  \tl_if_in:NnTF \pluton_tensor_latin_alphabet_tl { #1 } {
    \pluton_tensor_latin_tl { #1 }
  } {
    \tl_if_in:NnT \pluton_tensor_greek_alphabet_tl { #1 } {
      \pluton_tensor_greek_tl { #1 }
    }
  }
}
\NewDocumentCommand \tensor { O{} m } {
  \group_begin:
  \keys_set:nn { pluton / tensor } {
    latin-alphabet = abcdefhijklmnopqrstuvwxyz
                     ABCDEFHIJKLMNOPQRSTUVWXYZ,
    greek-alphabet = \alpha\beta\delta\epsilon
                     \phi\gamma\eta\iota\theta
                     \kappa\lambda\mu\nu\pi\chi
                     \rho\sigma\tau\omega\xi\psi\xi
                     \Alpha\Beta\Delta\Epsilon  % capitals; some of
                     \Phi\Gamma\Eta\Iota\Theta  % these *definitely*
                     \Kappa\Lambda\Mu\Nu\Pi\Chi % aren't defined, but
                     \Rho\Sigma\Tau\Omega\Xi\Psi\Xi, % emacs helps :)
    latin = \mathbf,
    greek = \mathrm,
    #1
  }
  \pluton_tensor:n { #2 }
  \group_end:
}
\ExplSyntaxOff
\begin{document}
$\tensor{A}$ $\tensor{\Lambda}$
\end{document}

相關內容