Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am tired of writing \frac{\partial something}{\partial something_else}. Can I automate it by defining some sort of shortcut to it, so that when I write \p(x)(y) it will transform to \frac{\partial x}{\partial y}?

share|improve this question
    
Welcome to TeX.sx! A tip: You can use backticks ` to mark your inline code as I did in my edit. –  egreg Jan 7 '13 at 11:52
    
@egreg Thanks ) I also think that my tags are not correct, so it would be great if somebody tagged it appropriately. –  Sunny88 Jan 7 '13 at 11:53
2  
With regards to (partial) derivatives, this question could be of interest: tex.stackexchange.com/questions/22076/… –  Torbjørn T. Jan 7 '13 at 12:17
add comment

3 Answers 3

up vote 20 down vote accepted
\documentclass{article}

\usepackage{amsmath}

\newcommand\p[2]{\frac{\partial #1}{\partial #2}}

\begin{document}
    $ \p{x}{y} $
\end{document}
share|improve this answer
    
Thanks, it works. –  Sunny88 Jan 7 '13 at 11:58
add comment

The ability to define complex macros is one of the strengths of LaTeX. The traditional LaTeX way would be to use \p{x}{y} in the text instead of \p(x)(y). If you are happy with \p{x}{y}, then you can just add

\newcommand{\p}[2]{\frac{\partial#1}{\partial#2}}

to your preamble. Since this new macro is requires mathmode, you may also want to wrap the macro in an \ensuremath so something like

\newcommand{\p}[2]{\ensuremath{\frac{\partial#1}{\partial#2}}}

If you really want \p(x)(y) then you can add

\def\p(#1)(#2){\ensuremath{\frac{\partial #1}{\partial #2}}}

to your preamble. This isn't particularly robust in that something like $\p((a+b)(a-b))(a+b)$ will break it. There are ways to make it more robust including LaTeX3, however, I would advise against using \p(x)(y) unless there is a good reason (especially as a beginner).

One final note is that it is probably better to use a slightly longer and more descriptive names for macros. While \p seems good now, in a few months it likely will not mean anything to you. Short macro names also increase the chances you might run into a name clash with some other package.

share|improve this answer
4  
The \def-variant with the parentheses can break easily if the arguments uses parentheses too. Try e.g. $\p((a+b)(a-b))(a+b)$. I wouldn't recommend it. –  Ulrike Fischer Jan 7 '13 at 13:10
    
@UlrikeFischer \usepackage{xparse} and \NewDocumentCommand{\p}{r()r()}{\frac{\partial #1}{\partial #2}} than $ \p((a+b)(a-b))(a+b) $ (Though, I find the usage of () very unnatural and I wouldn’t do it here either.) –  Qrrbrbirlbel Jan 7 '13 at 14:07
    
I think it would be nice if you explained in your answer why it is a good idea to use ensuremath. –  Vivi Jan 7 '13 at 17:26
    
@Vivi good suggestion. I edited the answer. –  StrongBad Jan 8 '13 at 12:16
add comment

The commath package has a lot of these handy macros already defined, like \pd[2]{f}{x}. I happen to use it, but it also has some nasty issues, like incorrect detection of inline vs. display math, and a conflict with TikZ over the colon character (see Why does pdfTeX hang on this file?).

share|improve this answer
2  
The \pd macro, as you surely have noticed, doesn't work as expected. The problem is that \ifinner isn't true or false where the author of commath thinks it should. Thus using commath can cause many more problems than it solves. –  egreg Jan 7 '13 at 14:03
    
I have to agree with @egreg that the package is not well-made. I don't see such inner details as he does, but I see some problem of concept that make the package bad IMHO (e.g. defining many 2- and 3-letter macros). –  tohecz Jan 7 '13 at 14:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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