37

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
  • 2
    Can't you just define your own command for the vectors? How are you writing it now? Feb 25, 2012 at 3:53
  • 3
    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, 2012 at 14:35
  • 4
    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. Feb 25, 2012 at 17:08
  • 1
    For vectors, I would suggest using \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.
    – erik
    Feb 14, 2015 at 18:43
  • 1
    Also, \mathbf{} doesn't work in align statements across the alignment symbol.
    – Jeff
    May 25, 2015 at 22:47

6 Answers 6

45

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).

7
  • 3
    Ooh. That is a really cool trick. Feb 25, 2012 at 7:52
  • 3
    This is the best solution so far. Feb 25, 2012 at 8:46
  • 1
    Very elegant and very easy to use solution. That's great !
    – projetmbc
    Feb 25, 2012 at 14:00
  • 3
    One should remember, though, that this will remove the original definition of \*.
    – egreg
    Feb 14, 2015 at 17:05
  • 1
    @RyanReich You'd have discovered it if you had used \newcommand (which is one of the main reasons for the command to exist).
    – egreg
    Feb 14, 2015 at 19:09
15

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

enter image description here

5
  • 1
    Nice! And how about the Greek letters? Feb 25, 2012 at 6:03
  • 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
    \command@factory... that's epic!
    – Werner
    Feb 25, 2012 at 7:25
  • 1
    Shouldn't the \defs be outside of the loops? Feb 25, 2012 at 10:20
  • 1
    @AndreyVihrov Probably better to optimize. This way saved quite a few \expandafters. Feb 25, 2012 at 10:27
14

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}

example

1
  • 2
    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. Mar 1, 2012 at 20:15
12

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}
3
  • 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
  • 1
    For 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.
    – wchargin
    Jun 18, 2014 at 3:07
5

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).

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

When automating tex (speeding-up workflow is a particular case) — there's always two approaches:

  1. automate the problem in tex
  2. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.