I'm having trouble defining a new command for latex. What I want to do? Define a \newcommand to define mappings like in this:

f : \begin{align*}
   & \mathbb{N} \to \mathbb{R} \\
   & x \mapsto f(x) 

My problem is that I tried using the align environment (since it doesn't require math mode) as follows:

{#1}\colon \begin{align*}%
  & {#2} \longrightarrow {#3}\\%
  & {#4} \longmapsto {#5}% 

Then trying to compile writing: \appli{f,\mathb{R},\mathbb{R},x,f(x)} and it doesn't work.

Following that I tried using the array environment as follows:

{#1}\colon \begin{array}{rcl}%
    & {#2} \longrightarrow {#3}\\%
    & {#4} \longmapsto {#5}%

And compiling writing: $\appli{f,\mathbb{R},\mathbb{R},x,f(x)}$. Still doesn't work. I also tried not writing \mathbb{} just R to see if it works and it still doesn't work.

What I get is a bunch of errors saying that I missed a } or a $ or that I'm not on math mode when compiling. Frankly I'm not an expert, I don't understand much of the errors but I'd like to define mappings easily.

Thank you very much for your help in advance.

  • do you really want the f: on a line of its own before the align environment?? Commented Sep 20, 2022 at 9:13
  • Nah I don't really mind. It could be a bonus tho.
    – Jueicon
    Commented Sep 20, 2022 at 9:18
  • 1
    sorry but before thinking about a command you should get the output right. align is meant to be a display environment on its own. Check the amsmath documentation if you don't need alignat or something like that instead. Beside this: arguments in TeX are given braces and not as comma lists, so you to use your command you should do something like \appli{f}{\mathbb{R}}{\mathbb{R}}... Commented Sep 20, 2022 at 9:29
  • OK thanks a lot! I'll try it out right now!
    – Jueicon
    Commented Sep 20, 2022 at 9:32

2 Answers 2


With the \NewDocumentCommand from xparse, it works:

    \usepackage{amsmath, amsfonts}
    \[ {#1}\colon \begin{aligned}[t]%
      {#2} & \longrightarrow {#3}\\%
     {#4} & \longmapsto {#5}%
    \end{aligned} \]}




enter image description here


A hundred thanks to Ulrike Fischer and Bernard for their answers!

The main problem on what I was writing was, what Ulrike pointed out, that "arguments in TeX are given braces and not as comma lists" meaning that when I wrote \appli{f,\mathb{R},\mathbb{R},x,f(x)} compiling couldn't work. A solution is to write instead \appli{f}{\mathb{R}}{\mathbb{R}}{x}{f(x)}!

Having said all that I was still tingly about the align environment since I wanted to define a command that could still be written in the middle of a sentence and that defined a map with the name of the map at line height so i tried keeping the array environment and what I got is this:

{#2} & \longrightarrow & {#3}\\%
{#4} & \longmapsto & {#5}%


Writing the \newcommand this way I get what I wanted :)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .