0

I wrote a code:

\documentclass{article}

\usepackage{amsmath}

\newcommand{\tvm}[1]{{\mathbf{#1}}}

\newcommand{\tdot}[1]{\expandafter\dot#1}

\newcommand{\vecta}{{\tvm{v}}{abc}^{def}} \newcommand{\vectb}{\tvm{v}{abc}^{def}}

\begin{document} $ \tdot{\vecta} $ % line a \ $ \tdot{\vectb} $ % line b \end{document}

where \tdot is used to apply \dot only to the base character of the argument that has subscripts, and 'line a' gives a correct result as

enter image description here

But 'line b' that uses \vectb gives an error. The difference comes from the braces around \tvm{v}. I would like to let the macro \tvm have its output braced somewhat internally so that I don't need to write braces for each use of \tvm.

Note that actual definition of \tvm that I use is

\makeatletter
\newcommand{\vm}[1]{
    \@tfor\next:=#1\do{
        \ifcat\next\relax
        {\boldsymbol{\next}}
        \else
        {\mathbf{\next}}
        \fi
    }
}
\makeatother

which discriminate between English and Greek.

Hermis14
  • 423
  • Why don't you define a single command, which takes 3 parameters? If you need flexibility, you can also define one command per case. – MS-SPO Oct 04 '21 at 05:21
  • as I sdaid before you ca use {\tvm{v}} to brace the base, but also use \mathrm{def} not def never use math italic for words, it is spaced to look like a product of variables. Also are you sure that \ifcat test does what you want (it seems very weird) and why use \mathbf (so getting upright bold) on some inputs but \boldsymbol (so getting bold italic) on others? why not simply use \boldsymbol{#1} or better, \bm{#1} ? – David Carlisle Oct 04 '21 at 06:55

1 Answers1

2

This works, if you use a variant of the \rdot macro (that now mysteriously became \tdot) I suggested in https://tex.stackexchange.com/a/617510/4427

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\tdot}{m} { \hermis_check_subsup:nn { \dot } { #1 } } % define analogously \tddot and so on

\cs_new_protected:Nn \hermis_check_subsup:nn { \exp_args:Nne \regex_match:nnTF { (^|_) } { \text_expand:n { #2 } } {% there is a _ or ^ in the argument \tl_set:Nx \l_tmpa_tl { \text_expand:n { #2 } } \regex_replace_once:nnN { (.?)(^|_)(.) } { \c{dot}\cB{\1\cE}\2\3 } \l_tmpa_tl \tl_use:N \l_tmpa_tl } {% no _ or ^ in the argument #1{#2} } }

\ExplSyntaxOff

\makeatletter \DeclareRobustCommand{\vm}[1]{% @tfor\next:=#1\do{% \ifcat\next\relax \boldsymbol{\next}% \else \mathbf{\next}% \fi }% } \makeatother

\newcommand{\vecta}{{\vm{v}}{abc}^{def}} \newcommand{\vectb}{\vm{v}{abc}^{def}}

\begin{document}

$\tdot{a}+\tdot{a_{x}}+\tdot{a_{x_1}}+\tdot{a^2_x}+\tdot{a^2}+\tdot{a_x^2}$

$\tdot{\mathit{abc}_x}+\tdot{\mathit{ab}^2_x}$

$ \tdot{\vecta} $ % line a

$ \tdot{\vectb} $ % line b

\end{document}

There is too much indirection in your commands, so the simplistic approach of \expandafter cannot work.

enter image description here

egreg
  • 1,121,712