2

I have a macro that takes an argument and bolds it (in addition to some other things). But I would like it not to bold the numbers.

For example, what I have right now is something like

\newcommand{\mybold}{\mathbf}

and \mybold{R^2} gives

enter image description here

but what I want is for \mybold{R^2} to give

enter image description here

where the "2" is not bolded.

How can I do that?

4
  • 5
    \mybold{R}^2? Mar 16, 2015 at 15:49
  • 1
    @karlkoeller: Too easy ;-)
    – user31729
    Mar 16, 2015 at 16:53
  • @karlkoeller: yea no. I'd want the code as written but have it displayed as pictured. Mar 17, 2015 at 0:38
  • 2
    It is doubtless possible - egreg has just done it. But why do you want your code to not reflect your output. such a mismatch is misleading and against LaTeX's philosophy
    – Au101
    Dec 5, 2015 at 22:32

2 Answers 2

2

Change (locally) the math code of the digits.

\documentclass{article}

\newcommand{\mybold}[1]{\mathbf{\fixnumbers\relax#1}}
\newcommand{\fixnumbers}{%
  \fixnumber{0}%
  \fixnumber{1}%
  \fixnumber{2}%
  \fixnumber{3}%
  \fixnumber{4}%
  \fixnumber{5}%
  \fixnumber{6}%
  \fixnumber{7}%
  \fixnumber{8}%
  \fixnumber{9}%
}
\newcommand{\fixnumber}[1]{\mathcode`#1=\numexpr\mathcode`#1-"7000\relax}

\begin{document}
$\mybold{R^2}+2-\mathbf{3}$
\end{document}

enter image description here

1

Run with xelatex or lualatex:

   \documentclass{article}
    \usepackage{unicode-math}
    \setmathfont{xits-math.otf}
    \setmathfont{xits-mathbold.otf}[version=mybold,range={"0020-"002A,"003A-"FFFF}]
    \newcommand\mybold[1]{{\mathversion{mybold}#1}}
    \begin{document}
        $\mybold{R^2}+2-\mathbf{3}$
    \end{document}

enter image description here

The R must be in \mathit. However, if you do not like it you canchange it to \mathup

2
  • LaTeX Font Warning: Command \mathversion invalid in math mode on input line 7. Indeed, I recalled about \mathversion being forbidden in math mode. Package bm must go through complicated patches to bolden individual symbols.
    – user4686
    Dec 6, 2015 at 14:41
  • Naturally, the code seems to go through, however does LuaLaTeX or XeLaTeX document one can use \mathversion this way with no after-effects ?
    – user4686
    Dec 6, 2015 at 14:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .