0

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

\documentclass[english]{scrreprt}
\usepackage{amsmath}
\usepackage{amssymb}
\begin{document}
f : \begin{align*}
   & \mathbb{N} \to \mathbb{R} \\
   & x \mapsto f(x) 
\end{align*}}
\end{document}

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

\newcommand{\appli}[5]{%
{#1}\colon \begin{align*}%
  & {#2} \longrightarrow {#3}\\%
  & {#4} \longmapsto {#5}% 
\end{align*}}

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:

\newcommand{\appli}[5]{%
{#1}\colon \begin{array}{rcl}%
    & {#2} \longrightarrow {#3}\\%
    & {#4} \longmapsto {#5}%
\end{array}}

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.

4
  • 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

1

With the \NewDocumentCommand from xparse, it works:

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

        \begin{document}

    \appli{f}{\mathbb{N}}{\mathbb{R}}{x}{f(x)}

        \end{document} 

enter image description here

0

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:

\newcommand{\appli}[5]{%
${#1}$:$\begin{array}{rcl}%
{#2} & \longrightarrow & {#3}\\%
{#4} & \longmapsto & {#5}%
\end{array}$%
}

\appli{u}{\mathbb{N}}{\mathbb{R}}{n}{u(n)}\newline

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 .