22

I often have a whole bunch of definitions such as

\newcommand{\R}{\ensuremath{\mathbb{R}}}

with R replaced by R,Z,C, etc.

I might also have a bunch of math operators or category definitions, e.g.

\newcommand{\cC}{\ensuremath{\mathscr{C}}}

What I would love is a way I could define this all at once. For example I could have some function \bb that I could just write

\bb{Z,R,C,...}

\operators{Hom,Tor,Ext,....}

\category{C,S,H,...}

and this would then allow me to just use \Z for \mathbb{Z} in the document.

Is such a thing possible?

Werner
  • 603,163
Qwirk
  • 785
  • 3
    possible duplicate of http://tex.stackexchange.com/questions/48 – Zev Chonoles Apr 10 '12 at 06:41
  • 2
    Just a remark: I would recommend not using \ensuremath with commands like \N. I don't think that $\N$ costs much more typing than \N. And imagine you want to write $-\N$ and you forget the dollars and write -\N, then it gets completely different meaning. – yo' Apr 10 '12 at 08:23
  • 3 wonderful answers, so I'll go with the community! Thanks to all responders – Qwirk Apr 13 '12 at 05:33

3 Answers3

25

etoolbox's list parser \docsvlist is an ideal candidate for this:

enter image description here

\documentclass{article}
\usepackage{amssymb}% http://ctan.org/pkg/amssymb
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\newcommand{\bb}[1]{%
  \renewcommand*{\do}[1]{% \do to each item in list
    \expandafter\newcommand\csname ##1\endcsname{\ensuremath{\mathbb{##1}}}%
  }%
  \docsvlist{#1}% Process list
}
\begin{document}
\bb{R,N,Z,Q,C}% Create \mathbb macros
Real: \R, Natural: \N, Integer: \Z, Rational: \Q, Complex: \C
\end{document}

The macro \do is applied to each entry in the CSV list.

Werner
  • 603,163
14

Here's a possible solution:

\documentclass{article}
\usepackage{amssymb}

\newcommand{\processlist}[3][\relax]{% 
  \def\listfinish{#1}% 
  \long\def\listact{#2}% 
  \processnext#3\listfinish} 
\newcommand{\processnext}[1]{% 
  \ifx\listfinish#1\empty\else\listact{#1}\expandafter\processnext\fi} 

\newcommand{\bb}[1]{%
  \expandafter\newcommand\csname #1\endcsname{\ensuremath{\mathbb{#1}}}}

\begin{document}
\processlist{\bb}{{R}{N}{Z}{Q}}
Real: \R, Natural: \N, Relative: \Z, Fractional: \Q
\end{document}

I should add that I combined these two answers to come up with the above.

André
  • 2,345
14

Here's a more abstract (and quite shorter) way:

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
% #1 = command to use
% #2 = optional prefix
% #3 = list
\NewDocumentCommand{\createbunch}{ m O{} m }
 {
  \clist_map_inline:nn { #3 } { \cs_new_protected:cpn { #2 ##1 } { #1 { ##1 } } }
 }
\ExplSyntaxOff

\createbunch{\mathbb}{Z,R,C}

\createbunch{\operatorname}{Hom,Tor,Ext}

\createbunch{\mathcal}[c]{C,S,H}

% This is just for showing the meaning of some of the created macros
\makeatletter
\newcommand\Meaning[1]{\texttt{\string#1: \expandafter\strip@prefix\meaning#1}}
\makeatother
\begin{document}

\Meaning\Z

\Meaning\R

\Meaning\Hom

\Meaning\cS

\end{document}

enter image description here

egreg
  • 1,121,712