[Tex/LaTex] New command based on command name

macros

I had this idea that would save me a considerable amount of time in writing my thesis. Here's the deal:
I need to typeset lots of equations with vectors and matrices in them. I'd like my vectors in boldface, lowercase and my matrices in boldface, uppercase. For a vectors and matrices I've defined the following in my preamble:

\renewcommand{\v}[1]{\ensuremath{\boldsymbol{#1}}}
\newcommand{\m}[1]{\ensuremath{\boldsymbol{\uppercase{#1}}}}

In context, I use \v{x} to typeset a vector x, etc.

However, wouldn't it be nice to be able to typeset \vx to create that same vector x. Then, if I'm writing about a vector a, b and c, I can just write \va, \vb and \vc.

Is this possible in LaTeX?

Best Answer

If you use extensively bold math italic for your vectors and matrices, the best solution is to define a new symbol font:

\DeclareMathAlphabet{\mathbmit}{OML}{cmm}{b}{it}

and then doing

\renewcommand{\vec}[1]{\mathbmit{#1}}
\let\matr\vec

(or, in the "macho programmer way", \renewcommand{\vec}{\mathbmit}). If a matrix is called "A", there is little sense in inputting it as \matr{a}, in my opinion.

Abbreviations such as \va, \vb and so on can be defined, but the biggest risk is to forget the commands' meaning in a short time. If you really want to follow this path, then here's how:

\documentclass{article}

\DeclareMathAlphabet{\mathbmit}{OML}{cmm}{b}{it}
\renewcommand{\vec}[1]{\mathbmit{#1}}
\let\matr\vec

\makeatletter
\@tfor\next:=abcdefghijklmnopqrstuvwxyxz\do
 {\begingroup\edef\x{\endgroup
    \noexpand\@namedef{v\next}{\noexpand\vec{\next}}%
  }\x}
\@tfor\next:=ABCDEFGHIJKLMNOPQRSTUVWXYZ\do
 {\begingroup\edef\x{\endgroup
    \noexpand\@namedef{m\next}{\noexpand\matr{\next}}%
  }\x}
\makeatother

\begin{document}
This is a vector $\vx$ and this is a matrix $\mA$.

This is a vector $\vec{x}$ and this is a matrix $\matr{A}$.

Also $\matr{\Gamma}$ works.
\end{document}

Using \ensuremath is, in my opinion, wrong, as these are mathematical entities and so they should always be in explicit math mode.

enter image description here

Related Question