tags:

views:

132

answers:

3

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

+3  A: 

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.

Crowley
if you want to have macro specially designed for greek letters and another for latin letters you can add`\newcommand\bl[1]{\ensuremath{\mathbf{#1}}}`into preamble and use it. - It will work only for latin letters.
Crowley
+1 I guess my question is more academic than practical. Your suggestion is totally valid. I guess writing `\b X` instead of `\bX` isn't a very big distinction. Still, my original goal is to avoid the space. Your point regarding trashing existing macros is especially important. It would make very confusing code to redefine `\bf` for example.
Geoff
+1  A: 

I would recommend doing:

\newcommand{\b}[1]{\mathbf{#1}}

as Crowley says, and similar for all the other alphabets. However, if you really want to do it using LaTeX code, here's one that seems to work:

\documentclass{article}
\usepackage{amssymb}
\newcounter{char}
\setcounter{char}{1}

\loop\ifnum\value{char}<27
\edef\c{\Alph{char}}
\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\csname\expandafter\expandafter\expandafter b\expandafter\c\expandafter\endcsname\expandafter{\expandafter\mathbb\expandafter{\c}}
\addtocounter{char}{1}
\repeat

\begin{document}

\(\bZ\)
\end{document}

I lost count of how many 'expandafter's there are in that! To get lowercase letters, replace the Alph by alph.

Andrew Stacey
Would it affect greek letters too? And why to use `\bH ello \bW orld`, when you can use `\b Hello \b World`?
Crowley
+1 Cool solution. I never knew about `\expandafter` or `csname`. I think using the latter would allow you to trim your macro quite a bit.
Geoff
@Crowley: I missed the bit about greek letters. One could define a version of \alph and \Alph for greek letters. Overkill for one application, but could be useful elsewhere. With regard to your second point: I agree completely and I said so! In my documents, I use (a variant of) your solution. Nonetheless, the question of being able to loop over the alphabet was sufficiently intriguing that I thought I'd have a go. I tried to strike a tone that showed that I wouldn't _actually_ do this in practice. After all, 18 expandafters could be considered just a tab excessive!
Andrew Stacey
+4  A: 
\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$
Alexey Malistov
This is great. So line 2 is essentially a loop because it inserts itself before the next character, right? Line one could be inserted into line 2, but is separate only for readability. A two line solution! The only down side is having to type the alphabet.
Geoff
How about for a list of Greek letters? (`{Gamma}{mu}` or `\Gamma\mu` or some other reasonable list.)
Geoff
I do not understand how you want to write Greek letters. You write `\bA, \bB`, etc for `A,B,...`. What is about Greek? `\bGamma`? `\bmmu` ? I do not understand.
Alexey Malistov
Yes `\bGamma` or `\bmu` or `\bLambda` or `\blambda`. Currently I just have a list of these commands made manually.
Geoff
I added new info.
Alexey Malistov
Why do you need `\text` ?
Geoff
`\text` for scripts: `2 ^ \bgamma` for example.
Alexey Malistov