使用 tikz 数组箭头

使用 tikz 数组箭头

我正在尝试绘制两个用箭头分隔的数组。这是我目前的代码。它给出了一个错误,箭头显示不在同一行。有什么帮助吗?有没有比使用 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}

答案1

您的 MWE 存在许多问题:

  • \Longrightarrow应该在数学环境中:$\Longrightarrow$
  • [fragile]必须位于框架标题之前:
\begin{frame}[fragile]{Sorting}

甚至更好

\begin{frame}[fragile]
\frametitle{Sorting}
  • 为了使图片和箭头垂直对齐,需要改变图片的基线。例如:
\begin{tikzpicture}[baseline=-3pt]
  • 通过定义矩阵样式,您可以使代码更简单、更简短。请参阅下面的 MWE:
\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}

在此处输入图片描述

相关内容