Expressive macro for tensors; raised and lowered indices

In my opinion, the subscripts and superscripts are a single argument.

You can use the tensor package, without reinventing the wheel: it has a very handy syntax.

I also provide a \tens command according to your preferences.

\documentclass{article}
\usepackage{tensor}

%\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\tens}{mo}
 {
  #1
  \IfNoValueTF { #2 } 
   {
    \__myridium_tens_up_lookup:
   }
   {
    \__myridium_tens_down_lookup: [ #2 ]
   }
 }

\cs_new_protected:Nn \__myridium_tens_down_lookup:
 {
  \peek_charcode_ignore_spaces:NTF [
   {
    \__myridium_tens_down:w
   }
   { \kern2\scriptspace }
 }
\cs_new_protected:Npn \__myridium_tens_down:w [ #1 ]
 {
  {\mathstrut}
  \sb{#1}
  \kern-\scriptspace
  \__myridium_tens_up_lookup:
 }
\cs_new_protected:Nn \__myridium_tens_up_lookup:
 {
  \peek_catcode_ignore_spaces:NTF \c_group_begin_token
   {
    \__myridium_tens_up:n
   }
   { \kern2\scriptspace }
 }
\cs_new_protected:Nn \__myridium_tens_up:n
 {
  {\mathstrut}
  \sp{#1}
  \kern-\scriptspace
  \__myridium_tens_down_lookup:
 }
\ExplSyntaxOff

\begin{document}

\subsection*{With \texttt{tensor}}
\[
\tensor{\ddot{x}}{^\mu}=
\tensor{\Gamma}{^\mu_\alpha_\beta}
\tensor{\dot{x}}{^\alpha} \tensor{\dot{x}}{^\beta}
\]

\[
\tensor{\Gamma}{_\mu^\nu^\rho_\alpha^\nu^\rho}
\tensor{\dot{\Gamma}}{_\mu^\nu^\rho_\alpha^\nu^\rho}
\]

\subsection*{With the hand-made macro}
\[
\tens{\ddot{x}}{\mu}=
\tens{\Gamma}{\mu}[\alpha\beta]
\tens{\dot{x}}{\alpha} \tens{\dot{x}}{\beta}
\]

\[
\tens{\Gamma}[\mu]{\nu\rho}[\alpha]{\nu\rho}
\tens{\dot{\Gamma}}[\mu]{\nu\rho}[\alpha]{\nu\rho}
\]

\end{document}

enter image description here


I wouldn’t use such a syntax, but SemanTeX can be set up to accomplish something resembling this (disclaimer: I am the author). Note that you will need a recent update of SemanTeX (October or later, I think) for this example to work. Note that I also prefer defining keys dot and ddot instead of directly using the commands \dot and \ddot.

\documentclass{article}

\usepackage{semantex}

\NewVariableClass\tens[
    output=\tens,
    definekeys={
        {dot}{ command=\dot },
        {ddot}{ command=\ddot },
        {preindex}{ rightreturn, symbolputright={{}} },
        {postindex}{ rightreturn, symbolputright=\kern-\scriptspace },
    },
    definekeys[1]={
        {default}{ preindex, lower={#1}, postindex },
        {arg}{ preindex, upper={#1}, postindex },
    },
]

\begin{document}

$ \tens{\dot x}{\mu} = \tens{\dot{\Gamma}}{\mu}[\alpha][\beta] 
    \tens{\dot{x}}{\alpha} \tens{\dot{x}}{\beta} $

$ \tens{\ddot x}{\mu} = \tens{\dot{\Gamma}}{\mu}[\alpha][\beta]
    \tens{\dot{x}}{\alpha} \tens{\dot{x}}{\beta} $

$ \tens{x}[ddot]{\mu} = \tens{\Gamma}[dot]{\mu}[\alpha][\beta]
    \tens{x}[dot]{\alpha} \tens{x}[dot]{\beta} $

\end{document}

enter image description here


Personally, I would prefer to use a more keyval-based syntax, as below:

\documentclass{article}

\usepackage{semantex}

\NewVariableClass\Tensor[
    output=\Tensor,
    definekeys={
        {dot}{ command=\dot },
        {ddot}{ command=\ddot },
        {preindex}{ rightreturn, symbolputright={{}} },
        {postindex}{ rightreturn, symbolputright=\kern-\scriptspace },
    },
    definekeys[1]={
        {up}{ preindex, upper={#1}, postindex },
        {low}{ preindex, lower={#1}, postindex },
    },
]

\begin{document}

$ \Tensor{x}[dot,up=\mu] = \Tensor{\Gamma}[dot,up=\mu,low=\alpha,low=\beta]
    \Tensor{x}[dot,up=\alpha] \Tensor{x}[dot,up=\beta] $

$ \Tensor{x}[dot,up=\mu] = \Tensor{\Gamma}[dot,up=\mu,low=\alpha,low=\beta]
    \Tensor{x}[dot,up=\alpha] \Tensor{x}[dot,up=\beta] $

\NewObject\Tensor\tGamma{\Gamma}
\NewObject\Tensor\tx{x}

$ \tx[dot,up=\mu] = \tGamma[dot,up=\mu,low=\alpha,low=\beta]
    \tx[dot,up=\alpha] \tx[dot,up=\beta] $

$ \tx[dot,up=\mu] = \tGamma[dot,up=\mu,low=\alpha,low=\beta]
    \tx[dot,up=\alpha] \tx[dot,up=\beta] $

\end{document}

enter image description here


Here is something that works like you describe but with round brackets instead of curly ones. As usual such things can be a bit fragile, so occasionally you need to \relax a bit to mark it fully work, as can be seen in the second example.

\documentclass{article}
\makeatletter
\edef\tens@u{(}
\edef\tens@l{[}
\def\tens@U#1)#2{{}^{#1}\expandafter\tens@i#2\relax}
\def\tens@L#1]#2{{}_{#1}\expandafter\tens@i#2\relax}
\def\tens@i#1#2{\edef\tens@t{#1}%
\ifx\tens@t\tens@u
\expandafter\tens@U#2
\else
\ifx\tens@t\tens@l
\expandafter\tens@L#2
\else
#1#2
\fi
\fi}
\def\tens#1#2{#1\expandafter\tens@i#2}
\makeatother
\begin{document}
\begin{tabular}{rl}
works: &
$\tens{\Gamma}[\mu](\nu\rho)[\alpha](\nu\rho) \dot\tens{x}(\alpha)
\dot\tens{x}(\beta)$ \\[2em]

does not work: & 
$\tens{\Gamma}[\mu](\nu\rho)[\alpha](\nu\rho) \dot\tens{x}(\alpha)
\tens{x}(\beta)$ \\[2em]

relax and it works again: &
$\tens{\Gamma}[\mu](\nu\rho)[\alpha](\nu\rho) \dot\tens{x}(\alpha)\relax
\tens{x}(\beta)$ \\
\end{tabular}
\end{document}

enter image description here

To be clear: such macros are mainly for recreation purposes and not for the real world. These days the LaTeX world has enough other problems...