6

I am looking to create a "table" or something similar in LaTeX where I show correspondence/mapping between a natural number and some other natural number, as indicated in the picture below (please excuse the badly drawn Paint example):

enter image description here

In other words "one maps onto zero, two maps onto one..." etc. I am, however, not sure how to do so in LaTeX.

Can anyone please provide me with some help as to how I can go about typesetting something similar to this in a LaTeX environment?

1
  • 2
    Use a tabular (text) or array (math) and $\updownarrow$. Feb 22, 2015 at 19:53

7 Answers 7

13

One option using a matrix; an array could also be used, but with matrix you don't have to specify the number and format of columns:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\[
\begin{matrix}
0 & 1 & 2 \\
\updownarrow & \updownarrow & \updownarrow  \\
2 & 3 & 1 
\end{matrix}
\]

\end{document}

enter image description here

As has been noted, the vertical space around the arrows seems a little uneven: there's more space above the arrows than below them, so one can easily fix it:

\documentclass{article}
\usepackage{amsmath}

\newcommand\UpDownarrow[1][0.2ex]{
  \raisebox{#1}[0pt][0pt]{$\updownarrow$}
}

\begin{document}
\[
\begin{matrix}
0 & 1 & 2 \\
\UpDownarrow & \UpDownarrow & \UpDownarrow \\
2 & 3 & 1 
\end{matrix}
\]

\end{document}

enter image description here

Just for the record, the array version:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\[
\begin{array}{ccc}
0 & 1 & 2 \\
\updownarrow & \updownarrow & \updownarrow  \\
2 & 3 & 1 
\end{array}
\]

\end{document}
3
  • ... except that the vertical space looks wrong: the space above the arrows seems larger than the space below the arrows. Or is it an illusion?
    – lhf
    Feb 23, 2015 at 11:09
  • @lhf No, it's not an illusion but it can be easily fixed, if desired. See my updated answer. Feb 23, 2015 at 14:52
  • @Dillon You're welcome. I updated my answer with an improved version that might be of interest for you. Feb 23, 2015 at 14:52
9

This might seem an overkill, but may be a good way of generalization.

\documentclass{scrartcl}
\usepackage{mathtools,tikz}
\usetikzlibrary{matrix}

\begin{document}
\[
  \begin{tikzpicture}
    \matrix[name=m,matrix of math nodes,column sep=1em,row sep=1em]
      {0 & 1 & 2 \\ 2 & 3 & 1 \\};
    \draw[<->] (m-1-1) -- (m-2-1);
    \draw[<->] (m-1-2) -- (m-2-2);
    \draw[<->] (m-1-3) -- (m-2-3);
  \end{tikzpicture}
\]
\end{document}

enter image description here

P.S.: If you load \usetikzlibrary{arrows.meta} it will probably give better arrowheads by default.

3
  • 1
    This is certainly not overkill. :). It's always good to share another possibility of solving a problem :)
    – user860374
    Feb 22, 2015 at 20:26
  • 1
    I meant because tikz is a huge package, but this will definitely provide a lot of flexibility.
    – Manuel
    Feb 22, 2015 at 20:27
  • 1
    I feel like this has better spacing somehow. …The top and bottom spaces seem to be more even than matrix answer. Feb 22, 2015 at 21:33
7

Yet another tikz solution, but here I use a \foreach loop:

\documentclass{article}
\usepackage{tikz}
\newcommand\aestrut{\rule[-0.5ex]{0pt}{2ex}}
\begin{document}

  \begin{tikzpicture}[my node/.style={anchor=base}]
    \foreach[count=\myi from 0] \mya/\myb in {0/1,1/2,2/3,h/g,e/j,j/k}
    {
      \node[my node] (A\myi) at (\myi,1) {\aestrut\mya};
      \node[my node] (B\myi) at (\myi,0) {\aestrut\myb};
      \draw[arrows=<->] (A\myi) -- (B\myi);
    }
  \end{tikzpicture}

\end{document}

enter image description here

It is a bit to set up, but once it's been set up it's very easy to add or remove pairs from the correspondence: just add (or remove) the pair from the in {.....} group where the pairs are separated by /.

5

Depending on your intended use, a TABstack may offer certain advantages.

