GeneralUsing xparse for multiple arguments in equation

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
amp7
Posts: 2
Joined: Tue May 27, 2014 1:33 am

Using xparse for multiple arguments in equation

Post by amp7 »

Hi, I've only been using Latex for 1 week and am trying to improve the efficiency of writing parts of the same equation over and over. Please find attached images.
In this equation, the expression \frac{V_{SE} \pi c \sin a}{1+\sqrt{1-c^2}} is constant, but the P term changes and sometimes a t appears in the numerator. I'm trying to use xparse to be able to change the P and t terms but can't get it to work. A Missing $ inserted error actually shows up
Here's my code attempt in the preamble

Code: Select all

\DeclareDocumentCommand{\lf} {0{P_{mean}} 0{t} m} {\ensuremath{\frac{#1 V_{SE}\pi c #2 }{1+\sqrt{1-c^2} }}}
I'm not too sure how to call the function in the body.
I'd like it so that the default P term is P_{mean} but can change to P_{min} or P_{max}, and default t term is there but can disappear by changing the argument.
I've tried reading the xparse documentation but am still quite confused.
Any help would be great, thanks
Attachments
W_C.png
W_C.png (14.24 KiB) Viewed 4059 times
W_E.png
W_E.png (14.12 KiB) Viewed 4059 times

Recommended reading 2024:

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

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

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Using xparse for multiple arguments in equation

Post by Johannes_B »

Please be careful, a 0 (zero) is not an O (as in optional) ;)

The very best and safest way to find the causes of errors is working with a small example, if there is an error, you can boil it down to just the minimum, most times the problem is obvious. You can find out how to do that reading about it: How to prepare a minimum example

Please see the code below, you can click on »Open in writelatex« and see the output and the code side by side.
One question is remaining: Is it worth it, to do this? If you handle this very term multiple times in your document, than i think it is worth it. If not, and this equation appears just one time only, just take the time to write it down. You'll get lost in documentaion and the attempt to save time was all in vain. FYI: Looking at the code, commenting it, writing this answer (and drinking coffee while working) took me half an hour.

Code: Select all

\documentclass{article}
\usepackage{mathtools}
\usepackage{xparse}
\usepackage{blindtext}
\usepackage{physics}
\NewDocumentCommand{\term} { 
	O {P_\text{mean}}%optional, defaults to P_mean
	O {t}%optional, defaults to t
} {
	\frac{#1 V_{\text{SE}} \, \pi c #2 \cdot \sin a }
	{1+\sqrt{1-c^2} }  }
	%As i didn't see, what the mandatory argument
	%would/should be, i simply left it out.
\begin{document}
\[ \term \]
\[ \term[P] \]
\[ \term[P_\text{min}][2t] \]
Pay attention, if you want to leave out the P (default instead)
but want to change the t part.
\[ \term[t^3] \]
\[ \term[][t^2] \]
\begin{align*}
	W_c = \oint P \dd{V_c} &=%Have a look at package physics
	- \term\\
	&= - \term[P_\text{min}] \cdot
	\frac{\sqrt{1+c}}{\sqrt{1-c}}\\
	&= -\term[P_\text{max}] \cdot
	\frac{\sqrt{1-c}}{\sqrt{1+c}}
\end{align*}

What happens if we want to use our command in text? 
Try it out by deleting the comment sign %\term
Usually, having ensuremath in your document is discouraged.
Remember, you can still do $\term$. Please compare both outputs.
\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
amp7
Posts: 2
Joined: Tue May 27, 2014 1:33 am

Using xparse for multiple arguments in equation

Post by amp7 »

Hi Johannes – sorry for not giving a minimal example. I will remember to do so in the future.

Thank you for your code; it is very helpful. Thank you for pointing out that the syntax is O not zero. Is there any way to remove the t term and keep P_mean as default without explicitly writing \term[P_\text{mean}][]?

Also, isn't the fraction the mandatory argument since it's the last argument of the function?
Is there a reason you used P_\text{mean} rather than P_{mean} ?
Why is \ensuremath discouraged?

You are correct; I need to consider the trade-off. I would think recycling parts of an equation is worth it and was hoping this exercise would help master the multiple argument syntax in xparse. After all, isn't the point of Latex to help automate writing certain terms?

Thank you again
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Using xparse for multiple arguments in equation

Post by Johannes_B »

If you want to change the part with the t, leaving P the default, you should try to evaluate, which event will be more often.
If you often need to change t, not P, you should simply change the order, in which they are used.

A LaTeX macro doesn't need a mandatory argument, don't think of it as a mathematical function, but as Lego building blocks.

In the code below are examples, why i put mean/min/max within \text{ }. They are words, and therefor text. Not using text for them makes them individual variables

You can read about ensuremath at http://tex.stackexchange.com/questions/ ... math-macro

Code: Select all

\documentclass{article}
\usepackage{mathtools}
\usepackage{xparse}
\usepackage{blindtext}
\usepackage{physics}
\NewDocumentCommand{\term} { 
	O {P_\text{mean}}%optional, defaults to P
	O {t}%optional, defaults to t
} {
	\frac{#1 V_{\text{SE}} \, \pi c #2 \cdot \sin a }
	{1+\sqrt{1-c^2} }  }
	

	%Taking the fraction as a mandatory argument:
\NewDocumentCommand{\fullterm} { 
	O {P_\text{mean}}%optional, defaults to P
	O {t}%optional, defaults to t
	m
} {
	\frac{#1 V_{\text{SE}} \, \pi c #2 \cdot \sin a }
	{1+\sqrt{1-c^2} } \cdot #3  }
\begin{document}
\[ \term \]
\[ \term[P] \]
\[ \term[P_\text{min}][2t] \]
Pay attention, if you want to leave out the P (default instead)
but want to change the t part.
\[ \term[t^3] \]
\[ \term[][t^2] \]
\begin{align*}
	W_c = \oint P \dd{V_c} &=%Have a look at package physics
	- \term\\
	&= - \term[P_\text{min}] \cdot
	\frac{\sqrt{1+c}}{\sqrt{1-c}}\\
	&= -\term[P_\text{max}] \cdot
	\frac{\sqrt{1-c}}{\sqrt{1+c}}
\end{align*}

What happens if we want to use our command in text? 
Try it out by deleting the comment sign %\term
Usually, having ensuremath in your document is discouraged.
Remember, you can still do $\term$. Please compare both outputs.

\NewDocumentCommand {\Pmin} {} {P_\text{min} }
\NewDocumentCommand {\Pmax} {} {P_\text{max} }
\NewDocumentCommand {\Pmean} {} {P_\text{mean} }


Using fullterm now:
\begin{align*}
	W_c = \oint P \dd{V_c} &=%Have a look at package physics
	- \term\\
	&= - \fullterm[\Pmin]{\frac{\sqrt{1+c}}{\sqrt{1-c}}}\\
	&= -\fullterm[\Pmax]{\frac{\sqrt{1-c}}{\sqrt{1+c}}}
\end{align*}


Comparing text and variables:
\begin{align*}
	P_{min}  & \Pmin \\%defined above 
	P_{mean} & \Pmean
\end{align*}
\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Post Reply