我有一些 C 代码,想用列表将其插入到 LaTeX 文档中。我还想显示其汇编代码的翻译。我不知道如何在多列视图中显示这两种代码,以便让读者能够快速比较 C 代码和汇编代码。我不需要将 C 代码行与其对应的汇编指令对齐,因为我认为这并不容易。我需要的只是并排显示这两个代码。你能帮我吗?
谢谢
答案1
可以使用包显示代码listings
。我将使用两个并排效果,minipages
如下所示。一般来说,你必须小心,因为列表是逐字材料,并且不适用于其他宏的参数。(使用保存框会有所帮助)
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{listings}
\usepackage{lipsum}
\lstset{basicstyle=\ttfamily\scriptsize}
\begin{document}
\lipsum[1]
\par\noindent
\begin{minipage}[t]{.45\textwidth}
\begin{lstlisting}[language=C,frame=lrtb]
int function test(int a) {
return a;
}
\end{lstlisting}%
\end{minipage}%
\hfill
\begin{minipage}[t]{.45\textwidth}
\begin{lstlisting}[frame=lrtb]
TEST:
goto SOMEWHERE
\end{lstlisting}%
\end{minipage}%
\lipsum[2]
\end{document}
答案2
你可以尝试帕科卢姆斯包裹。
另请参阅其他问题:平行文本、列表和显示方程式。
例子
\documentclass[12pt]{article}
\usepackage[a4paper,margin=1cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[scaled=0.82]{beramono}
\usepackage{pifont,calc,xcolor}
\usepackage{parcolumns}
\usepackage{listings}
\definecolor{verylightgray}{gray}{0.85}
\newlength\lsthorizontalpadding
\setlength\lsthorizontalpadding{3pt}
\newcommand*\lstnumberstyle{\ttfamily\scriptsize}
\newlength\lstnumbersep
\setlength\lstnumbersep{10pt}
\newlength\lstnumberwidth
\setlength\lstnumberwidth{\widthof{\lstnumberstyle00}+\lstnumbersep+\lsthorizontalpadding}
\lstset{
,basicstyle=\ttfamily%
,breaklines=true%
,tabsize=4%
,showstringspaces=false%
,numbers=left%
,numbersep=\lstnumbersep%
,numberstyle=\lstnumberstyle%
,framesep=0pt%
,xleftmargin=\lstnumberwidth%
,framexleftmargin=\lsthorizontalpadding%
,xrightmargin=\lsthorizontalpadding%
,framexrightmargin=\lsthorizontalpadding%
,backgroundcolor=\color{verylightgray}%
,postbreak=\ding{229}\space%
}
%% far from complete list of gasm-specific keywords and instructions in AT&T syntax
\lstdefinelanguage[x86gasm]{Assembler}[x86masm]{Assembler}{%
,morekeywords={rax,rbx,rcx,rdx,rip,rdi,rsi,rsp,subq,decl,movq,movl,xorl,imull,popq}%
,morekeywords=[2]{.file,.section,.string,.text,.globl,.cfi_startproc,.cfi_def_cfa_offset,.cfi_endproc,.size,.ident}%
}
\lstnewenvironment{ccol}[1][]{\lstset{language=c,#1}}{}
\lstnewenvironment{acol}[1][]{\lstset{language=[x86gasm]Assembler,#1}}{}
\pagestyle{empty}
\parindent=0pt
\begin{document}
\texttt{gcc} on \texttt{text.c} file (left side) [\texttt{gcc -S -Os text.c}] gives following assembler code (right side):
\begin{parcolumns}{2}
\colchunk[1]{\begin{ccol}
#include <stdio.h>
int main(int argc, char *argv[])
{
int t;
if (argc < 2) {
fprintf(stderr, "Not enough arguments!\n");
return 1;
}
t = atoi(argv[1]);
printf("%d^2 = %d\n", t, t * t);
return 0;
}
\end{ccol}}
\colchunk[2]{\begin{acol}
.file "test.c"
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "Not enough arguments!\n"
.LC1:
.string "%d^2 = %d\n"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
subq $8, %rsp
.cfi_def_cfa_offset 16
decl %edi
jg .L2
movq stderr(%rip), %rsi
movl $.LC0, %edi
call fputs
movl $1, %eax
jmp .L3
.L2:
movq 8(%rsi), %rdi
xorl %eax, %eax
call atoi
movl %eax, %edx
movl %eax, %esi
movl $.LC1, %edi
imull %eax, %edx
xorl %eax, %eax
call printf
xorl %eax, %eax
.L3:
popq %rdx
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Debian 4.4.5-8) 4.4.5"
.section .note.GNU-stack,"",@progbits
\end{acol}}
\end{parcolumns}
\end{document}