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.

4
  • Welcome to TeX.SX! So you want \dot{a}_{bcd} as result? Why not just use it like this?
    – dexteritas
    Oct 2, 2021 at 13:19
  • 7
    are you sure you need a test, and not simply \newcommand\rdot[1]{\dot #1} ? Oct 2, 2021 at 13:20
  • @dexteritas I want to use \dot for macro characters. For example, I'd like to define \text = v_{abc} and apply \dot to \text.
    – Hermis14
    Oct 2, 2021 at 18:29
  • I most certainly wouldn’t advice you to define a command called \text, as this command already exists and is pretty important!
    – Gaussler
    Oct 3, 2021 at 13:21

4 Answers 4

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}
8
  • 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 2, 2021 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}} Oct 2, 2021 at 21:29
  • Wow, I didn't know there is a difference between \bf{} and \mathbf{}. It works great!
    – Hermis14
    Oct 2, 2021 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$ Oct 2, 2021 at 22:31
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

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

2
  • It seems your method is simple and practical. Would you give some comment on how to apply it to a boldface base character? Your method removes the base character for \rdot{\bf{v}_{abc}}.
    – Hermis14
    Oct 2, 2021 at 18:39
  • I don't know a quick fix for this, but e.g. egreg's solution works for this.
    – dexteritas
    Oct 4, 2021 at 10:19
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

3
  • I had to add: \usepackage{xparse}
    – dexteritas
    Oct 4, 2021 at 10:19
  • @daxteritas Then you might want to update your TeX distribution. :-)
    – Gaussler
    Oct 4, 2021 at 11:18
  • @Gaussler Ah, ok. Yes I have an older version on this machine.
    – dexteritas
    Oct 4, 2021 at 11:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .