我有一个 LaTeX 代码设置如下:
\documentclass[a4paper,10pt]{report}
\usepackage[utf8]{inputenc}
\usepackage{fullpage}
\usepackage{listings}
\usepackage{graphicx}
\usepackage{float}
\usepackage{parcolumns}
\usepackage{qtree}
\usepackage{url}
\usepackage{color}
\usepackage{xcolor}
\usepackage{caption}
\usepackage{textcomp}
\usepackage{todonotes}
\lstset{
language=C,
keywordstyle=\bfseries\ttfamily\color[rgb]{0,0,1},
identifierstyle=\ttfamily,
commentstyle=\color[rgb]{0.133,0.545,0.133},
stringstyle=\ttfamily\color[rgb]{0.627,0.126,0.941},
showstringspaces=false,
basicstyle=\small,
numberstyle=\footnotesize,
stepnumber=1,
numbersep=10pt,
tabsize=2,
breaklines=true,
prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
breakatwhitespace=false,
aboveskip={1.5\baselineskip},
columns=fixed,
upquote=true,
extendedchars=true,
frame=single
}
\begin{document}
\begin{parcolumns}[nofirstindent]{2}
\colchunk{
\noindent\begin{minipage}{.45\textwidth}
\begin{lstlisting}[language={[x86masm]Assembler},caption={Original code},label={lst:OrigCode}]
1
2
3
4
5
..
12
\end{lstlisting}
\end{minipage}
}
\colchunk{
\begin{minipage}{.45\textwidth}
\begin{lstlisting}[language={[x86masm]Assembler},caption={Code after insertion of detour},label={lst:AfterDetour}]
86
4
5
..
12
\end{lstlisting}
\end{minipage}
}
\colplacechunks
\end{parcolumns}
\end{document}
这将产生两个并排的代码清单,如下所示:
我想通过在代码更改的部分之间画线来更清楚地说明更改,以产生如下效果:
这可能吗?我的替代方法是修改图像,然后将其作为图像附加到文档中,但如果我决定修改列表,这是不可取的。
答案1
您可以使用overlay
和remember picture
属性tikz
/pgf
标记列表中的元素,并在排版后绘制线条。为此,您可以暂时“转义”列表格式,方法mathescape=true
是标记特定位置。这是一个最小示例:
\documentclass[a4paper,10pt]{report}
\usepackage{fullpage}% http://ctan.org/pkg/fullpage
\usepackage{listings}% http://ctan.org/pkg/listings
\usepackage{parcolumns}% http://ctan.org/pkg/parcolumns
\usepackage{textcomp}% http://ctan.org/pkg/textcomp
\usepackage{tikz}% http://ctan.org/pkg/pgf
\usetikzlibrary{calc}
% http://tex.stackexchange.com/questions/1559/adding-a-large-brace-next-to-a-body-of-text/1570#1570
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\lstset{
language=C,
keywordstyle=\bfseries\ttfamily\color[rgb]{0,0,1},
identifierstyle=\ttfamily,
commentstyle=\color[rgb]{0.133,0.545,0.133},
stringstyle=\ttfamily\color[rgb]{0.627,0.126,0.941},
showstringspaces=false,
basicstyle=\small,
numberstyle=\footnotesize,
stepnumber=1,
numbersep=10pt,
tabsize=2,
breaklines=true,
prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
breakatwhitespace=false,
aboveskip={1.5\baselineskip},
columns=fixed,
upquote=true,
extendedchars=true,
frame=single,
mathescape % Allows escaping to (La)TeX mode within $..$
}
\begin{document}
\begin{parcolumns}[nofirstindent]{2}
\colchunk{
\noindent\begin{minipage}{.45\textwidth}
\begin{lstlisting}[language={[x86masm]Assembler},caption={Original code},label={lst:OrigCode}]
1 $\tikzmark{L1line1}$
2
3
4 $\tikzmark{L1line4}$
5
..
12
\end{lstlisting}
\end{minipage}
}
\colchunk{
\begin{minipage}{.45\textwidth}
\begin{lstlisting}[language={[x86masm]Assembler},caption={Code after insertion of detour},label={lst:AfterDetour}]
$\tikzmark{L2line1}$ 86
$\tikzmark{L2line4}$ 4
5
..
12
\end{lstlisting}
\end{minipage}
}
\colplacechunks
\end{parcolumns}
\tikz[overlay,remember picture] \draw[color=red] ($(L1line1)+(0pt,0.7ex)$) -- ($(L2line1)+(0pt,0.7ex)$);
\tikz[overlay,remember picture] \draw[color=blue] ($(L1line4)+(0pt,0.7ex)$) -- ($(L2line4)+(0pt,0.7ex)$);
\end{document}
在“数学转义模式”下,您可以执行任何 (La)TeX 命令。我们只是利用这个机会来设置\tikzmark
我们稍后使用的。
节点的放置和对齐由您决定。可以使用类似的方法pst-node
来自pstricks
捆绑,但需要latex
-> dvips
->ps2pdf
或xelatex
编译序列。下面是使用 重复上述方法pstricks
,它会产生相同的输出:
\documentclass[a4paper,10pt]{report}
\usepackage{fullpage}% http://ctan.org/pkg/fullpage
\usepackage{listings}% http://ctan.org/pkg/listings
\usepackage{parcolumns}% http://ctan.org/pkg/parcolumns
\usepackage{textcomp}% http://ctan.org/pkg/textcomp
\usepackage{pst-node}% http://ctan.org/pkg/pst-node
\lstset{
language=C,
keywordstyle=\bfseries\ttfamily\color[rgb]{0,0,1},
identifierstyle=\ttfamily,
commentstyle=\color[rgb]{0.133,0.545,0.133},
stringstyle=\ttfamily\color[rgb]{0.627,0.126,0.941},
showstringspaces=false,
basicstyle=\small,
numberstyle=\footnotesize,
stepnumber=1,
numbersep=10pt,
tabsize=2,
breaklines=true,
prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
breakatwhitespace=false,
aboveskip={1.5\baselineskip},
columns=fixed,
upquote=true,
extendedchars=true,
frame=single,
mathescape % Allows escaping to (La)TeX mode within $..$
}
\begin{document}
\begin{parcolumns}[nofirstindent]{2}
\colchunk{
\noindent\begin{minipage}{.45\textwidth}
\begin{lstlisting}[language={[x86masm]Assembler},caption={Original code},label={lst:OrigCode}]
1 $\rnode[r]{L1line1}{\strut}$
2
3
4 $\rnode[r]{L1line4}{\strut}$
5
..
12
\end{lstlisting}
\end{minipage}
}
\colchunk{
\begin{minipage}{.45\textwidth}
\begin{lstlisting}[language={[x86masm]Assembler},caption={Code after insertion of detour},label={lst:AfterDetour}]
$\rnode[l]{L2line1}{\strut}$ 86
$\rnode[l]{L2line4}{\strut}$ 4
5
..
12
\end{lstlisting}
\end{minipage}
}
\colplacechunks
\end{parcolumns}
\psline[linecolor=red](L1line1)(L2line1)
\psline[linecolor=blue](L1line4)(L2line4)
\end{document}
答案2
Werner 答案的更新:我不得不使用\renewcommand
而不是\newcommand
。否则它将无法工作,因为\tikzmark
已经定义。
编辑:我能够稍微扩展一下命令。所以现在它也可以内联运行。
\renewcommand{\tikzmark}[1]{\tikz[overlay,remember picture,baseline=(#1.base)] \node (#1) {};}