![两个列表并排,带有引用箭头](https://linux22.com/image/388541/%E4%B8%A4%E4%B8%AA%E5%88%97%E8%A1%A8%E5%B9%B6%E6%8E%92%EF%BC%8C%E5%B8%A6%E6%9C%89%E5%BC%95%E7%94%A8%E7%AE%AD%E5%A4%B4.png)
东南
lstlisting
我正在尝试使用、、和在tikzpicture
LaTeX中绘制下面的图像minipage
tabular
我目前得到的代码如下所示
\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}