[Tex/LaTex] Bold Roman expressions in Math Mode, with proper spacing

boldmath-moderoman

I have some math formulas where I need bold roman style for some math expressions. Since they appear quite often, I need a macro for them. The obvious way is to write \newcommand{\foo}[1]{\mathbf{#1}}, which works in principle. However, it falls apart when the command argument contains a superscript, and there is a subscript added outside the command, like so: \foo{x^k}_t which results in

x^k_t with wrong spacing

where the t subscript is too far away, presumably because it's outside an mbox. Using the bm package and defining \newcommand{\foo}[1]{\bm{#1}}, I get the correct spacing as I want it:

x^k_t with right spacing but wrong font

But now I'm back to the wrong font as it's no longer in Roman style. And all approaches I've tried to combine \bm and switching to a Roman style show up fail in one way or another. I can't seem to find a solution that will look like the second case, but use a Roman font like in the first case. Any ideas on how I could solve this? Thanks in advance!

Best Answer

enter image description here

You can get from the markup you want to the markup barabra correctly said that you need:

\documentclass{article}
\usepackage{bm}
\def\foo#1{\xfoo#1\relax^\relax\valign}
\def\xfoo#1^#2\relax#3\valign{%
\mathbf{#1}\ifx\valign#2\valign\else^{\mathbf{#2}}\fi}
\begin{document}
$\foo{x}$, $\foo{x}_t$

$\foo{x^k}$, $\foo{x^k}_t$
\end{document}
Related Question