在 tikzpicture 中嵌入代码片段

在 tikzpicture 中嵌入代码片段

我试图修改给出以下输出的乳胶片段:

在此处输入图片描述

因此,一小段 C 代码片段位于“代码 A”和“代码 B”标签上方,如下所示

                right
   +--------------->-------------+
   |                             |
   |                             |
main {                        int main {              
  printf("Hello\n");            printf("Hello\n");
}                               return 0;                   
                              }

Code A                        Code B
   |                             |
   |                             |
   +---------------<-------------+
                left

所以我尝试做

\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}
\usetikzlibrary{arrows.meta,chains,shapes.geometric}

\begin{document}

\begin{figure}
\begin{center}
\begin{tikzpicture}[
  node distance=2cm,
  font=\small,
  block/.style = {align=center}
]
\node (codeA) [block] {
%    \begin{verbatim}
%    main {
%       printf("Hello\n");
%    }
%    \end{verbatim}
    \tiny{Code A}
};

\node (codeB) [block, right=of codeA] {
%    \begin{verbatim}
%    int main {
%       printf("Hello\n");
%       return 0;
%    }
%    \end{verbatim}
    \tiny{Code B}
};

\path [-latex]
  (codeA.north) edge [bend left=35]
    node[above] {right} (codeB.north);
\path [-latex]  (codeB.south) edge [bend left=35]
    node[below] {left} (codeA.south);
\end{tikzpicture}
\end{center}
\end{figure}
\end{document}

但是在标签部分使用环境会出现错误。

如何才能做到这一点?

答案1

一个简单的解决方法是将 verbatim 环境放在 minipage 中

此外,\tiny命令不会将您想要缩放的文本作为参数,而是根本没有参数......请参阅更改后的命令。

\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}
\usetikzlibrary{arrows.meta,chains,shapes.geometric}

\begin{document}

\begin{figure}
\begin{center}
\begin{tikzpicture}[
  node distance=2cm,
  font=\small,
  block/.style = {align=center}
]
\node (codeA) [block] {%
\centering
\begin{minipage}{4cm}
\centering
    \begin{verbatim}
    main {
       printf("Hello\n");
    }
    \end{verbatim}

    {\tiny Code A}
\end{minipage}%
};

\node (codeB) [block, right=of codeA] {%
\centering
\begin{minipage}{4cm}
\centering
    \begin{verbatim}
    int main {
       printf("Hello\n");
       return 0;
    }
    \end{verbatim}

    {\tiny Code B}
\end{minipage}%
};

\path [-latex]
  (codeA.north) edge [bend left=35]
    node[above] {right} (codeB.north);
\path [-latex]  (codeB.south) edge [bend left=35]
    node[below] {left} (codeA.south);
\end{tikzpicture}
\end{center}
\end{figure}
\end{document}

在此处输入图片描述

编辑:

\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}
\usetikzlibrary{arrows.meta,chains,shapes.geometric}

\begin{document}

\begin{figure}
\begin{center}
\begin{tikzpicture}[
  node distance=2cm,
  font=\small,
  block/.style = {align=center}
]
\node (codeA) [block] {%
\centering
\begin{minipage}{4cm}
\centering
    \begin{verbatim}
    main {
       printf("Hello\n");
    }
    \end{verbatim}
\vspace{3mm}

{\tiny Code A}%
\end{minipage}
};

\node (codeB) [block, right=of codeA] {%
\centering
\begin{minipage}{4cm}
\centering
    \begin{verbatim}
    int main {
       printf("Hello\n");
       return 0;
    }
    \end{verbatim}

\vspace{-2mm}

{\tiny Code B}%
\end{minipage}
};

\path [-latex]
  (codeA.north) edge [bend left=35]
    node[above] {right} (codeB.north);
\path [-latex]  (codeB.south) edge [bend left=35]
    node[below] {left} (codeA.south);
\end{tikzpicture}
\end{center}
\end{figure}
\end{document}

相关内容