5

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}
  • 1
    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. – David Carlisle Aug 20 '16 at 20:07
  • Thank you @DavidCarlisle. I'm in the process of learning about the process, not necessarily thinking about this particular example. – JPi Aug 20 '16 at 20:09
9

enter image description here

\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}
3

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
       {\edef\reserved@a{\reserved@a#3}\advance\@tempcnta \m@ne}%
    \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}

enter image description here

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
  • 1
    Did you search latex.ltx for asterisks? – GuM Aug 20 '16 at 23:03
  • 1
    @GustavoMezzetti Yes, indeed – egreg 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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