3

I often have to write expressions like this in math mode:

L_t^pL_x^q

Desired output

For some reason, I am not always satisfied with typing this and I would like to define some new command with the same output. I was thinking whether it could be possible to define a command with the same output for which the code would look like the following:

\LL_t_x^p^q

I have found this post concerning tensors that looks quite related (see the first answer). The syntax used in the package "tensor" would be quite good, but I don't know how to replicate a command with that syntax.

My questions are the following:

  1. Even though the syntax in the package "tensor" is quite nice, in my deepest dreams I would like to be able to use the exact same one in the command that I wrote above: \LL_x_t^p^q. So, would there be a way of designing a command that works as above (that is, a command of the form \LL_t_x^p^q which has the same output of L_t^pL_x^q)?

  2. If not, how can I design a command with the same syntax of the package "tensor"? That is (if I understand correctly), a generic command with 3 classes of inputs \arg, \superscriptI,\superscriptII,..., \subscriptI,\subscriptII,... with the following syntax:

\commandname{\arg}{^\superscriptI_\subscriptI_\subscriptII^\superscriptII...}

(in my case, I would like to write a command of the form \LL{_x_t^p^q}, with the same output as L_t^pL_x^q).

  • Would \LL_{t,x}^{p,q} be an alternative? – egreg Nov 29 '22 at 16:08
  • Thank you for the comment. If you are talking about the syntax, yes, this is close to what I want, even though I would prefer the syntax of the package "tensor". (Just to be clear) if you are talking about the output of your command, it is not what I am looking for: I would like to have some indices after the first L and some indices after the second L – Lorenzo Pompili Nov 29 '22 at 16:14
  • It would be more natural to not have the subscript and superscript tokens as parameter separators, but preferably defnine \newcommand\LL[4]{L_{#1}^{#3}L_{#2}^{#4}} and invoke it as \LL{t{x}{p{}q} (or \LL txpq if you are lazy) – Hagen von Eitzen Nov 30 '22 at 16:51

3 Answers3

6

enter image description here

\documentclass{article}

\def\LL{\futurelet\tmp\LLx} \def\LLx{\ifx\tmp_\expandafter\LLxx\fi} \def\LLxx_#1#2^#3{L_{#1}^{#3}\LL#2}

\begin{document}

$\LL_t_x^p^q$

$\LL$

$\LL_t_x_a^p^q^b$

\end{document}

David Carlisle
  • 757,742
5

The use case shows some possible variations in the input syntax, all of which give the desired output. For example, \LL_x_t^p^q$, $\LL_x^p_t^q$, and $\LL^p_x_t^q$ all produce identical output.

EDITED to permit embraced sub or superscripts, for example, of the form \LL_{x_a}_t^p^q.

\documentclass{article} 
\newcommand\LL{
  \toks0{}\toks2{}
  \def\subbase{0}
  \def\supbase{0}
  \LLdigest
}
\newcommand\LLdigest[2]{
  \ifx_#1
    \toks\subbase\expandafter{\the\toks\subbase#1{#2}}
    \def\subbase{2}
    \def\next{\LLdigest}
  \else
    \ifx^#1
      \toks\supbase\expandafter{\the\toks\supbase#1{#2}}
      \def\supbase{2}
    \def\next{\LLdigest}
    \else
      L\the\toks0L\the\toks2 
      \def\next{#1#2}
    \fi
  \fi
  \next
}
\begin{document} 
$\LL_x_t^p^q$

$\LL_x^p_t^q$ \end{document}

enter image description here

  • 1
    Thank you! I probably need some time to "digest" your code, but it looks exactly what I wanted – Lorenzo Pompili Nov 29 '22 at 16:28
  • @LorenzoPompili You are welcome. I essentially build two token lists (0 and 2). The first sub and superscript are assigned to \toks0, later ones assigned to \toks2. When I run out of digesting sub or superscripts, I end the digestion. – Steven B. Segletes Nov 29 '22 at 16:32
4

I can propose a slightly different syntax.

\documentclass{article}
\usepackage{amsmath}

\NewDocumentCommand{\LL}{e{^}}{% \LLaux{#1}{#2}% } \NewDocumentCommand{\LLaux}{% >{\SplitArgument{1}{,}}m >{\SplitArgument{1}{,}}m }{% \LLauxa#1#2% } \NewDocumentCommand{\LLauxa}{mmmm}{% L{#1\vphantom{#2}}^{#3\vphantom{#4}} L_{#2\vphantom{#1}}^{#4\vphantom{#3}} }

\begin{document}

$\LL_{t,x}^{p,q}$

\end{document}

enter image description here

However, if all your instances of \LL have the form

\LL_t_x^p^q

that is, in every call there are two subscripts and two superscripts, the solution is more straightforward:

\newcommand{\LL}{} % for safety
\def\LL_#1_#2^#3^#4{%
  L_{#1\vphantom{#2}}^{#3\vphantom{#4}}%
  L_{#2\vphantom{#1}}^{#4\vphantom{#3}}%
}
egreg
  • 1,121,712