Creating a new command for physical versors

amsmathmacros

I am trying to create a command in order to achieve this:

I would like to write:

\ver{x} , \ver{y} , \ver{z} , \ver{r} , etc…

And I would like to get:

\hat{\mathbf{x}} , \hat{\mathbf{y}} , \hat{\mathbf{z}} , \hat{\mathbf{r}} … respectively.

I need a command capable of doing this for every letter I put between {}.

I have tried \newcommand{\ver}{\hat\mathbf} but I am getting error: extra or forgotten }.

I have also tried \newcommand{\ver}{\hat{\mathbf{•}}} or \newcommand{\ver{•}{\hat{\mathbf{•}}} but I get no result.

If anyone knows how can I solve this problem I will appreciate it.

Best Answer

You might do

\newcommand{\ver}[1]{\hat{\mathbf{#1}}}

but the result of \ver{i} would not be nice, because the i should lose the dot.

\documentclass{article}
\usepackage{amsmath}

\DeclareSymbolFont{mathbf}{OT1}{\familydefault}{\bfdefault}{n}
\DeclareSymbolFontAlphabet{\mathbf}{mathbf}
\DeclareMathSymbol{\boldimath}{\mathord}{mathbf}{"10}
\DeclareMathSymbol{\boldjmath}{\mathord}{mathbf}{"11}

\newcommand{\ver}[1]{% we assume #1 is a single Latin letter
  \if#1i%
    \hat{\boldimath}%
  \else
    \if#1j%
      \hat{\boldjmath}%
    \else
      \hat{\mathbf{#1}}%
    \fi
  \fi
}

\begin{document}

$\ver{a}+\ver{c}+\ver{i}+\ver{j}+\ver{k}+\ver{x}$

\end{document}

enter image description here

A version that also works with Greek letters.

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

% for the dotless i and j in bold face upright
\DeclareSymbolFont{mathbf}{OT1}{\familydefault}{\bfdefault}{n}
\DeclareSymbolFontAlphabet{\mathbf}{mathbf}
\DeclareMathSymbol{\boldimath}{\mathord}{mathbf}{"10}
\DeclareMathSymbol{\boldjmath}{\mathord}{mathbf}{"11}

\ExplSyntaxOn

\NewDocumentCommand{\ver}{m}
 {% Assumption: the argument is
  % either a single Latin letter or
  % or a single command for a Greek letter
  \token_if_cs:NTF #1
   { \hat{\bm{#1}} } % single Greek letter
   {% check for i or j
    \str_case:nnF { #1 }
     {
      {i}{\hat{\boldimath}} 
      {j}{\hat{\boldjmath}}
     }
     { \hat{\mathbf{#1}} } % single Latin letter not i nor j
   }
 }

\ExplSyntaxOff

\begin{document}

$\ver{a}+\ver{c}+\ver{i}+\ver{j}+\ver{k}+\ver{x}+\ver{\alpha}+\ver{\Gamma}$

\end{document}

Remember to add \usepackage{xparse} if this seems not to work (and update your TeX system).

enter image description here