两个列表并排,带有引用箭头

两个列表并排,带有引用箭头

东南

lstlisting我正在尝试使用、、和在tikzpictureLaTeX中绘制下面的图像minipagetabular 我们试图绘制的目标图片

我目前得到的代码如下所示

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

上面的代码生成了以下“艺术作品” 上述代码的结果

有谁可以润色这幅“艺术品”,使它看起来更接近目标图像吗?

亲切的问候,

赞多斯

编辑: 修改了我的代码,使其可编译

答案1

tikzmark,您正在加载,有一个用于此目的的库,,listings它允许您引用代码行而无需实际修改它。我正在使用这个很好的答案

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

在此处输入图片描述

答案2

如图tikz

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

在此处输入图片描述

相关内容