Math & ScienceIt is possible to make this command?

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
Yue
Posts: 30
Joined: Sat May 08, 2010 9:58 pm

It is possible to make this command?

Post by Yue »

I am writing a paper and I have defined these commands:

Code: Select all

\newcommand{\creau}[1]{u_{#1}^*}
\newcommand{\anniu}[1]{u_{#1}^{\vphantom{*}}}
The

Code: Select all

\vphantom
is there to e.g make the k's in

Code: Select all

\creau{k}\anniu{k}
be at the same height.

So now, when I write

Code: Select all

{\anniu{k}}^2
, it looks so ugly because of the $\phantom$ command.

Is there a way to define

Code: Select all

\anniu{}
so that when I write

Code: Select all

{\anniu{k}}^2
, I get the normal

Code: Select all

u_k^2
?

I could of course change it all manually, but as I have used the

Code: Select all

\anniu{}
100 times, It will be very hard...
Last edited by Yue on Wed Jul 07, 2010 6:04 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

meho_r
Posts: 823
Joined: Tue Aug 07, 2007 5:28 pm

It is possible to make this command?

Post by meho_r »

Even without \vphantom, you're in trouble here because you haven't used custom made commands correctly. What I mean is that {\anniu{k}}^2 is just wrong for the purpose because it is not the same as u_k^2 but actually {u_k}^2.

So, when you're using custom made commands which may have a second argument too, you may define them with two arguments and a default value for times when only one argument is specified (but which is ignored when you actually do specify both arguments), something like this:

Code: Select all

\documentclass{book}

\newcommand{\creau}[1]{u_{#1}^*}
\newcommand{\anniu}[2][\vphantom{*}]{u_{#2}^{#1}}% Inside the second square brackets set comes a default value

\begin{document}

$\creau{k}-u_k^2-{u_k}^2-\anniu{k}-\anniu[2]{k}$

\end{document}
So, in the example, \anniu{k} will result in u_k^{\vphantom{*}} (since only one argument is specified so the default value is in effect), while \anniu[2]{k} will give u_k^2, ignoring \vphantom{*} completely because both arguments are explicitly given.
Last edited by meho_r on Wed Jul 07, 2010 9:52 am, edited 1 time in total.
Yue
Posts: 30
Joined: Sat May 08, 2010 9:58 pm

Re: It is possible to make this command?

Post by Yue »

Thx this was helpful
Post Reply