6

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}}?

2

3 Answers 3

7

Updated answer

Use the e argument type.

\documentclass{article}

\ExplSyntaxOn
\NewDocumentCommand{\definegenericstarred}{m}
 {
  \clist_map_inline:nn { #1 }
   {
    \cs_new_protected:cpn { ##1s } { \genericstarred{##1} }
   }
 }
\ExplSyntaxOff

\NewDocumentCommand{\genericstarred}{me{^_}}{%
  #1^{*\IfValueT{#2}{#2}}\IfValueT{#3}{_{#3}}%
}

\definegenericstarred{X,Y,Z,W}

\begin{document}

$\Xs+\Zs^T+\Ws+\Ys^2$

$\Xs_1+\Zs^T_1+\Ws+\Ys^2_1$

$\Xs_1+\Zs_1^T+\Ws+\Ys_1^2$

\end{document}

enter image description here

Old answer (superseded by the new one)

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 *
 }
\cs_new_protected:Npn \passerby_add_star:n #1
 {
  ^{*#1} % the final superscript
 }
\ExplSyntaxOff

\definegenericstarred{X,Y,Z,W}

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

enter image description here

3
  • 1
    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, 2014 at 17:19
  • It’s been 7 years of LaTeX development since this answer was written, and you might want to update it with the e-type so that you no longer have to be as “disciplined” with the order of ^ and _.
    – Gaussler
    Mar 12, 2023 at 11:27
  • 1
    @Gaussler Really so!
    – egreg
    Mar 12, 2023 at 11:28
7

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).

enter image description here

\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}
10
  • What if the OP just wants to write \Zs without the additional superscript?
    – A.Ellett
    Mar 2, 2014 at 16:14
  • 1
    @A.Ellett That's not part of the question, but I'll edit my answer to handle that case.
    – jub0bs
    Mar 2, 2014 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, 2014 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.
    – jub0bs
    Mar 2, 2014 at 16:24
  • 1
    @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)?
    – jub0bs
    Mar 2, 2014 at 16:26
2

I don't have enough reputation to write a comment so posting as an answer. Try {\Zs}^T. It looks like the curly brackets forces the \Zs to act as an independent entity therefore avoiding the double superscript problem.

Edit: There seems to be another problem here. Since the second superscript is applied with \Zs as the body, the second superscript shows up bigger than the first one.

1
  • 2
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Mar 11, 2023 at 13:34

You must log in to answer this question.

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