Bold Roman expressions in Math Mode, with proper spacing

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

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:

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!

-
Must the subscript and superscript also be in bold roman, or in bold italic, or in ordinary math style? –  Bernard Mar 15 at 19:27
Good point, I guess I should have clarified some more. Whatever is inside the \foo{} should be in bold roman, but whatever is outside it, should be in ordinary math style. But as opposed to my first example (which has the fonts all correct), it should be right next to the x, rather than outside the mbox containing the x^k. –  mindriot Mar 16 at 1:26

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}

-
Perfect, that did it. Thanks! –  mindriot Mar 16 at 9:41

you can use \mathbf to get the upright letters -- but you have to enter them separately for base and scripts:

$\mathbf{x}^{\mathbf{k}}_t$


-
Thanks for your input! Yes, I could do that. But the thing is that in my case the x^k is inside the macro (e.g. \foo{x^k}_t), and in my case I can't separate the \mathbf{} command like that. I.e., I basically want to create a command \foo{x} which behaves exactly like \bm{x}, except it uses bold upright/roman characters instead of bold standard characters. I could modify my commands/macros to use multiple parameters to follow your suggestion, but it would be an awful mess of macros... –  mindriot Mar 16 at 1:20

You could modify the definition of \foo to instruct \bm to operate on \mathrm{#1} instead of "just" #1. (By the way, I'm assuming that since the main symbol and the superscript term should be in upright bold, the same goes for the subscript term. Please advise if this assumption is invalid.)

\documentclass{article}
\usepackage{bm}
\newcommand{\foo}[1]{\bm{\mathrm{#1}}}
\begin{document}
$\foo{x^k}$, $\foo{x^k_t}$
\end{document}

-
Thanks for your input! The difference in your example is, you have the "_t" inside the command parameter, where I wanted \foo{x^k}_t. I.e., t should not be set in bold upright, but in normal style. –  mindriot Mar 16 at 1:13