5

I'm not good at TeX programming. I've looked for the method that detects the subscript or superscript of the argument in macro. Existing relevant answers using the xifthen package (Detecting subscript in command argument) are somewhat verbose for me to understand. Specifically, I want to make a macro such that if the argument has a subscript, \dot{} is applied only to the base character. It would have the form

\newcommand{\rdot}[1]{'code body'}

For example, \rdot{a_{bcd}} will give enter image description here instead of enter image description here that \dot{a_{bcd}} gives. \rdot should apply to superscripts in the same way.

Edit: I noticed that some macros in the answers yield a different result for boldface letters. For example, consider \rdot{\bf{v}_{abc}}. I didn't know it would make such a difference.

Hermis14
  • 423

4 Answers4

5

Testing for _ is fragile and likely to fail if the subscript is hidden in a macro or made with \sb rather than _ or perhaps if it follows ^ so the base isn't "everything before _" .

I don't think it is needed as the accent will naturally apply to the first token or brace group.

enter image description here

\documentclass{article}

\newcommand\rdot[1]{\dot #1}

\begin{document}

$\rdot{a_{bcd}}$ and $\rdot{a}$ or even $\rdot{a^{2}_b}$

\end{document}

David Carlisle
  • 757,742
  • Thank you! But it gives a different result for a character inside another macro; for example, $ \bf{v}_{a} $. What would be a correction for that? – Hermis14 Oct 02 '21 at 18:16
  • @Hermis14 well of course \bf should never be used in lates, and if you do use it (when it os defined, which it is not defined by default) the syntax would be {\bf a} not \bf{a} then it would work with \rdot as would teh standard latex \rdot{\mathbf{a}_{bcd}} in general you can use {} as in \rdot{{abcdef}_{bcd}} – David Carlisle Oct 02 '21 at 21:29
  • https://tex.stackexchange.com/a/617514/1090 – David Carlisle Oct 02 '21 at 22:01
  • Wow, I didn't know there is a difference between \bf{} and \mathbf{}. It works great! – Hermis14 Oct 02 '21 at 22:23
  • @Hermis14 \bf would make the rest of the expression be bold (or be undefined, it is not a standard latex command) compare $\mathbf{a}bc$ and $\bf{a}bc$ – David Carlisle Oct 02 '21 at 22:31
  • I have a problem. When I define \newcommand{\vect}{{\mathbf{v}}_{a}} and apply \rdot{\vect}, the trick doesn't work. – Hermis14 Oct 03 '21 at 00:49
  • @Hermis14 why questions should always have a full example.... If you add your \vect code to the example here you get this output ie it works fine. I would guess you are using amsmath version of \dot which gives an error in that case, you could use \newcommand\rdot[1]{\expandafter\dot #1} – David Carlisle Oct 03 '21 at 09:47
  • It works robustly. I really appreciate your persistent advice! – Hermis14 Oct 03 '21 at 13:26
3

This is a problem that has been bothering me, too. For instance, if you define a command \abcd that expands to a_{bcd}, then \dot{\abcd} expands to \dot{a_{bcd}} rather than \dot{a}_{bcd}. You can hack your way around this, but it’s never going to work robustly enough to cover all the cases you might run into. That was part of my motivation for developing the package semantex which allows things like this to work, but with a different syntax:

\documentclass{article}

\usepackage{semantex}

\NewVariableClass\MyVar[ output=\MyVar, define keys={ {dot}{ command=\dot }, }, ]

\NewObject\MyVar\va{a} \NewObject\MyVar\vb{b} \NewObject\MyVar\vc{c} \NewObject\MyVar\vd{d} \NewObject\MyVar\vect{\mathbf{v}}[sep i=\va]

\begin{document}

( \va[\vb\vc\vd, dot] )

( \va[\vb\vc\vd][dot] )

\NewObject\MyVar\abcd[copy=\va,sep i=\vb\vc\vd]

( \abcd[dot] )

( \vect[dot] )

\end{document}

enter image description here

Gaussler
  • 12,801
2

You can use the command \IfSubStr (of package xstring) to check if the parameter contains _ and then use \StrBefore and \StrBehind. This places the dot also centered over the variable, if it consists of multiple characters.

Code:

\documentclass{article}

\usepackage{xstring}

\makeatletter \newcommand{\rdot}[1]{% \IfSubStr{#1}{}{% \StrBefore{#1}{}[@tempa]% \StrBehind{#1}{}[@tempb]% \dot{@tempa}{@tempb}% }{% #1% }% } \makeatother

\begin{document}

$\rdot{a}, \rdot{a_{bcd}}, \rdot{asdf_{bcd}}$

\end{document}

Result:

enter image description here

dexteritas
  • 9,161
2

I believe it's more natural to type \dot{a}_{x}, but if you really want to, you can do it.

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\rdot}{m} { \hermis_check_subsup:nn { \dot } { #1 } } % define analogously \rddot` and so on

\cs_new_protected:Nn \hermis_check_subsup:nn { \regex_match:nnTF { (^|_) } { #2 } {% there is a _ or ^ in the argument \tl_set:Nn \l_tmpa_tl { #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

\begin{document}

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

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

\end{document}

enter image description here

egreg
  • 1,121,712