# Automatic inclusion of the following character, but with its subscripts and superscripts?

There are such commands as \vec which adds an arrow to the top of the immediate next non-space character. However, superscripts and subscripts are excluded from the scope.

Now suppose I define a command \vt as follows:

\newcommand*\vt[1]%
{\ensuremath{\mathbf{#1}}}


So it acts like \mathbf: in math mode, \vt{} makes the character inside boldfaced. However, without the brackets, it can also make the next character boldfaced, much like how \vec works. Now, suppose I need to make exactly one character boldfaced, but its subscripts and superscripts also, without using the brackets (so for example, if I want to mass copy-and-paste the command; the brackets would be a hassle). How can this be done?

• The standard latex syntax always has braces around the mandatory argument. The fact that it can sometimes be omitted for some commands is an unfortunate accident of the implementation. (that said you can use the xparse packagee argument type to define this kind of syntax. Jun 12, 2017 at 21:22
• @DavidCarlisle Interesting, I was not aware that this is an "unfortunate accident of the implementation" (I was assuming it is a feature for lazy coders). Oct 23, 2021 at 1:59

## 1 Answer

Surely not a good idea but certainly possible. Use xparse embellishments. This will not be as productive in the long run as you think it will.

\documentclass{article}
\usepackage{xparse}

\NewDocumentCommand \vt { m e{^_}}
{
\mathbf{#1}
\IfNoValueF{#2}{\sp{\mathbf{#2}}}
\IfNoValueF{#3}{\sb{\mathbf{#3}}}
}

\begin{document}

$\vt d$

$\vt d^2$

$\vt d_{x^2 - y^2}$

$\vt d_{x^2 - y^2}^2$

$\vt d^2_{x^2 - y^2}$

\end{document}


• Your code does not compile for me. I presume I have something out of date? expl3 2016/03/28 v6468 Jun 13, 2017 at 10:04
• @StevenB.Segletes You need the latest version (2017/05/29 I believe). It's the default one in TL2017. Jun 13, 2017 at 10:19
• Well, I just upgraded to expl3 2017/05/29 and it still gives me the error ! Missing \$ inserted. Perhaps I will ask over at chat... Jun 13, 2017 at 10:40
• Joseph and Ulrike helped me out. I needed not only l3kernel updated, but also l3packages. Nice answer! Jun 13, 2017 at 11:00
• This works! Thanks so much -- I got TeXShop to work after updating it to the MacTex 2017 version. One thing though -- the package siunitx seems to disable this. Is there a possible way to resolve that? Not that I would ever need the package siunitx in the environment I am currently working in, but it would be so nice to know what is happening. (And maybe to resolve a future conflict?) Jun 13, 2017 at 18:28