10

The following diagram is given:

enter image description here

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[-&gt;] (a1) edge (b1);
\path[-&gt;] (a2) edge (b2) edge (b3);
\path[-&gt;] (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!

Mikasa
  • 589

4 Answers4

11

See How to add arrow in equations and matrix? for a description of how it works.

enter image description here

\documentclass{article}
\usepackage{booktabs}
\usepackage{tikz}
\newcommand\tikznode[3][]{%
  \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}
gernot
  • 49,614
7

A simple answer with tikz-cd.

\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}

enter image description here

mbert
  • 4,171
6

Here is an alternative with nicematrix.

enter image description here

\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-2); \draw-stealth--(3-2); \draw-stealth--(4-2); \draw-stealth--(5-2); }
\end{NiceTabular}

\end{document}

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

enter image description here

\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-2); \draw-stealth--(3-2); \draw-stealth--(4-2); \draw-stealth--(5-2); }
\end{NiceTabular}

\end{document}

Sandy G
  • 42,558
2

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}

enter image description here

Andrey L.
  • 1,794