3

I need \mathbbm to print a colored output.

I tried:

\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{bbm}

\let\oldmathbbm\mathbbm \def\mathbbm#1#{\mathbbmaux{#1}} \newcommand*\mathbbmaux[2]{{\color{red}\oldmathbbm#1{#2}}}

\pagestyle{empty} \begin{document}

$\mathbbm{i}$ ${\mathbbm i}$

\end{document}

but the string ${\mathbbm i}$ returns an error. Where Am I wrong?

I need this trick to spot if authors used \mathbbm, directly in the pdf file. (I pass this strings to the pdflatex engine without modifying the source .tex file.)

Gabriele
  • 1,815
  • The error comes because with \def\mathbbm#1#{...} you are defining \mathbbm so that it must find an opening brace. Why are you doing that at all? – campa Mar 12 '21 at 21:20
  • @campa I updated my question giving an explanation of my purpose. – Gabriele Mar 12 '21 at 21:29
  • I understand that, but I still don't understand the reason for the brace trick #1#{. – campa Mar 12 '21 at 21:30
  • Honestly, I don't remember... it's an old piece of code I've been using for a while, that never gave a problem until this case. Since I use it in a weird way I though \renewcommand didn't work. – Gabriele Mar 12 '21 at 21:35

2 Answers2

3

I would rather use renewcommand:

%\documentclass[11pt]{article}
\documentclass[border=3.141592]{standalone}
\usepackage{xcolor}
\usepackage{bbm}

\let\oldmathbbm\mathbbm \renewcommand\mathbbm[1]{\color{red}\oldmathbbm{#1}}

\begin{document}

$\mathbbm{i}$ ${\mathbbm i}$ $\oldmathbbm{i}$

\end{document}

enter image description here

Zarko
  • 296,517
3

I do not recommend bbm, as its obsolete font format does not display correctly in a PDF. (And nobody prints DVIs on a laser printer any more.)

This lets you choose a more meaningful name, instead of redefining \mathbbm to mean something other than what someone reading your source would assume. This has the happy side-effects of simplifying your code and solving your problem.

\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage[svgnames]{xcolor}
\usepackage[bb=dsserif]{mathalpha}

\newcommand\mathredbb[1]{\textcolor{Red}{\mathbb{#1}}}

\pagestyle{empty} \begin{document}

$i \in \mathredbb{i}$, $i \in {\mathredbb i}$

\end{document}

dsserif sample

Other nice things you can do with mathalpha are use a heavier blackboard bold as \mathbbb, select from an extensive list of math alphabets, and scale the font with the bbscaled= package opton.

In LuaLaTeX or XeLaTeX, you could use the same code with unicode-math rather than mathalpha.

Davislor
  • 44,045
  • indeed I need this trick to spot if authors used mathbbm directly in the pdf file. (I pass this strings to the pdflatex engine without modifying the source .tex file.) – Gabriele Mar 12 '21 at 21:26
  • @GabrieleNicolardi Ah! Classic XY problem! That makes more sense. Other methods: remove the mathbbm package, redefine \mathbbm to give an error message, do a search. – Davislor Mar 12 '21 at 22:31