How to define multiple functions in the same diagram?

Solution 1

I would do

\documentclass{article}
\usepackage{amsmath} % \xleftarrow, \xrightarrow
\usepackage{stmaryrd} % \mapsfrom
\begin{document}
\[
\begin{array}{ccccc}
    A & \xleftarrow{f} & B & \xrightarrow{g} & C\\
    f(x) & \mapsfrom & x & \mapsto & g(x)
\end{array}
\]
\end{document}

enter image description here

You can improve this solution by using Segletes' awesome answer.


Solution 2 (better IMHO)

However, one can have another solution using TikZ. Pros: you can change the size of the arrow; Cons: the code is longer.

\documentclass[tikz,margin=3mm]{standalone}
\usetikzlibrary{decorations.markings,positioning}
\begin{document}
\begin{tikzpicture}[decoration={
    markings,
    mark={at position 0 with {\arrow{|}}}}
]
\node (B) {$B$};
\node [left=1.5cm of B.west] (A) {$A$};
\node [right=1.5cm of B.east] (C) {$C$};
\node [below=0.5cm of B.south] (x) {$x$};
\node [left=1.5cm of x.west] (f) {$f(x)$};
\node [right=1.5cm of x.east] (g) {$g(x)$};
\draw[postaction={decorate},->] (x)--(f);
\draw[postaction={decorate},->] (x)--(g);
\draw[->] (B)--(A) node[midway,above] {$f$};
\draw[->] (B)--(C) node[midway,above] {$g$};
\end{tikzpicture}
\end{document}

enter image description here

Edit 1 for Solution #2

Do you mean something like this?

\documentclass[tikz,margin=3mm]{standalone}
\usetikzlibrary{decorations.markings,positioning}
\begin{document}
\begin{tikzpicture}[
    decoration={
        markings,
        mark={at position 0 with {\arrow{|}}}},
    every node/.style={rectangle,minimum height=.75cm}
]
\node (B) {$d(M,N)=\sqrt{(x_M-x_N)^2+(y_M-y_N)^2}$};
\node [left=1.5cm of B.west] (A) {$a^2+b^2=c^2$};
\node [right=1.5cm of B.east] (C) {$E=mc^2$};
\node [below=0.5cm of B.south] (x) {$x$};
\node [below left=0.5cm and 0pt of A.south east] (f) {$f(x)$};
\node [below right=0.5cm and 0pt of C.south west] (g) {$g(x)$};
\draw[postaction={decorate},->] (x)--(f);
\draw[postaction={decorate},->] (x)--(g);
\draw[->] (B)--(A) node[midway,above=-1ex] {\scriptsize $f$};
\draw[->] (B)--(C) node[midway,above=-1ex] {\scriptsize $g$};
\end{tikzpicture}
\end{document}

enter image description here

Edit 2 for Solution #2

This is even better IMHO

\documentclass[tikz,margin=3mm]{standalone}
\usetikzlibrary{decorations.markings,positioning}
\begin{document}
\begin{tikzpicture}[
    decoration={
        markings,
        mark={at position 0 with {\arrow{|}}}
    },
    every node/.style={rectangle,minimum height=.75cm}
]
\node (B) {$d(M,N)=\sqrt{(x_M-x_N)^2+(y_M-y_N)^2}$};
\node [left=1.5cm of B.west] (A) {$a^2+b^2=c^2$};
\node [right=1.5cm of B.east] (C) {$E=mc^2$};
\node [below=0.5cm of B.south] (x) {$x$};
\node [below left=0.5cm and 0pt of A.south east] (f) {$f(x)$};
\node [below right=0.5cm and 0pt of C.south west] (g) {$g(x)$};
\node [below right=0.5cm and 0pt of B.south west] (xl) {};
\node [below left=0.5cm and 0pt of B.south east] (xr) {};
\draw[postaction={decorate},->] (xl)--(f);
\draw[postaction={decorate},->] (xr)--(g);
\draw[->] (B)--(A) node[midway,above=-1ex] {\scriptsize $f$};
\draw[->] (B)--(C) node[midway,above=-1ex] {\scriptsize $g$};
\end{tikzpicture}
\end{document}

enter image description here


You can use tabstackengine to get the proper math spacings and align the operators.

\documentclass{article}
\usepackage{amsmath} % \xleftarrow, \xrightarrow
\usepackage{stmaryrd} % \mapsfrom
\usepackage{tabstackengine}
\TABstackMath
\setstacktabulargap{0pt}
\TABbinary
\begin{document}
\[
\tabularCenterstack{rrcll}{
    A & \xleftarrow{f} & B & \xrightarrow{g} & C\\
    f(x) & \mapsfrom & x & \mapsto & g(x)
}
\]
\end{document}

enter image description here

If vertical alignment of the relational operators is not a requirement, a simple stack will do:

\documentclass{article}
\usepackage{amsmath} % \xleftarrow, \xrightarrow
\usepackage{stmaryrd} % \mapsfrom
\usepackage{tabstackengine}
\stackMath
\begin{document}
\[
\Centerstack{
    A \xleftarrow{f} B \xrightarrow{g} C\\
    f(x) \mapsfrom x \mapsto g(x)
}
\]
\end{document}

enter image description here


You don't really need tikz-cd for that – a simple alignat will do:

\documentclass{article}

\usepackage{amsmath, stmaryrd}
\usepackage{makebox} 

\begin{document}

\begin{alignat*}{2}
A & \xleftarrow{\makebox[10pt]{$\scriptstyle f$}} B & & \xrightarrow{\makebox[10pt]{$\scriptstyle g$}} C \\
f(x) & \longmapsfrom \makebox*{$B$}{$x$} & & \longmapsto g(x)
\end{alignat*}

\end{document} 

enter image description here