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} 
2
  • 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. Commented Aug 20, 2016 at 20:07
  • Thank you @DavidCarlisle. I'm in the process of learning about the process, not necessarily thinking about this particular example. Commented Aug 20, 2016 at 20:09

2 Answers 2

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} 
0
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. ;-)

4
  • I'll have to study your answer to understand what's going on, thank you! Commented Aug 20, 2016 at 20:41
  • 1
    Did you search latex.ltx for asterisks? Commented Aug 20, 2016 at 23:03
  • 1
    @GustavoMezzetti Yes, indeed Commented Aug 21, 2016 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! :-) Commented Aug 22, 2016 at 1:41

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.