Drawing integer arrays

Replace the contents of the \el-command with a TikZ-node! This way you can determine a fixed minimum size.

\documentclass{beamer}
\usepackage{tikz}
\usepackage{color}                                                                                                                                
\newcommand{\vet}[1]{\foreach \num in {#1}{\el{\num}}}
\newcommand{\el}[1]{\tikz{\node[font=\footnotesize, minimum size=16pt, draw]{#1}}}
\newcommand{\bl}{\color{blue}}
\newcommand{\gr}{\color{gray}}
\newcommand{\re}{\color{red}}

\begin{document}
\begin{frame}[fragile]\frametitle{Merge}
\begin{tabular}{cc}
copy value from aux & into sorted array\\
\vet{\re $1^i$,\re 3,\re 5,\re 6, \bl $0^j$,\bl 2,\bl 4,\bl 9,\bl 10}  & 
\vet{\bl 0, \#, \#, \#, \#, \#, \#, \#,\#}\\ \\
\vet{\re $1^i$,\re 3,\re 5,\re 6, \gr 0, \bl $2^j$,\bl 4,\bl 9,\bl 10} &
\vet{\gr 0, \re 1, \#, \#, \#, \#, \#, \#,\#}\\
\vet{\gr 1, \re $3^i$,\re 5,\re 6, \gr 0, \bl $2^j$,\bl 4,\bl 9,\bl 10} &
\vet{\gr 0, \gr 1, \bl 2, \#, \#, \#, \#, \#,\#}\\
\vet{\gr 1, \re $3^i$,\re 5,\re 6, \gr 0, \gr 2,\bl $4^j$,\bl 9,\bl 10} &
\vet{\gr 0, \gr 1, \gr 2, \re 3, \#, \#, \#, \#,\#}\\
\vet{\gr 1, \gr 3,\re $5^i$,\re 6, \gr 0, \gr 2,\bl $4^j$,\bl 9,\bl 10} &
\vet{\gr 0, \gr 1, \gr 2, \gr 3, \bl 4, \#, \#, \#,\#}\\
\vet{\gr 1, \gr 3,\re $5^i$,\re 6, \gr 0, \gr 2,\gr 4,\bl $9^j$,\bl 10} &
\vet{\gr 0, \gr 1, \gr 2, \gr 3, \gr 4, \re 5, \#, \#,\#}\\
\vet{\gr 1, \gr 3,\gr 5,\re $6^i$,\gr 0, \gr 2,\gr 4,\bl $9^j$,\bl 10} &
\vet{\gr 0, \gr 1, \gr 2, \gr 3, \gr 4, \gr 5, \re 6,\#, \#}\\
\vet{\gr 1, \gr 3,\gr 5,\gr $6^i$,\gr 0, \gr 2,\gr 4,\bl $9^j$,\bl 10} &
\vet{\gr 0, \gr 1, \gr 2, \gr 3, \gr 4, \gr 5, \gr 6, \bl 9,\bl 10}\\
\end{tabular}
\end{frame}
\end{document}

enter image description here


I'm sure this can also be done with TikZ.

You can add an optional argument to integerarrays to state a different size of the cells (default 1.5em).

\documentclass{article}
\usepackage{xparse}
\usepackage{xcolor}

\ExplSyntaxOn
% user level commands
\NewDocumentEnvironment{integerarrays}{O{1.5em}mm+b}
 {
  \renewcommand{\arraystretch}{0}
  \dim_set:Nn \l_kees_array_width_dim { #1 }
  \kees_array_table:nnn { #2 } { #3 } { #4 }
 }{}

\NewDocumentCommand{\vet}{O{}m}
 {
  \group_begin:
  \dim_compare:nT { \l_kees_array_width_dim = 0pt }
   {% we're inline
    \dim_set:Nn \l_kees_array_width_dim { 1.5em }
   }
  \keys_set:nn { kees/array } { #1 }
  \kees_array_vet:n { #2 }
  \group_end:
 }

% keys
\keys_define:nn { kees/array }
 {
  width .dim_set:N = \l_kees_array_width_dim,
  font  .tl_set:N = \l_kees_array_font_tl,
 } 

% variables
%\dim_new:N \l_kees_array_width_dim
\seq_new:N \l_kees_array_body_seq
\seq_new:N \l_kees_array_vet_in_seq
\seq_new:N \l_kees_array_vet_out_seq

% internal functions
\cs_new_protected:Nn \kees_array_table:nnn
 {
  % split the body at \\
  \seq_set_split:Nnn \l_kees_array_body_seq { \\ } { #3 }
  % check for a trailing \\
  \seq_pop_right:NN \l_kees_array_body_seq \l__kees_array_last_tl
  \tl_if_blank:VF \l__kees_array_last_tl
   {% there was no trailing \\, reinstate the last item
    \seq_put_right:NV \l_kees_array_body_seq \l__kees_array_last_tl
   }
  \begin{tabular}{@{}cc@{}}
  #1 & #2 \\[1ex]
  % use the body with a bit of backing up to hide the double rule
  \seq_use:Nn \l_kees_array_body_seq { \\[-\fboxrule] }
  \end{tabular}
 }

\cs_new_protected:Nn \kees_array_vet:n
 {
  % split the input at commas
  \seq_set_split:Nnn \l_kees_array_vet_in_seq { , } { #1 }
  % make each item the argument to the boxing macro
  \seq_set_map:NNn \l_kees_array_vet_out_seq \l_kees_array_vet_in_seq
   {
    \kees_array_box:n { \exp_not:n { ##1 } }
   }
  % use the sequence backing up a bit to hide the double rule
  \seq_use:Nn \l_kees_array_vet_out_seq { \hspace{-\fboxrule} }
 }

\cs_new_protected:Nn \kees_array_box:n
 {
  \group_begin:
  \setlength{\fboxsep}{0pt}
  \framebox[\l_kees_array_width_dim]
   {
    \l_kees_array_font_tl
    \rule[-0.3\l_kees_array_width_dim]{0pt}{\l_kees_array_width_dim}$#1$
   }
  \group_end:
 }
\ExplSyntaxOff

\newcommand{\bl}{\color{blue}}
\newcommand{\gr}{\color{gray}}
\newcommand{\re}{\color{red}}

\begin{document}

\begin{integerarrays}{copy value from aux}{into sorted array}
\vet{\re 1^i,\re 3,\re 5,\re 6, \bl 0^j,\bl 2,\bl 4,\bl 9,\bl 10}  & 
\vet{\bl 0, \#, \#, \#, \#, \#, \#, \#,\#}\\
\vet{\re 1^i,\re 3,\re 5,\re 6, \gr 0, \bl 2^j,\bl 4,\bl 9,\bl 10} &
\vet{\gr 0, \re 1, \#, \#, \#, \#, \#, \#,\#}\\
\vet{\gr 1, \re 3^i,\re 5,\re 6, \gr 0, \bl 2^j,\bl 4,\bl 9,\bl 10} &
\vet{\gr 0, \gr 1, \bl 2, \#, \#, \#, \#, \#,\#}\\
\vet{\gr 1, \re 3^i,\re 5,\re 6, \gr 0, \gr 2,\bl 4^j,\bl 9,\bl 10} &
\vet{\gr 0, \gr 1, \gr 2, \re 3, \#, \#, \#, \#,\#}\\
\vet{\gr 1, \gr 3,\re 5^i,\re 6, \gr 0, \gr 2,\bl 4^j,\bl 9,\bl 10} &
\vet{\gr 0, \gr 1, \gr 2, \gr 3, \bl 4, \#, \#, \#,\#}\\
\vet{\gr 1, \gr 3,\re 5^i,\re 6, \gr 0, \gr 2,\gr 4,\bl 9^j,\bl 10} &
\vet{\gr 0, \gr 1, \gr 2, \gr 3, \gr 4, \re 5, \#, \#,\#}\\
\vet{\gr 1, \gr 3,\gr 5,\re 6^i,\gr 0, \gr 2,\gr 4,\bl 9^j,\bl 10} &
\vet{\gr 0, \gr 1, \gr 2, \gr 3, \gr 4, \gr 5, \re 6,\#, \#}\\
\vet{\gr 1, \gr 3,\gr 5,\gr 6^i,\gr 0, \gr 2,\gr 4,\bl 9^j,\bl 10} &
\vet{\gr 0, \gr 1, \gr 2, \gr 3, \gr 4, \gr 5, \gr 6, \bl 9,\bl 10}
\end{integerarrays}

\bigskip

\begin{integerarrays}[2em]{copy value from aux}{into sorted array}
\vet{\re 1^i,\re 3,\re 5,\re 6, \bl 0^j,\bl 2,\bl 4,\bl 9,\bl 10}  & 
\vet{\bl 0, \#, \#, \#, \#, \#, \#, \#,\#}\\
\vet{\re 1^i,\re 3,\re 5,\re 6, \gr 0, \bl 2^j,\bl 4,\bl 9,\bl 10} &
\vet{\gr 0, \re 1, \#, \#, \#, \#, \#, \#,\#}\\
\end{integerarrays}

\bigskip

\vet{1,2,3,4,5}

\bigskip

\vet[width=1em,font=\scriptsize]{\re 1^i,\re 3,\re 5,\re 6, \bl 0^j,\bl 2,\bl 4,\bl 9,\bl 10}

\end{document}

enter image description here


Like this:

enter image description here

\documentclass{beamer}
\usepackage{tikz}
\usepackage{color}
\newcommand{\vet}[1]{\foreach \num in {#1}{\el{\num}}}
\newcommand{\el}[1]{\fbox{\parbox[t][2ex][c]{1.1em}{#1}}}
\newcommand{\bl}{\color{blue}}
\newcommand{\gr}{\color{gray}}
\newcommand{\re}{\color{red}}

\begin{document}
\begin{frame}[fragile]
\frametitle{Merge}
    \footnotesize
    \setlength\tabcolsep{3pt}
    \renewcommand\arraystretch{0.9}
\begin{tabular}{@{} ll @{}}
copy value from aux & into sorted array\\
\vet{\re $1^i$,\re 3,\re 5,\re 6, \bl $0^j$,\bl 2,\bl 4,\bl 9,\bl 10}  &
\vet{\bl 0, \#, \#, \#, \#, \#, \#, \#,\#}\\
\vet{\re $1^i$,\re 3,\re 5,\re 6, \gr 0, \bl $2^j$,\bl 4,\bl 9,\bl 10} &
\vet{\gr 0, \re 1, \#, \#, \#, \#, \#, \#,\#}\\
\vet{\gr 1, \re $3^i$,\re 5,\re 6, \gr 0, \bl $2^j$,\bl 4,\bl 9,\bl 10} &
\vet{\gr 0, \gr 1, \bl 2, \#, \#, \#, \#, \#,\#}\\
\vet{\gr 1, \re $3^i$,\re 5,\re 6, \gr 0, \gr 2,\bl $4^j$,\bl 9,\bl 10} &
\vet{\gr 0, \gr 1, \gr 2, \re 3, \#, \#, \#, \#,\#}\\
\vet{\gr 1, \gr 3,\re $5^i$,\re 6, \gr 0, \gr 2,\bl $4^j$,\bl 9,\bl 10} &
\vet{\gr 0, \gr 1, \gr 2, \gr 3, \bl 4, \#, \#, \#,\#}\\
\vet{\gr 1, \gr 3,\re $5^i$,\re 6, \gr 0, \gr 2,\gr 4,\bl $9^j$,\bl 10} &
\vet{\gr 0, \gr 1, \gr 2, \gr 3, \gr 4, \re 5, \#, \#,\#}\\
\vet{\gr 1, \gr 3,\gr 5,\re $6^i$,\gr 0, \gr 2,\gr 4,\bl $9^j$,\bl 10} &
\vet{\gr 0, \gr 1, \gr 2, \gr 3, \gr 4, \gr 5, \re 6,\#, \#}\\
\vet{\gr 1, \gr 3,\gr 5,\gr $6^i$,\gr 0, \gr 2,\gr 4,\bl $9^j$,\bl 10} &
\vet{\gr 0, \gr 1, \gr 2, \gr 3, \gr 4, \gr 5, \gr 6, \bl 9,\bl 10}\\
\end{tabular}
\end{frame}
\end{document}

Tags:

Tikz Pgf