# Faster way of writing \mathbf

Is there a quicker way of doing boldface in math mode instead of typing out \mathbf{} each time? (It gets cumbersome when you denote simple vectors with boldface...)

-
Can't you just define your own command for the vectors? How are you writing it now? –  Peter Grill Feb 25 '12 at 3:53
Every time I have a vector, I write $\mathbf{x}$. –  jamaicanworm Feb 25 '12 at 3:57
I'd rather use \bm from the bm package, does not get much fater than that, and then $\bm{v}=(v_1,v_2,\dots,v_n)$ does not look odd any more (seems to to me to use upright bold instead of italic bold) –  daleif Feb 25 '12 at 14:35
your input would be more scrutable to someone else if you define a shorthand that makes obvious the vector nature of the symbols, rather than just the fact that they're boldface. –  barbara beeton Feb 25 '12 at 17:08

There's a really cheesy way of saving keystrokes that is no longer than Todd's answer but just as comprehensive as Yiannis's:

\documentclass{article}

\def\*#1{\mathbf{#1}}

\def\ab{ab}
\begin{document}
$\*v, \*w, \*\ab, \*\Gamma$.
\end{document}


Explanation: a control sequence whose name is a non-letter doesn't require either spaces or braces after it (unless of course you want it to act on multiple tokens, like plain ab; a macro such as my \ab will work just fine, though).

-
Ooh. That is a really cool trick. –  Todd Lehman Feb 25 '12 at 7:52
This is the best solution so far. –  Yiannis Lazarides Feb 25 '12 at 8:46
Very elegant and very easy to use solution. That's great ! –  projetmbc Feb 25 '12 at 14:00

Just to expand on Todd Lehman's answer, you can save a bit of typing in the definitions, by automating the creation of the commands:

The LaTeX kernel has a looping construct named \@tfor that enables parsing a list of characters. We leverage this to define commands of the form \Va..\Vz and \VA..\VZ automatically using \csname..\endcsname.

\@tfor\next:=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\do{%
\def\command@factory#1{%
\expandafter\def\csname V#1\endcsname{#1}
}
\expandafter\command@factory\next
}


The full MWE is shown below:

\documentclass{article}
\makeatletter
\@tfor\next:=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\do{%
\def\command@factory#1{%
\expandafter\def\csname V#1\endcsname{#1}
}
\expandafter\command@factory\next
}
\makeatother
\begin{document}
$a \cdot (\Vv \otimes \Vw) = (a \cdot \Vv) \otimes \Vw = \Vv \otimes (a \cdot \Vw)$
\end{document}


To have bold greek, we can use a similar technique. This time we will put all the greek letters in a comma delimited list, like:

 alpha,beta,gamma,zeta...Alpha...Zeta


we can then iterate over it, this time using a @for loop, here is the amended minimal.

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathpazo}

\makeatletter
\@tfor\next:=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\do{%
\def\command@factory#1{%
\expandafter\def\csname V#1\endcsname{\mathbb{#1}}
}
\expandafter\command@factory\next
}
\begin{document}
$a \cdot (\Vv \otimes \Vw) = (a \cdot \Vv) \otimes \Vw = \Vv \otimes (a \cdot \Vw)$

\def\greekvectors#1{%
\@for\next:=#1\do{%
\def\X##1;{%
\expandafter\def\csname V##1\endcsname{\boldsymbol{\csname##1\endcsname}}
}
\expandafter\X\next;
}
}

\greekvectors{alpha,beta,iota,gamma,lambda,nu,eta,Gamma,varsigma}

$\VGamma\Viota\Valpha\Vnu\Vnu\Veta\Vvarsigma$
\end{document}


Will output

-
Nice! And how about the Greek letters? –  Todd Lehman Feb 25 '12 at 6:03
@ToddLehman I would do them as \Valpha, \Vbeta etc., by looping through a comma delimited list. Will add it to the MWE. –  Yiannis Lazarides Feb 25 '12 at 6:13
\command@factory... that's epic! –  Werner Feb 25 '12 at 7:25
Shouldn't the \defs be outside of the loops? –  Andrey Vihrov Feb 25 '12 at 10:20
@AndreyVihrov Probably better to optimize. This way saved quite a few \expandafters. –  Yiannis Lazarides Feb 25 '12 at 10:27

What you want is not necessarily a faster way to write \mathbf{v}, but a faster way to obtain the vector v, yes?

In this case, I would define a set of very short letter-specific macros, one for each vector variable. For example:

\documentclass{article}
\newcommand{\Vu}{\mathbf{u}}
\newcommand{\Vv}{\mathbf{v}}
\newcommand{\Vw}{\mathbf{w}}
\newcommand{\Vx}{\mathbf{x}}
\newcommand{\Vy}{\mathbf{y}}
\newcommand{\Vz}{\mathbf{z}}
\begin{document}
$\|\Vx\| = \sqrt{\Vx_1^2 + \cdots + \Vx_n^2}$
$a(\Vu+\Vv) = a\Vu + a\Vv$
$a_1\Vv_{i_1} + a_2\Vv_{i_2} + \cdots + a_n\Vv_{i_n} = 0$
$a \cdot (\Vv \otimes \Vw) = (a \cdot \Vv) \otimes \Vw = \Vv \otimes (a \cdot \Vw)$
\end{document}


-
Most of the times this is perhaps the easiest and most convenient solution. Also reminds you that you are dealing with some mathematical object (i.e. vector "b"); rather than some formatting issue. –  Juan A. Navarro Mar 1 '12 at 20:15

You can define a \newcommand*{\V}[1]{\mathbf{#1}}, then you can just write \V{x}:

\documentclass{article}
\usepackage{amsmath}
\newcommand*{\V}[1]{\mathbf{#1}}%
\begin{document}
$\mathbf{x} \V{x}$
\end{document}

-
You can also invoke this as \V x, which I find a bit nicer at times: \V c = \V a \times \V b. –  WChargin Jun 18 at 3:07

Define a command that takes an argument. No need to use a non-letter name: If your vector names are a single letter, you don't need to type the braces:

\newcommand\V[1]{\ensuremath{\mathbf{#1}}}

$\V w = a\V v_0 + \V u$


You do need a space after \V (unless you use a non-letter instead of V).

-
Or even better, \def\V:#1{\mathbf{#1}} $\V:ab\times\V:{ab}$ –  rdhs Feb 25 '12 at 13:47
Avoiding the space is precisely the reason to use a non-letter name. –  Ryan Reich Feb 26 '12 at 22:42