6

I want to write something like this: enter image description here

So far I tried with tikzcd, but I did not manage to give the arrows the right placement and length.

  • 4
    Welcome to TeX.SE! Could you please add a minimal example of what you tried so far? – CarLaTeX Feb 13 at 13:34
8

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

  • Thanks, I tried both ways. Since in the actual diagram I need, $A,B$ are quite long expressions, when using tikz, the arrows do not start at the same point. Is there a way to change this? – klirk Feb 13 at 14:35
  • @klirk Could you explain it further? Do you want to have the arrows aligned? A minimal working example is of great help here! – JouleV Feb 13 at 14:49
  • I want the arrow to be aligned yes. Alligned at the start and at the end. So their length will differ. – klirk Feb 13 at 15:25
  • @klirk Check my edited answer. – JouleV Feb 13 at 15:36
  • @CarLaTeX Thanks you (and marmot) very much ;-) In fact I have never learnt (or even attempted to learn) packages based on TikZ, like tikz-cd or circuitikz, but for a year I have been drawing many com­mu­ta­tive di­a­grams and electrical networks for my teacher (and in some answer on TeX.SE) quite well :) But maybe when I have some free-time I should start reading those packages' documentations, because it's time to do it ;-) – JouleV Feb 14 at 7:47
9

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

8

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

6

True, one does not need tikz-cd for that, but there is definitely nothing wrong with using it here.

\documentclass{article}
\usepackage{tikz-cd}
\begin{document}
\[
 \begin{tikzcd}[row sep=0.2em] 
  A & \arrow[l,"f" swap] B \arrow[r,"g"] & C \\
  f(x) & \arrow[l,mapsto] x \arrow[r,mapsto] & g(x) \\
 \end{tikzcd}
\]
\end{document}

enter image description here

6

The same as marmot's answer, but with different column alignment.

\documentclass{article}
\usepackage{tikz-cd}
\begin{document}
\[
 \begin{tikzcd}[row sep=.5ex,
 column sep=1.5em,
 /tikz/column 1/.append style={nodes={anchor=base east}},
 /tikz/column 3/.append style={nodes={anchor=base west}}] 
  A & \arrow[l,"f" swap] B \arrow[r,"g"] & C \\
  f(x) & \arrow[l,mapsto] x \arrow[r,mapsto] & g(x) \\
 \end{tikzcd}
\]
Or another alternative:
\[
 \begin{tikzcd}[row sep=.5ex,
 column sep=1.5em,
 /tikz/column 1/.append style={nodes={text width=width("$f(x)$"), text centered}},
 /tikz/column 3/.append style={nodes={text width=width("$g(x)$"), text centered}}] 
  A & \arrow[l,"f" swap] B \arrow[r,"g"] & C \\
  f(x) & \arrow[l,mapsto] x \arrow[r,mapsto] & g(x) \\
 \end{tikzcd}
\]
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.