Math & ScienceHow do I define this command?

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

How do I define this command?

Post by Yue »

josephwright wrote:
Yue wrote:
josephwright wrote:

Code: Select all

\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand\en{g}{%
  \IfNoValueTF#1{\epsilon}{\epsilon_{#1}}%
}
\begin{document}
See \( \en \) but \( \en{a} \)!
\end{document}
This works, but when I use \en{\textbf{k}}, it gives me two \epsilons. Is there no easy way to do \en{\textbf{k}} and \en?
Sorry, my error in the definition above. Try

Code: Select all

\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand\en{g}{%
  \IfNoValueTF{#1}{\epsilon}{\epsilon_{#1}}%
}
\begin{document}
See \( \en \) but \( \en{\textbf{k}} \)!
\end{document}
(Note the braces around #1 after \IfNoValueTF: I forgot those!)

Works great! Thanks! I replayed \epsilon with \en 45 times ^^, I hope this wont cause me problems, because then I can't change it back. I mean, this package and command wont cause trouble?

Recommended reading 2024:

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

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

josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

How do I define this command?

Post by josephwright »

Yue wrote: Works great! Thanks! I replayed \epsilon with \en 45 times ^^, I hope this wont cause me problems, because then I can't change it back. I mean, this package and command wont cause trouble?
Shouldn't do. xparse is part of the development efforts for LaTeX3, and is regarded as "stable" by the LaTeX Team.
Joseph Wright
Yue
Posts: 30
Joined: Sat May 08, 2010 9:58 pm

Re: How do I define this command?

Post by Yue »

Good to hear and so many friendly ppl here^^
Post Reply