8

SE

I am trying to draw the image below in LaTeX using lstlisting, tikzpicture, minipage, and tabular Target picture that we try to draw

The code that I got so far can be seen below

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usepackage{listings}

\lstset{language=C,
            basicstyle=\ttfamily,
            frame=single,
            keywordstyle=\color{blue}\ttfamily,
            stringstyle=\color{red}\ttfamily,
            commentstyle=\color{green}\ttfamily,
            morecomment=[l][\color{magenta}]{\#}
}

\begin{document}

\begin{minipage}{0.8\textwidth}

\begin{tikzpicture}

    \draw[- >,overlay] (1.5,-2.8) -- node [below] {(int, int)} (4,-0.8);

\end{tikzpicture}

\begin{tabular}{cc}
    \begin{lstlisting}[language=C]
void foo()
{
    int q = 5;
    int l = 6;

    bar(q, l);

}
\end{lstlisting}

&
\begin{lstlisting}[language=C]
void bar(int a, int b)
{
    ...
    ...
    ...
    ...
}
\end{lstlisting}

\end{tabular}

\end{minipage}

\end{document}

The code above results in the following piece of 'art' Result from the code above

Is there anyone that can polish this 'art', so it looks a bit more to the target image?

With kind regards,

Xhendos

EDIT: Changed my code so it is compileable

2
  • Please post a compilable example with \documentclass, the required packages etc. Not just a fragment.
    – user31729
    Commented Jan 3, 2019 at 10:18
  • Sorry @ChristianHupfer. The code is now compileable
    – Xhendos
    Commented Jan 3, 2019 at 10:24

2 Answers 2

4

tikzmark, which you are loading, has a library for that purpose, listings, which allows you to refer to lines of the code without actually modifying it. And I am using this nice answer.

\documentclass{article}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usetikzmarklibrary{listings}
\lstset{language=C,
            basicstyle=\ttfamily,
            %frame=single,
            keywordstyle=\color{blue}\ttfamily,
            stringstyle=\color{red}\ttfamily,
            commentstyle=\color{green}\ttfamily,
            morecomment=[l][\color{magenta}]{\#}
}

\newcounter{tmkcount}
\tikzset{
  use tikzmark/.style={
    remember picture,
    overlay,
    execute at end picture={
      \stepcounter{tmkcount}
    },
  },
  tikzmark suffix={-\thetmkcount}
}
\begin{document}
%\begin{minipage}{0.8\textwidth}

\begin{tabular}{|l|p{2cm}|l|}
\multicolumn{1}{l}{\texttt{file\_x.c}} &
\multicolumn{1}{l}{} &
\multicolumn{1}{l}{\texttt{file\_y.c}}\\
\cline{1-1} \cline{3-3}
\begin{lstlisting}[language=C,name=codeL]
void foo()
{
    int q = 5;
    int l = 6;

    bar(q, l);

}
\end{lstlisting}
\begin{tikzpicture}[use tikzmark]
\coordinate (aux) at (pic cs:line-codeL-6-end);
\end{tikzpicture}
& &
\begin{lstlisting}[language=C,name=codeR]
void bar(int a, int b)
{
    ...
    ...
    ...
    ...
    ...
}
\end{lstlisting}\\
\cline{1-1} \cline{3-3}
\end{tabular}
\begin{tikzpicture}[use tikzmark]
\draw[-latex] ([yshift=2ex,xshift=-3ex]aux) to[bend left=10] 
node[midway,above,sloped]{(int,int)} (pic cs:line-codeR-1-start);
\end{tikzpicture}
%\end{minipage}
\end{document}

enter image description here

2
  • I don't know how you do it, but you always surprise me.
    – Xhendos
    Commented Jan 3, 2019 at 10:49
  • @Xhendos Thanks! It's actually all LoopSpace's magic I am trying to use here.
    – user121799
    Commented Jan 3, 2019 at 10:51
4

as tikz image:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning, quotes}
\usepackage{listings}
\lstset{language=C,
            basicstyle=\ttfamily,
            keywordstyle=\color{blue}\ttfamily,
            stringstyle=\color{red}\ttfamily,
            commentstyle=\color{green}\ttfamily,
            morecomment=[l][\color{magenta}]{\#}
}

    \begin{document}
    \begin{tikzpicture}[
node distance = 22mm,
   box/.style = {draw, align=left}
                        ]
\node (a) [box]
{\hspace*{-2em}%
    \begin{lstlisting}[language=C]
void foo()
{
    int q = 5;
    int l = 6;

    bar(q, l);
}
\end{lstlisting}
};
\node (b) [box, right=of a]
{\hspace*{-2em}%
\begin{lstlisting}[language=C]
 void bar(int a, int b)
{
    ...
    ...
    ...
    ...
}
    \end{lstlisting}
};
\draw[->, black!75] ([shift={(2.2,-4.4ex)}] a.west) to [pos=0.7,"{(int,int)}", sloped] ([shift={(2ex,8ex)}] b.west);
\end{tikzpicture}
\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 .