Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quick way to make 26 macros (one for each letter)

Tags:

latex

Instead of making a macro for each letter, as in

\def\bA{\mathbf{A}}
...
\def\bZ{\mathbf{Z}}

Is there a way to loop over a character class (like capital letters) and generate macros for each? I'd also like to do the same for Greek letters (using bm instead of mathbf).

like image 501
Geoff Avatar asked Jun 01 '10 20:06

Geoff


3 Answers

\def\mydefb#1{\expandafter\def\csname b#1\endcsname{\mathbf{#1}}}
\def\mydefallb#1{\ifx#1\mydefallb\else\mydefb#1\expandafter\mydefallb\fi}
\mydefallb ABCDEFGHIJKLMNOPQRSTUVWXYZ\mydefallb

New for Greek

\def\mydefgreek#1{\expandafter\def\csname b#1\endcsname{\text{\boldmath$\mathbf{\csname #1\endcsname}$}}}
\def\mydefallgreek#1{\ifx\mydefallgreek#1\else\mydefgreek{#1}%
   \lowercase{\mydefgreek{#1}}\expandafter\mydefallgreek\fi}
\mydefallgreek {beta}{Gamma}{Delta}{epsilon}{etaex}{Theta}{Iota}{Lambda}{kappa}{mu}{nu}{Xi}{Pi}{rho}\mydefallgreek


$\bGamma\bDelta \bTheta \bLambda \bXi \bPi $

$\bbeta \bgamma\bdelta \bepsilon \betaex \btheta \biota \blambda \bkappa \bmu \bnu \bxi \bpi \brho$
like image 143
Alexey Malistov Avatar answered Nov 16 '22 05:11

Alexey Malistov


Expanding on Andrew's answer, here is a solution without \expandafter:

\makeatletter
\@tempcnta=\@ne
\def\@nameedef#1{\expandafter\edef\csname #1\endcsname}
\loop\ifnum\@tempcnta<27
  \@nameedef{b\@Alph\@tempcnta}{\noexpand\mathbb{\@Alph\@tempcnta}}
  \advance\@tempcnta\@ne
\repeat

This will define \bA, \bB, and so on, to expand to \mathbb{A}, \mathbb{B}, and so on.

like image 6
egreg Avatar answered Nov 16 '22 07:11

egreg


Wouldn't be better to define one command

\newcommand\bm[1]{\ensuremath{${\boldmath$#1$}}$}

and it can be used both in text mode and math mode. Usage:

\[\bm{F(x)}=\int\bm\delta(x)\ dx]
\where \mb F is blah blah blah and \bm \delta is halb halb halb...

Result:
F(x)='inegral delta(x)'dx
Where F is blah blah blah and 'delta' is halb halb halb...

Outer dollars are there to leave math (roman) mode because \boldmath command has no effect in math mode. Inner ones switch back to math (bold). Additional braces (${\boldmath) ensures that \boldmath command will work only with #1

Another advantage of this code is testing new commands for existence of \bb and \bg. So you can't crash LaTeX makros easily.

I hope this is what you're looking for.

like image 4
Crowley Avatar answered Nov 16 '22 06:11

Crowley