0

I am trying to draw two arrays separated using an arrow. This is my code so far. It gives me an error, and the arrow is shown not on the same line as arrays. Any help ? Is there a better way to do this rather than using tabular ?

\documentclass[12pt,xcolor=dvipsnames]{beamer}
\usepackage{tikz}

\usetikzlibrary{arrows.meta,
                matrix,
                positioning,
                }

\makeatletter
\patchcmd{\beamer@sectionintoc}
  {\vfill}
  {\vskip0.5em}{} {}
\pretocmd{\beamer@subsectionintoc}
  {\vskip0.25em}
  {}
  {}
\makeatother 

\title[Example]{Example}
\author{Author}


\begin{document}
\begin{frame}[fragile]{Sorting}
\textbf{Example:}
\begin{tabular}{ccc}
    \begin{tikzpicture} [nodes in empty cells,
        nodes={minimum width=0.5cm, minimum height=0.5cm},
        row sep=-\pgflinewidth, column sep=-\pgflinewidth]
        %    border/.style={draw}
        \matrix(vector)[matrix of nodes, ampersand replacement=\&, % <- added ampersand replacement
         row 1/.style={nodes={ minimum width=0.3cm}},
         nodes={draw}]
          {
             $3$ \& $4$ \& $1$ \& $2$ \\
         };
         \end{tikzpicture} & $\Longrightarrow$ &  \begin{tikzpicture} [nodes in empty cells,
          nodes={minimum width=0.5cm, minimum height=0.5cm},
        row sep=-\pgflinewidth, column sep=-\pgflinewidth]
                \matrix(vector)[matrix of nodes, ampersand replacement=\&, % <- added ampersand replacement
         row 1/.style={nodes={ minimum width=0.3cm}},
         nodes={draw}]
         { 
            $1$ \& $2$ \& $3$ \& $4$\\
         };
          \end{tikzpicture}
    \end{tabular}    
\end{frame}
\end{document}
3
  • (i) \Longrightarrow should be in math environment: $\Longrightarrow$, (ii) [fragile] had to be before frame title: \begin{frame}[fragile]{Sorting}
    – Zarko
    Jul 16, 2022 at 1:09
  • @Zarko, I corrected it. Thanks. But as you can see arrow appears below the arrays. Not on the same line.
    – Shew
    Jul 16, 2022 at 1:16
  • of course not, bzt this was not possible to correct before correct your MWE. See my answer )will appear soon)
    – Zarko
    Jul 16, 2022 at 1:19

1 Answer 1

1

Your MWE has many issues:

  • \Longrightarrow should be in math environment: $\Longrightarrow$,
  • [fragile] had to be before frame title:
\begin{frame}[fragile]{Sorting}

or even better

\begin{frame}[fragile]
\frametitle{Sorting}
  • for vertical aligning od pictures and arrow, you need to change pictures' baselines. For example:
\begin{tikzpicture}[baseline=-3pt]
  • You can make code simpler and shorter by defining matrix style. See MWE below:
\documentclass[12pt,xcolor=dvipsnames]{beamer}
\usepackage{tikz}

\usetikzlibrary{arrows.meta,
                matrix,
                positioning,
                }

\makeatletter % this part of code is not needed in this MWE
\patchcmd{\beamer@sectionintoc}
  {\vfill}
  {\vskip0.5em}{} {}
\pretocmd{\beamer@subsectionintoc}
  {\vskip0.25em}
  {}
  {}
\makeatother

\title[Example]{Example}
\author{Author}


\begin{document}
\begin{frame}[fragile]
\frametitle{Sorting}
    
\tikzset{
    Matrix/.style = {matrix of nodes,
                     nodes in empty cells,
                     nodes={draw, minimum size=5mm, anchor=center},
                     row sep=-\pgflinewidth,
                     column sep=-\pgflinewidth},
                     ampersand replacement=\&
        }
                        
\textbf{Example:}
    \begin{tabular}{ccc}
\begin{tikzpicture}[baseline=-3pt]
\matrix [Matrix]
{
3   \& 4 \& 1 \& 2 \\
};
\end{tikzpicture} 
    & $\Longrightarrow$ 
        &  \begin{tikzpicture}[baseline=-3pt]
        \matrix [Matrix]
        {
        1 \& 2 \& 3 \& 4    \\
        };
          \end{tikzpicture}
    \end{tabular}
\end{frame}
\end{document}

enter image description here

You must log in to answer this question.

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