2

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.

7
  • 2
    Welcome! \newcommand{\ver}[1]{\hat\mathbf{#1}} works for me. Oct 9, 2021 at 18:56
  • Hello! I have tried it but I am still getting the same error, is there any package I need to make it work? Thanks!
    – FacuO.Z.
    Oct 9, 2021 at 20:00
  • I did it! I used \newcommand{\ver}[1]{\hat{\mathbf{#1}}}. Thank you LaTeXereXeTaL!
    – FacuO.Z.
    Oct 9, 2021 at 20:16
  • Glad it's working for you. Going only by what you included in your post, it seems you didn't allow for the macro to take an argument. Oct 9, 2021 at 21:22
  • 2
    @LaTeXereXeTaL You missed a pair of braces: \hat\mathbf{x} is wrong and produces errors. The correct code would be \newcommand{\ver}[1]{\hat{\mathbf{#1}}}
    – egreg
    Oct 9, 2021 at 21:42

1 Answer 1

3

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

3
  • Thank you! That's perfect.
    – FacuO.Z.
    Oct 9, 2021 at 22:09
  • 1
    @FacuO.Z. I’ll add also a version for Greek letters. Stay tuned.
    – egreg
    Oct 10, 2021 at 8:12
  • You literally saved my life, Thank you!
    – FacuO.Z.
    Oct 13, 2021 at 2:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .