Derivatives and partial derivatives in LaTeX (frac, partial)
A derivative is a fraction: \frac{dy}{dx}. A partial derivative uses \partial: \frac{\partial f}{\partial x}. The short forms are a prime, f'(x), and a dot, \dot{x}.
Preamble
The snippets on this page need these lines before \begin{document}:
\usepackage{amsmath}Derivative as a fraction
Numerator and denominator are normal letters. For a higher derivative the exponent goes on the d in the numerator and on the variable in the denominator.
\[
\frac{dy}{dx}
\qquad
\frac{d}{dx} \sin x = \cos x
\qquad
\frac{d^2 y}{dx^2}
\]Change the formula
The preview is drawn in your browser by KaTeX, which knows the common math commands. The editor compiles the formula with pdfTeX.
Partial derivatives
\partial prints the rounded d. \nabla prints the nabla operator for a gradient.
\[
\frac{\partial f}{\partial x}
\qquad
\frac{\partial^2 f}{\partial x \, \partial y}
\qquad
\nabla f = \left( \frac{\partial f}{\partial x}, \frac{\partial f}{\partial y} \right)
\]Change the formula
The preview is drawn in your browser by KaTeX, which knows the common math commands. The editor compiles the formula with pdfTeX.
Primes and dots
An apostrophe after the function name prints a prime. \dot{x} and \ddot{x} put one or two dots over a letter, the usual form for time derivatives; amsmath adds \dddot and \ddddot.
\[
f'(x)
\qquad
f''(x)
\qquad
f^{(n)}(x)
\qquad
\dot{x}
\qquad
\ddot{x}
\]Change the formula
The preview is drawn in your browser by KaTeX, which knows the common math commands. The editor compiles the formula with pdfTeX.
Define a derivative command
\newcommand{\deriv}[2]{...} defines a command with two arguments, #1 for the function and #2 for the variable. The example also sets the d upright with \mathrm{d}; change it in one place to switch the whole document.
\newcommand{\dd}{\mathrm{d}}
\newcommand{\deriv}[2]{\frac{\dd #1}{\dd #2}}
\[
\deriv{y}{x}
\qquad
\deriv{}{t} \, e^{2t} = 2 e^{2t}
\]Ready-made commands from the physics package
\dv{y}{x} from the physics package writes an ordinary derivative and \pdv{f}{x} a partial one. An optional argument sets the order, and \pdv{f}{x}{y} writes a mixed partial derivative.
Complete document:
\documentclass{article}
\usepackage{physics}
\begin{document}
\[
\dv{y}{x}
\qquad
\dv[2]{y}{x}
\qquad
\pdv{f}{x}
\qquad
\pdv{f}{x}{y}
\]
\end{document}Pitfalls
physicssets the d upright. The package optionitalicdiffmakes it italic.physicsdefines a command\qtyof its own. Whensiunitxis loaded as well,\qty{5}{\metre}no longer prints a quantity; see Units and numbers in LaTeX with siunitx.- For a derivative evaluated at a point, with a tall bar and a subscript, see Large brackets in LaTeX.
Compiled with pdfLaTeX from TeX Live 2023.
Related pages
- Package
physics - Package
amsmath - How-to Fractions in LaTeX
- How-to Integrals in LaTeX
- How-to Define your own commands in LaTeX