Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
2  
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
2  
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
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. –  barbara beeton Feb 25 '12 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 at 18:43

6 Answers 6

up vote 33 down vote accepted

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

share|improve this answer
3  
Ooh. That is a really cool trick. –  Todd Lehman Feb 25 '12 at 7:52
3  
This is the best solution so far. –  Yiannis Lazarides Feb 25 '12 at 8:46
1  
Very elegant and very easy to use solution. That's great ! –  projetmbc Feb 25 '12 at 14:00
3  
One should remember, though, that this will remove the original definition of \*. –  egreg Feb 14 at 17:05
    
@egreg true. Though I've never used that, to the point that I didn't know there was one. –  Ryan Reich Feb 14 at 19:07

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

share|improve this answer
1  
Nice! And how about the Greek letters? –  Todd Lehman Feb 25 '12 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. –  Yiannis Lazarides Feb 25 '12 at 6:13
5  
\command@factory... that's epic! –  Werner Feb 25 '12 at 7:25
1  
Shouldn't the \defs be outside of the loops? –  Andrey Vihrov Feb 25 '12 at 10:20
1  
@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}

example

share|improve this answer
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. –  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}
share|improve this answer
    
What is the difference between \newcommand and \newcommand*? Also, are there any differences between this solution and @alexis's solution? –  jamaicanworm Feb 25 '12 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? –  Peter Grill Feb 25 '12 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 '14 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).

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

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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