\documentclass{article}
\usepackage{amsmath,tabstackengine,xcolor}
\stackMath
\begin{document}
While a TABstack can be used in a math environment
\[
\tabbedCenterstack[l]{0&1&2\\\updownarrow&\updownarrow&\updownarrow\\2&3&1}
\]
it can also be used inline: 
\tabbedCenterstack[l]{0&1&2\\\updownarrow&\updownarrow&\updownarrow\\2&3&1}
\\with differing vertical alignments
\tabbedLongstack[l]{0&1&2\\\updownarrow&\updownarrow&\updownarrow\\2&3&1} 
or 
\tabbedLongunderstack[l]{0&1&2\\\updownarrow&\updownarrow&\updownarrow\\2&3&1},\\
with different horizontal gap%
\setstacktabbedgap{1ex}
\tabbedCenterstack[l]{0&1&2\\\updownarrow&\updownarrow&\updownarrow\\2&3&1},
placed in a box 
\fbox{\,%
  \tabbedCenterstack[l]{0&1&2\\\updownarrow&\updownarrow&\updownarrow\\2&3&1}}\\
or saved%
\savestack\mybox{%
  \tabbedCenterstack[l]{0&1&2\\\updownarrow&\updownarrow&\updownarrow\\2&3&1}}
for repeated use: \mybox, \fbox{\,\mybox}, \mybox.\\
Or with annotation:
\tabbedCenterstack[l]{0&1&2\\\updownarrow&\updownarrow&\updownarrow\\%
  2\bllap{\color{red}\tiny\mbox{This is important}\uparrow}&3%
  &\brlap{\color{red}\uparrow\tiny\mbox{but not this}}1
}
\end{document}

enter image description here

2

A PSTricks solution:

\documentclass{article}

\usepackage{multido,pstricks}

\def\Map(#1)#2#3{%
  \psline{<->}(#1,0)(#1,1)
  \rput(#1,-0.3){#2}
  \rput(#1,1.3){#3}}

% parameter
\def\arrows{3}

\begin{document}

\begin{pspicture}(0.95,-0.4)(\arrows.05,1.4) % boundry found manually
  \multido{\iA = 0+1, \iB = 1+1}{\arrows}{\Map(\iB){$\iA$}{$\iB$}}
\end{pspicture}

\end{document}

output

All you have to do is choose the value of \arrows and the drawing will be adjusted accordingly.

1

One more solution with :

% arara: pdflatex

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

\begin{document} 
\begin{tikzcd}[column sep=1.7em, every arrow/.append style={<->}]
    1\arrow{d} & 2\arrow{d} & 3\arrow{d} \\
    0 & 1 & 2
\end{tikzcd}
\end{document}

enter image description here

1

A MetaPost way of doing this:

prologues := 3;
outputtemplate := "%j-%c.mps";

input latexmp; setupLaTeXMP(options = "12pt", textextlabel = enable, mode = rerun);

% t[] values on top, b[] values on bottom
% u spacing between columns, v arrows length
vardef corresp(suffix t, b)(expr u, v) =
    save i; numeric i; i = 1;
    forever:
        exitif (unknown t[i]) or (unknown b[i]);
        drawdblarrow ((i-1)*u, 0) -- ((i-1)*u, v);
        label.top("$" & decimal t[i] & "$", ((i-1)*u, v)); 
        label.bot("$" & decimal b[i] & "$", ((i-1)*u, 0));
        i := i+1;
    endfor
enddef; 

% The OP's example
beginfig(1);
    numeric u, v, t[], b[]; u = 0.75cm; v = 0.75cm; 
    for i = 1 upto 3: b[i] = i-1; t[i] = i; endfor
    corresp(t, b)(u, v);
endfig;

% A bigger example
beginfig(2);
    numeric u, v, t[], b[]; u = cm; v = cm; 
    for i = 1 upto 7: t[i] = i-1; b[i] = (i-1)**2; endfor
    corresp(t, b)(u, v);
endfig;

end.

Horizontal spacing u between numbers can be adjusted at will, as for the arrows length v. As many numbers as wished at the top and the bottom can be entered, respectively in the arrays t[] and b[]. The corresp macro takes care of the subsequent job.

The code is supposed to be compiled by MetaPost with LaTeX as typesetting engine.

mpost --tex=latex program.mp

Two figures are produced by this code: The first one is the OP's example, the second one is another (bigger) example.

enter image description here

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .