Tx · LaTeX & TikZ

How to Define and Typeset Your Own Function in LaTeX

Published By the 9to5Science editorial team

At a glance

  1. 1. Choose the goal

    Use mathematical notation to define a function, or define a LaTeX command to reuse its notation.

  2. 2. Use proper spacing

    Typeset named functions as operators, not as ordinary italic variables.

  3. 3. Define a macro

    Use newcommand for reusable notation and pass the input as an argument.

  4. 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 f(x)=x2f(x)=x^2, 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:

f ⁣:R→R,f(x)=x2.f\colon\mathbb{R}\to\mathbb{R},\qquad f(x)=x^2.

Here ff is the function name, and xx is its input. The expression f(x)=x2f(x)=x^2 gives the rule. The notation f ⁣:R→Rf\colon\mathbb{R}\to\mathbb{R} 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 x↦x2x\mapsto x^2.

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 ff.

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 F(x)F(x), 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 a+ba+b. 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 FF 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 ff, keep it as $f(x)$: a single-letter function name is conventionally italic, so \DeclareMathOperator is not appropriate for it.

Common mistakes

  • Using \newcommand when you mean a mathematical definition. A macro is a typesetting shortcut. State the function’s rule separately, for example with f(x)=x2f(x)=x^2.
  • Writing a multi-letter operator in ordinary math italics. Use \DeclareMathOperator or \operatorname for names such as rank or myfun.
  • Forgetting the argument marker. In a one-argument macro, use #1 in the definition and provide one argument each time you call it.
  • Defining a command more than once. \newcommand reports an error if the command already exists. Choose a less common command name, or use \renewcommand only when you intentionally want to replace an existing definition.
  • Adding a macro for a single simple expression. If you write f(x)f(x) 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

  1. The amsmath package documentation

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.