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?

  • 3
    \newcommand{\optimal}[1]{#1^*} – marmot Nov 6 '18 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 Nov 6 '18 at 18:00
  • @CarLaTeX OK, I tried. If you find a better way, I will be very happy to remove my answer. ;-) – marmot Nov 6 '18 at 18:11
  • @marmot No, I leave it to egreg :) – CarLaTeX Nov 6 '18 at 18:40
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}
  • How would you handle \newoptimal{V_N^1}? – Werner Nov 6 '18 at 18:20
  • @Werner {\newoptimal{V_N}}^1 or define it with an optional argument. I guess that this was not the question, right? – marmot Nov 6 '18 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.