How to Define and Typeset Your Own Function in LaTeX
At a glance
- 1. Choose the goal
Use mathematical notation to define a function, or define a LaTeX command to reuse its notation.
- 2. Use proper spacing
Typeset named functions as operators, not as ordinary italic variables.
- 3. Define a macro
Use newcommand for reusable notation and pass the input as an argument.
- 4. Check the output
Compile a minimal example to confirm spacing and symbols look right.
“Define your own function” can mean two different things in LaTeX: state a mathematical function such as , or create a shortcut that typesets a particular function consistently. The first is ordinary mathematical notation; the second uses a LaTeX macro. In either case, the function name and its argument should have appropriate mathematical spacing.
Write the mathematical definition
Use the function notation that communicates the domain, codomain, and rule. For example:
Here is the function name, and is its input. The expression gives the rule. The notation says that the function maps real numbers to real numbers. If you want to show how an input maps to an output, you can also write .
A complete LaTeX example is:
\documentclass{article}
\usepackage{amsmath,amssymb}
\begin{document}
Let
\[
f\colon\mathbb{R}\to\mathbb{R},
\qquad f(x)=x^2.
\]
Thus, $x\mapsto x^2$ describes the rule for $f$.
\end{document}
The \colon command is provided by standard LaTeX and gives the colon spacing commonly used for function definitions. amssymb provides \mathbb{R}. You do not need a custom command just to write a function named .
Make a reusable function command
If you repeatedly use a named function, define a command that takes its input as an argument. For instance, if you want to call a function \myfun and have it display as , write:
\documentclass{article}
\usepackage{amsmath}
\newcommand{\myfun}[1]{F\left(#1\right)}
\begin{document}
The function is $\myfun{x}=x^2$.
For another input, $\myfun{a+b}$ is also available.
\end{document}
The [1] tells \newcommand that \myfun takes one argument. In the definition, #1 stands for the argument supplied when you use the command. \left( and \right) make the parentheses grow to fit larger inputs such as . For a simple argument, ordinary parentheses are often sufficient: \newcommand{\myfun}[1]{F(#1)}.
This macro only controls how the expression is typeset. It does not define a mathematical function by itself: the rule or meaning of still needs to be stated in the text or equations.
Typeset named functions as operators
Names such as rank, tr, or sech are usually upright, with spacing like other mathematical operators. Do not define a multi-letter function name as plain italic letters: that makes it look like a product of separate variables and can give incorrect spacing.
For a function name you use throughout a document, amsmath provides \DeclareMathOperator:
\documentclass{article}
\usepackage{amsmath}
\DeclareMathOperator{\myfun}{myfun}
\begin{document}
$\myfun(x)=x^2$ and $\myfun(a+b)=c$.
\end{document}
This produces an upright operator name with standard operator spacing. Use \DeclareMathOperator* instead if you want the operator to support limits above and below it in display math, as \lim does. For a one-off name, use \operatorname{myfun}(x) rather than creating a document-wide command.
If the intended function is simply named , keep it as $f(x)$: a single-letter function name is conventionally italic, so \DeclareMathOperator is not appropriate for it.
Common mistakes
- Using
\newcommandwhen you mean a mathematical definition. A macro is a typesetting shortcut. State the function’s rule separately, for example with . - Writing a multi-letter operator in ordinary math italics. Use
\DeclareMathOperatoror\operatornamefor names such asrankormyfun. - Forgetting the argument marker. In a one-argument macro, use
#1in the definition and provide one argument each time you call it. - Defining a command more than once.
\newcommandreports an error if the command already exists. Choose a less common command name, or use\renewcommandonly when you intentionally want to replace an existing definition. - Adding a macro for a single simple expression. If you write only once or twice, ordinary math notation is clearer.
Use standard function notation for the mathematical object, and reserve custom commands for notation you repeat or want to keep consistent throughout your document.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.