4

I'm looking for best practise w.r.t. double sub/superscripts. I have a bunch of macros to denote certain subclasses, the x-component, the nth order, the outside value, etc. I would like them to be able to use them independently, in arbitrary order. At first I had macros like

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\begin{document}
\newcommand{\x}[1]{{#1}_x}
\newcommand{\n}[1]{{#1}_n}
\newcommand{\additive}[1]{{#1}^\text{a}}
\newcommand{\out}[1]{{#1}^*}
$\x{\out{\additive{\n{B}}}}$
\end{document}

But that bunches things up terribly:

Removing the curly braces

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\begin{document}
\newcommand{\x}[1]{#1_x}
\newcommand{\n}[1]{#1_n}
\newcommand{\additive}[1]{#1^\text{a}}
\newcommand{\out}[1]{#1^*}
$\x{\out{\additive{\n{B}}}}$
\end{document}

This displays correctly:

However it gives me a double superscript error. I don't mind double superscripts; on the contrary, that's what I'm going for (unless someone has a better notation suggestion, I know it's congested, but it's a long document with many intricacies...)! I know I can do this manually, but is there a way to have any arbitrary order of such macros produce the latter result without error?

Thanks.

bjorne
  • 637
  • 1
    are you committed to that nested syntax? \zz{B}{\additive\out\n\x} would be a lot easier for example. – David Carlisle Feb 21 '17 at 16:21
  • @DavidCarlisle, not really committed to anything. The reason I like the nested structure is that I have shorthands, say, \newcommand{\Bax}{\x{\additive{B}}} and then I use that often because I don't want to have to type the whole thing, but sometimes I take components of that, so I want to be able to do \n{\Bax}. I don't quite understand your suggestion with \zz. – bjorne Feb 21 '17 at 16:28
  • I guess another "constraint" is that I have colleagues who like to change their minds on notation every few minutes, and so I often have to change something from a subscript to a superscript or vice versa, and so whatever I do should be able to cope with such a change without having to go through and change everywhere I've used the macro. – bjorne Feb 21 '17 at 16:32
  • \zz was just a made up name that you would replace by some more semantic name. But it is easier if you have a "container" macro as then you know when you have finished so can close any opened groups etc. with it as it is, \n can do essentially _\bgroup n so a following \x can be in the same subscript, but it is hard to know when to close the group as you don't have any distinguished macro for the whole construct. – David Carlisle Feb 21 '17 at 16:35
  • Oh, I see. Is there no way of making some kind of superscript and subscript lists, and each time I execute a command I essentially add the appropriate value to the appropriate list, and then I write each list next to a character on command. So \x\additive\n\out \zz{B} would just flush the list of sub/superscripts onto B? That way I wouldn't have to nest but would still be able to add one extra term to composite macros. Or is that too procedural? – bjorne Feb 21 '17 at 16:47
  • yes that would probably work (no time now but I'll answer later if no one else has) – David Carlisle Feb 21 '17 at 16:53

1 Answers1

4

enter image description here

Using a prefix list with a dedicated macro to mark the end of the list is a bit easier than nested calls as discussed in comments.

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}

\makeatletter
\gdef\zzsub{}
\gdef\zzsup{}
\newcommand{\x}{\g@addto@macro\zzsub{x}}
\newcommand{\n}{\g@addto@macro\zzsub{n}}
\newcommand{\additive}{\g@addto@macro\zzsup{\text{a}}}
\newcommand{\out}{\g@addto@macro\zzsup{*}}
\newcommand\zz[1]{%
#1%
\ifx\zzsub\@empty\else_{\zzsub}\fi
\ifx\zzsup\@empty\else^{\zzsup}\fi
\gdef\zzsub{}%
\gdef\zzsup{}%
}
\makeatother
\begin{document}

$\x\out\additive\n\zz{B} + \out\out\n\zz{Z}$
\end{document}

You might like to prepend items instead of appending them to the list. This would be helpful for compound constructs, like having a shorthand \newcommand{\Bax}{\x\additive\zz{B}} and then being able to add further tokens that sit further away from the core \n\Bax. In this case, prepend using this technique.

David Carlisle
  • 757,742