2

I would like to simplify the way to type indicator functions. So I used package bbm and defined \newcommand{\1}{\mathbbm{1}}

But I actually want something like \mathbbm{1}_{XXX}=\newcommand{1}{XXX}. So that after I typed \1 and I can continue to type {XXX} and the what comes up is \mathbbm{1}_{XXX}.

jarnosc
  • 4,266

1 Answers1

8

Just define \1 with an argument:

\newcommand{\1}[1]{\mathbbm{1}_{#1}}

Full example

\documentclass{article}

\usepackage{bbm}

\newcommand{\1}[1]{\mathbbm{1}_{#1}}

\begin{document}

$\1{x_1=x_2}$

\end{document}

enter image description here

On the other hand, the package bbm only provides bitmap fonts; you can get a scalable Type1 font from bbold.

\documentclass{article}
\usepackage{amsmath}

\DeclareRobustCommand{\1}[1]{\text{\usefont{U}{bbold}{m}{n}1}_{#1}}

\begin{document}

$\1{x_1=x_2}$

\end{document}

enter image description here

I wouldn't recommend \usepackage{bbold}, because it clobbers the \mathbb font from amssymb (or amsfonts).


Let's stick to \newcommand. When you do it, you can specify a number of arguments:

\newcommand{\foo}[2]{...}

would mean \foo should be followed by two braced groups, the arguments, like

\foo{Abc}{def}

and, in the definition text, you refer to the first braced group with #1 and to the second one by #2. In our case

\newcommand{\1}[1]{\mathbbm{1}_{#1}}

we just have one argument. This tells TeX that, when it finds \1{X}, it has to replace it with

\mathbbm{1}_{X}

The second version, with \DeclareRobustCommand is the same; this has the feature that, when the command \1 is found in a section title, it's written out as such in the auxiliary file. Just a technical thing, nothing to really worry about; using \newcommand in the second solution would be as good.

egreg
  • 1,121,712