1

I want to create a command for adding a superscript * to a variable, indicating its "optimal" value. However, when I do this, the superscript is added after the subscript instead of right above:

\documentclass{article}
\newcommand{\optimal}[1]{{#1}^*}
\begin{document}
$V_N^*$ vs. $\optimal{V_N}$
\end{document}

enter image description here

My question is: how can I write a newcommand which would add the superscript above the subscript?

4
  • 3
    \newcommand{\optimal}[1]{#1^*}
    – user121799
    Commented Nov 6, 2018 at 17:39
  • 1
    @marmot Please add an answer, I think an explanation will be useful to future users (and to the OP, of course)!
    – CarLaTeX
    Commented Nov 6, 2018 at 18:00
  • @CarLaTeX OK, I tried. If you find a better way, I will be very happy to remove my answer. ;-)
    – user121799
    Commented Nov 6, 2018 at 18:11
  • @marmot No, I leave it to egreg :)
    – CarLaTeX
    Commented Nov 6, 2018 at 18:40

1 Answer 1

2

For what it's worth: I suggest to define the command as \newcommand{\optimal}[1]{#1^*} because

enter image description here

\documentclass{article}
\newcommand{\oldoptimal}[1]{{#1}^*}
\newcommand{\newoptimal}[1]{#1^*}
\begin{document}
\begin{itemize}
 \item The original command, which I renamed \verb|\oldoptimal|, has additional
 braces:
 \[\oldoptimal{V_N}={V_N}^*\qquad\mbox{or}\qquad
 \verb|\oldoptimal{V_N}|=\verb|{V_N}^*|\;.\]
 \item In order to get the star where you want it to be, you thus need to remove
 the braces. This is what \verb|\newoptimal| does:
 \[\newoptimal{V_N}=V_N^*\qquad\mbox{or}\qquad
 \verb|\newoptimal{V_N}|=\verb|V_N^*| \;.\]
\end{itemize}
\end{document}
2
  • How would you handle \newoptimal{V_N^1}?
    – Werner
    Commented Nov 6, 2018 at 18:20
  • @Werner {\newoptimal{V_N}}^1 or define it with an optional argument. I guess that this was not the question, right?
    – user121799
    Commented Nov 6, 2018 at 18:22

You must log in to answer this question.

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