# Drawing a mapping diagram showing a relation between two sets?

The following diagram is given: I tried to draw it using table environment, but could not insert the skew arrows properly! Customizing this useful answer, I reached to:

 \documentclass[a4paper,12pt]{book}
\usepackage{tikz}
\usepackage{graphicx}
\usetikzlibrary{positioning}
\begin{document}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
[
mydot/.style={circle, fill, inner sep=1pt }, >=latex, shorten >= 3pt, shorten <= 3pt
]
\node[mydot,label={left:8}] (a1) {};
\node[mydot,below=of a1,label={left:9}] (a2) {};
\node[mydot,below=of a2,label={left:10}] (a3) {};

\node[mydot,right=1cm of a1,label={right:0}] (b1) {};
\node[mydot,below=of b1,label={right:1}] (b2) {};
\node[mydot,below=of b2,label={right:2}] (b3) {};
\node[mydot,below=of b3,label={right:3}] (b4) {};
\path[->] (a1) edge (b1);
\path[->] (a2) edge (b2) edge (b3);
\path[->] (a3) edge (b4);
\end{tikzpicture}
\end{figure}
\end{document}


Can I somehow insert the title: Domain and Range inside above MWE or I should work on another way? In this MWE the nodes are not close to each other as it is expected to be.

Thanks for the time!

• How to add arrow in equations and matrix? might help. Jan 31 at 16:12
• You should write a table (with tabularray package, for example) and insert those arrows in it, for example using tikzmark. Jan 31 at 16:15

See How to add arrow in equations and matrix? for a description of how it works. \documentclass{article}
\usepackage{booktabs}
\usepackage{tikz}
\newcommand\tikznode[]{%
\tikz[remember picture,baseline=(#2.base)]
\node[minimum size=0pt,inner sep=0pt,#1](#2){#3};%
}
\begin{document}
\begin{tabular}{cc}
\toprule
Domain & Range \\
\midrule
\tikznode{n8}{8}   & \tikznode{n0}{0}\\
\tikznode{n9}{9}   & \tikznode{n1}{1}\\
& \tikznode{n2}{2}\\
\tikznode{n10}{10} & \tikznode{n3}{3}\\
\bottomrule
\end{tabular}
\begin{tikzpicture}[remember picture,overlay,
shorten <=2pt,shorten >=2pt,>=stealth]
\draw[->] (n8) -- (n0);
\draw[->] (n9) -- (n1);
\draw[->] (n9) -- (n2);
\draw[->] (n10) -- (n3);
\end{tikzpicture}
\end{document}

• You are playing with the codes as a child plays with playdough! Thanks Jan 31 at 16:36

\documentclass{article}
\usepackage{amsmath,tikz-cd}

\begin{document}

\begin{tikzcd}[row sep=0ex]
\hline
\text{Domain} & \text{Range} \\
\hline
8 \ar[r] & 0 \\
9 \ar[r] \ar[rd] & 1 \\
& 2 \\
10 \ar[r] & 3 \\
\hline
\end{tikzcd}

\end{document} • Thanks friend for the time. Jan 31 at 17:35

Here is an alternative with nicematrix. \documentclass{article}

\usepackage{nicematrix, tikz}

\begin{document}

\begin{NiceTabular}{cc}
\hline
Domain & Range\\ \hline
8 & 0\\
9 & 1\\
& 2\\
10 & 3\\ \hline
\CodeAfter
\tikz{
\draw[-stealth](2-1)--(2-2);
\draw[-stealth](3-1)--(3-2);
\draw[-stealth](3-1)--(4-2);
\draw[-stealth](5-1)--(5-2);
}
\end{NiceTabular}

\end{document}


You can improve the spacing by using the booktabs commands \toprule, \bottomrule and \midrule. \documentclass{article}

\usepackage{nicematrix, booktabs, tikz}

\begin{document}

\begin{NiceTabular}{cc}
\toprule
Domain & Range\\ \midrule
8 & 0\\
9 & 1\\
& 2\\
10 & 3\\ \bottomrule
\CodeAfter
\tikz{
\draw[-stealth](2-1)--(2-2);
\draw[-stealth](3-1)--(3-2);
\draw[-stealth](3-1)--(4-2);
\draw[-stealth](5-1)--(5-2);
}
\end{NiceTabular}

\end{document}

• Thanks so much for the time! I got more than I have expected. Jan 31 at 17:34

Yet another Tikz solution with matrix:

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
\matrix (A) [matrix of nodes]
{ \hline
Domain & Range \\
\hline
8 & 0 \\
9 & 1 \\
& 2 \\
10 & 3 \\
\hline \\
};
\draw[->] (A-2-1) -- (A-2-2);
\draw[->] (A-3-1) -- (A-3-2);
\draw[->] (A-3-1) -- (A-4-2);
\draw[->] (A-5-1) -- (A-5-2);
\end{tikzpicture}
\end{document} • Thanks for every second you pointed out other aspects of using the Matrix environment! Feb 6 at 19:41