2

I am typesetting a long mathematical text (lecture notes), in which I use the alphabets

\mathbb A
\mathcal A
\mathfrak A

very often, so I decided to name them

\A \sA \fA

(For bb A, script A, fraktur A resp.). Now I want to shorten my preamble by creating a helper

\newcommand{\mathletter}[1]{%
    \newcommand{\#1}{\mathbb #1}
    \newcommand{\s#1}{\mathcal #1}
    \newcommand{\f#1}{\mathfrak #1}
}

which doesn't work since the \#1 etc. seem to not be expanded.
How can I wirte a macro which defines those three commands given a single letter as input?

Thanks for any help.

AlexR
  • 253
  • It is strange. Why not define 3 new commands? If your code works what would you type: \mathletter{A}? – Sigur Jan 27 '14 at 14:23
  • Because that means 26*3 = 78 lines of code vs. 36+5 = 41 lines, about half and much more readable than a bunch of \newcommand{\X}{\mathbb X} lines – AlexR Jan 27 '14 at 14:26
  • You might need to keep in mind that some commands are already defined (for example \O). – Snicksie Jan 27 '14 at 14:27
  • @Sigur No, latex complains about LaTeX Error: Command \# already defined. when doing that. – AlexR Jan 27 '14 at 14:27
  • 78? Would you define new commands for all letters? You are remaking the wheel. – Sigur Jan 27 '14 at 14:27
  • @Snicksie Thanks for the heads-up, since I prefer \emptyset, I can live without \O, so I'll just redefine it – AlexR Jan 27 '14 at 14:29
  • 2
    This might be what you're looking for: http://tex.stackexchange.com/questions/28704/defining-a-newcommand-with-variable-name-inside-another-newcommand?rq=1 – Snicksie Jan 27 '14 at 14:30
  • @AlexR \o is not empty set. These very short names does not help much in the readability of your source code. – daleif Jan 27 '14 at 14:31
  • @Snicksie Looks very promising, I'll try it out. – AlexR Jan 27 '14 at 14:31
  • @daleif I am TeXing math equations with lots and lots of mathbb, I have found the code to become much more readable by defining those short commands, plus the additional time save on typing them out. But that's up to preference I guess. – AlexR Jan 27 '14 at 14:33
  • 1
    Does this suffice: \documentclass{article} \usepackage{amssymb} \def\B#1{\mathbb #1} \def\C#1{\mathcal #1} \def\F#1{\mathfrak #1} \begin{document} $\B A \C A \F A $ \end{document} – Steven B. Segletes Jan 27 '14 at 14:34
  • @StevenB.Segletes That's also a good way of doing it, but I don't want to retype 35 pages of TeX :D – AlexR Jan 27 '14 at 14:36
  • 1
    @AlexR, you don't need to retype. You can use Find/Replace tools. – Sigur Jan 27 '14 at 14:37
  • @AlexR, my dislike is the very short names. Over time I think it is a lot better to name macros for what they are used for instead of forcing, say, coauthors to guess what a specific macro is used for. – daleif Jan 27 '14 at 14:39
  • @daleif Agreed, but those are only for my personal use, so there are no coauthors who have to guess anything :) – AlexR Jan 27 '14 at 14:51
  • You say "I don't want to retype 35 pages of TeX", but I think any good editor should be able to do a global search and replace that would take care of 99% of it. – Steven B. Segletes Jan 27 '14 at 16:04

1 Answers1

0

Thanks to @Snicksie, I have been able to come up with the following:

Note that this is not really "beautiful" and that it overwrites the commands \H,\L,\O,\P,\S but I have confirmed that I need none of them and "manually" undefined them so I get errors when I oversee any other commands.

\newcommand{\mathletter}[1]{%
    \expandafter\newcommand\csname #1\endcsname{\mathbb #1}
    \expandafter\newcommand\csname s#1\endcsname{\mathcal #1}
    \expandafter\newcommand\csname f#1\endcsname{\mathfrak #1}
}%
\let\H\undefined
\let\L\undefined
\let\O\undefined
\let\P\undefined
\let\S\undefined
\mathletter A
\mathletter B
\mathletter C
...
\mathletter Z

Another very elegant solution, thanks to @StevenB.Segelets is to define:

\def\B#1{\mathbb #1}
\def\C#1{\mathcal #1}
\def\F#1{\mathfrak #1}

And use those slighly different commands then. This doesn't redefine any existing macros and therefor is probably a cleaner solution, but requires to change previous syntax (\A, \sA, \fA) in all documents and thus requires some "rewriting".

AlexR
  • 253