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...)
6 Answers
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).
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
-
1
-
2@ToddLehman I would do them as
\Valpha
,\Vbeta
etc., by looping through a comma delimited list. Will add it to the MWE. Feb 25, 2012 at 6:13 -
5
-
1
-
1@AndreyVihrov Probably better to optimize. This way saved quite a few
\expandafter
s. Feb 25, 2012 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}
-
2Most 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. Mar 1, 2012 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}
-
What is the difference between
\newcommand
and\newcommand*
? Also, are there any differences between this solution and @alexis's solution? Feb 25, 2012 at 16:02 -
1For the first question see What's the difference between \newcommand and \newcommand*?. For the second question, this solution requires that you be in math mode and will result in an error if you are not, where as @alexis's solution will not result in an error if used outside of math mode. For a discussion on this see When not to use \ensuremath for math macro? Feb 25, 2012 at 17:58
-
You can also invoke this as
\V x
, which I find a bit nicer at times:\V c = \V a \times \V b
.– wcharginJun 18, 2014 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).
-
3
-
1Avoiding the space is precisely the reason to use a non-letter name. Feb 26, 2012 at 22:42
When automating tex (speeding-up workflow is a particular case) — there's always two approaches:
- automate the problem in tex
- automate the problem in text-editor
When talking about minimising key strokes — you can't get shorter then a single key stroke, and that is possible with automating text-editor.
For example, here's an Emacs function which does what you want:
(defun tex-make-vector ()
"If there's a selection -- wrap this with '\mathbf{' and '}'
and put the point to the end. Otherwise -- put the point
between '\mathbf{' and '}'
Also: when not in math mode -- enclose the thing in dollars."
(interactive)
(let (start end
(delim "")
(jump 1)
)
(when (not (texmathp))
(setq delim "$")
(setq jump 2)
)
(if (use-region-p)
(progn
(setq start (region-beginning))
(setq end (region-end))
(narrow-to-region start end)
(goto-char (point-min))
(insert (concat delim "\\mathbf{"))
(goto-char (point-max))
(insert (concat "}" delim))
(widen)
)
(progn
(insert (concat delim "\\mathbf{}" delim))
(backward-char jump)
)
)
))
You can bind it to a key:
(define-key LaTeX-mode-map (kbd "C-v") 'tex-make-vector)
and you'll be able to insert a vector with a single key stroke.
The bad thing with "automating with text-editor" is always the same: it is not as modular than defining a \newcommand
and using it. But using the custom command can also be fastened with text-editor.
\bm
from thebm
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)\vec{}
, and redefining\vec{}
appropriately. If you want vectors to be bold, you can use\renewcommand{\vec}[1]{\mathbf{#1}}%
. If you want vectors indicated by arrows, just comment out that out.\mathbf{}
doesn't work inalign
statements across the alignment symbol.