Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
by Yue on Fri May 21, 2010 12:49 pm
I want to make a command \en so that \en{x} gives \epsilon_{x} and \en gives \epsilon.
\newcommand{\en}[1]{\epsilon_{#1}} does not work because this will always take a argument.
-
Yue
-
- Posts: 30
- Joined: Sat May 08, 2010 8:58 pm
by gmedina on Fri May 21, 2010 1:11 pm
Hi, try this: - Code: Select all
\documentclass{book}
\newcommand*{\en}[1][]{\epsilon\sb{#1}}
\begin{document}
$\en[x]$
$\en$
\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
-

gmedina
-
- Posts: 2056
- Joined: Wed Jul 11, 2007 10:45 pm
- Location: Colombia
by josephwright on Fri May 21, 2010 1:19 pm
- Code: Select all
\documentclass{article} \usepackage{xparse} \NewDocumentCommand\en{g}{% \IfNoValueTF#1{\epsilon}{\epsilon_{#1}}% } \begin{document} See \( \en \) but \( \en{a} \)! \end{document}
Joseph Wright
-
josephwright
-
- Posts: 608
- Joined: Tue Jul 01, 2008 1:19 pm
-
by Yue on Fri May 21, 2010 1:27 pm
Thx guys, I will use the short one.
-
Yue
-
- Posts: 30
- Joined: Sat May 08, 2010 8:58 pm
by gmedina on Fri May 21, 2010 2:00 pm
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}
Thank you, Joseph, for bringing the xparse package to my attention; even with this short example I can tell I like it (the syntax for both the command definition and for the command "in action" is more "natural"). In my free time (if any) I will have a look at the packages from the xpackage bundle.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
-

gmedina
-
- Posts: 2056
- Joined: Wed Jul 11, 2007 10:45 pm
- Location: Colombia
by josephwright on Fri May 21, 2010 2:16 pm
gmedina wrote:Thank you, Joseph, for bringing the xparse package to my attention; even with this short example I can tell I like it (the syntax for both the command definition and for the command "in action" is more "natural"). In my free time (if any) I will have a look at the packages from the xpackage bundle.
Not a problem. I can't claim to have had the ideas for xparse, but I did write a lot the current implementation, so I do hope it is useful.
Joseph Wright
-
josephwright
-
- Posts: 608
- Joined: Tue Jul 01, 2008 1:19 pm
-
by Yue on Fri May 21, 2010 2:26 pm
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? Maybe it is best I use \newcommand{\en}[1]{\epsilon_{#1}} and use \epsilon for normal.
-
Yue
-
- Posts: 30
- Joined: Sat May 08, 2010 8:58 pm
by gmedina on Fri May 21, 2010 2:33 pm
Use curly braces to enclose the bold-faced expresion: - Code: Select all
\( \en{{\mathbf{k}}} \)
1,1,2,3,5,8,13,21,34,55,89,144,233,...
-

gmedina
-
- Posts: 2056
- Joined: Wed Jul 11, 2007 10:45 pm
- Location: Colombia
by Yue on Fri May 21, 2010 2:42 pm
gmedina wrote:Use curly braces to enclose the bold-faced expresion: - Code: Select all
\( \en{{\mathbf{k}}} \)
Okay thx for your help, but I have already used \en{\mathbf{k}} like 100 times in my project, hard to change it all:P. In the other posters idea I have to use \en[] instead of \en{}, hard to change as well.
-
Yue
-
- Posts: 30
- Joined: Sat May 08, 2010 8:58 pm
by josephwright on Fri May 21, 2010 3:46 pm
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!)
Joseph Wright
-
josephwright
-
- Posts: 608
- Joined: Tue Jul 01, 2008 1:19 pm
-
Return to Math & Science
Users browsing this forum: No registered users and 4 guests
|