# Double superscript/subscript issue with macros

I would like to be able to define a macro like \newcommand{\Zs}{Z^*} and then somewhere within the text say $\Zs^T$ and have the effect of writing $Z^{* T}$. It should also be possible to write $\Zs$ without the subscript. Is there a simple solution to make this possible? Preferably, the solution should allow one to do this for many symbols with relative ease.

I guess one way is to define a macro with an argument like \newcommand{\Zss}[1]{Z^{* #1}} and the use either $\Zss{}$ or $\Zss{T}$, but this does not seem very elegant.

Can we define a blueprint command, called starred so that whenever we want a starred symbol, we can write \newcommand\Xs{\starred{X}}?

-
– Jubobs Mar 2 '14 at 16:03

You have to be disciplined and write, if a subscript is needed,

\Zs^{<superscript>}_{<subscript>}


Here's the code:

\documentclass{article}

\makeatletter
\newcommand{\passerby@genericstarred}[1]{%
#1\@ifnextchar^\passerby@eathat{^*}%
}
\def\passerby@eathat#1#2{% #1 is ^
^{*#2}%
}
\newcommand{\definegenericstarred}[1]{%
\@for\next:=#1\do{%
\expandafter\edef\csname\next s\endcsname{%
\noexpand\passerby@genericstarred{\next}}%
}%
}
\makeatother

\definegenericstarred{X,Y,Z,W}

\begin{document}
$\Xs+\Zs^T+\Ws+\Ys^2$
\end{document}


The same with LaTeX3:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\definegenericstarred}{m}
{
\clist_map_inline:nn { #1 }
{
\cs_new_protected:cpn { ##1s } { \passerby_generic_starred:n { ##1 } }
}
}

\cs_new_protected:Npn \passerby_generic_starred:n #1
{
#1 % print the main letter
\peek_catcode_remove:NTF ^ % check if a ^ follows
{ \passerby_add_star:n }  % yes, add the * to the explicit superscript
{ ^{*} }                  % no, just add the *
}
{
^{*#1} % the final superscript
}
\ExplSyntaxOff

\definegenericstarred{X,Y,Z,W}

\begin{document}
$\Xs+\Zs^T+\Ws+\Ys^2$
\end{document}


-
Thanks, this looks interesting. I couldn't understand what you mean by being disciplined though. You mean if I need a subscript I have to include the braces around both super- and subscripts? – passerby51 Mar 2 '14 at 17:19

LaTeX's \newcommand is limiting in the delimiters it allows. For more general macros, you need to use TeX's \def. I refer you to egreg's comprehensive answer to (Re)definition of commands that use delimiters other than braces (e.g. \underbrace).

\documentclass{article}

% sanity check (causes an error if \Zs is already defined as LaTeX command)
\newcommand{\Zs}{}

\makeatletter
\def\Zs{%
\@ifnextchar^%
{\@Zs}
{\@latex@warning{Missing argument for \string\Zs}\@Zs^{}}%
}
\def\@Zs^#1{%
Z^{* #1}%
}
\makeatother

\begin{document}
foo $\Zs^T$ bar $\Zs$
\end{document}

-
What if the OP just wants to write \Zs without the additional superscript? – A.Ellett Mar 2 '14 at 16:14
@A.Ellett That's not part of the question, but I'll edit my answer to handle that case. – Jubobs Mar 2 '14 at 16:15
@Jubobs, thanks. Actually, being able to write both versions, with and without the superscript, is my question. It is important that one can still write Zs without superscript. – passerby51 Mar 2 '14 at 16:20
@passerby51 See my edit. However, your question is unclear as to whether you want the argument of \Zs to be optional. You should edit your question for clarification. – Jubobs Mar 2 '14 at 16:24
@passerby51 What syntax would you like for such a macro? In particular, how would you like to specify the letter to use (e.g. X,Y,Z, or W)? – Jubobs Mar 2 '14 at 16:26