Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
3  
possible duplicate of tex.stackexchange.com/questions/48 –  Zev Chonoles Apr 10 '12 at 6: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 8:23
    
3 wonderful answers, so I'll go with the community! Thanks to all responders –  Qwirk Apr 13 '12 at 5:33

3 Answers 3

up vote 25 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.