如何自訂一個新命令?

如何自訂一個新命令?

我想自訂一個命令\myvec{arg}

如果參數是單一字符,則\bm{arg}

否則然後\vv{arg}

答案1

改編代碼來自:

以下似乎有效:

\documentclass{article}
% This code by Hendrik Vogt https://tex.stackexchange.com/a/70562/2693
\newcommand*\ifsingle[3]{%
  \setbox0\hbox{$\mathaccent"0362{#1}^H$}%
  \setbox2\hbox{$\mathaccent"0362{\kern0pt#1}^H$}%
  \ifdim\ht0=\ht2 #3\else #2\fi
  }
\usepackage{bm}
\def\vv{} % define as needed
\newcommand*\myvec[1]{\ifsingle{#1}{\bm{#1}}{\vv{#1}}}
\begin{document}
$\myvec{a}$
$\myvec{aa}$
$\myvec{\alpha}$
$\myvec{\alpha\beta}$
\end{document}

程式碼的輸出

相關內容