2

I was trying to modify a latex snippet that gave this output:

enter image description here

so that a small C code snippet is place above the 'Code A' and 'Code B' labels as in

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

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

So I tried to do

\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}

But using an environment in the label part gave error.

How can this be done?

J...S
  • 189
  • Does this answer your question? https://tex.stackexchange.com/q/345871/110998 – gernot Jan 10 '22 at 09:46
  • Not exactly. There the verbatim env seems to be outside the tikzpicture. Not sure if that can be adopted for my use case though.. – J...S Jan 10 '22 at 09:57

1 Answers1

2

A simple fix is to place the verbatim environment inside a minipage

Also, \tiny command doesn't take as argument the text that you want to scale but no argument at all... See the changed command.

\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}

enter image description here

EDIT:

\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}

koleygr
  • 20,105