如何定制一个新命令?

如何定制一个新命令?

我想定制一个命令\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}

代码输出

相关内容