# How to make active characters work differently in mathmode and text mode?

Still learning about active characters, catcodes, mathcodes, and such.

I know how to make *hello* produce bold in text mode and $x$ produce a bold $x$ in math mode. I also know how to make *{hello} produce bold in text mode and $*x$ produce a bold $x$ in math mode.

What I don't know is how to make *hello* produce bold in text mode and $*x$ produce a bold $x$ in math mode. (Note the number of asterisks.)

(I've run out of upvotes for the day, but will reward good answers tomorrow)

This I can do:

\documentclass{article}

\usepackage{amsmath}
\usepackage{bm}

\begingroup\lccode\~=*\lowercase{\endgroup\def~#1~}{\ifmmode\bm{#1}\else{\bfseries #1}\fi}
\mathcode*="8000
\catcode*=13

\begin{document}
*this bold*  this not
$*x*$   % produces bold math
% I want $*x$ to produce a bold x
\end{document}


This I can do, also:

\documentclass{article}

\usepackage{amsmath}
\usepackage{bm}

\begingroup\lccode\~=*\lowercase{\endgroup\def~#1}{\ifmmode\bm{#1}\else{\bfseries #1}\fi}
\mathcode*="8000
\catcode*=13

\begin{document}
*{this bold}  this not
$*x$   % produces bold math
\end{document}

• since you are making it globally active the \mathcode setting isn't doing anything (also you break other uses of * such as in tabular preambles. Aug 20 '16 at 20:07
– JPi
Aug 20 '16 at 20:09

\documentclass{article}

\usepackage{amsmath}
\usepackage{bm}

\catcode*=13
\def*{\relax\ifmmode\expandafter\bm\else\expandafter\bstar\fi}
\def\bstar#1*{\textbf{#1}}

\begin{document}
*this bold*  this not
$*x x$   % produces bold math
\end{document}


This is not very different from David's answer, but it also fixes other usages of * (for variant commands and in tabular preambles).

\documentclass{article}

\usepackage{amsmath}
\usepackage{bm}

\makeatletter
\newcommand{\jpi@active@ast}{%
\relax % in case we're in a tabular
\ifmmode
\expandafter\jpi@active@ast@math
\else
\expandafter\jpi@active@ast@text
\fi
}
\catcode*=\active
\def*{\ifincsname\string*\else\expandafter\jpi@active@ast\fi}
\def\jpi@active@ast@text#1*{\textbf{#1}}
\let\jpi@active@ast@math\bm

% fix the kernel macros
\def\@ifstar#1{\@ifnextchar *{\@firstoftwo{#1}}} % fix \@ifstar
\def\@expast#1{\@xexpast #1*0x\@@}
\def\@xexpast#1*#2#3#4\@@{%
\edef\reserved@a{#1}%
\@tempcnta#2\relax
\ifnum\@tempcnta>\z@
\@whilenum\@tempcnta>\z@\do
\let\reserved@b\@xexpast
\else
\let\reserved@b\@xexnoop
\fi
\expandafter\reserved@b\reserved@a #4\@@}
\makeatother

\begin{document}
\section*{Unnumbered}
*this bold*  this not
$*x$   % produces bold math
\begin{equation*}
a=*x
\end{equation*}
\begin{tabular}[t]{*{2}{c}}
a & bb \\
cc & d
\end{tabular}
\end{document}


Do you want to use it? No, don't. ;-)

• I'll have to study your answer to understand what's going on, thank you!
– JPi
Aug 20 '16 at 20:41
• Did you search latex.ltx for asterisks?
– GuM
Aug 20 '16 at 23:03
• @GustavoMezzetti Yes, indeed Aug 21 '16 at 7:21
• @egreg: I had done so too, and I was delighted in seeing that I couldn’t find anything that wasn’t cared for! :-)
– GuM
Aug 22 '16 at 1:41