不需要特定的代码格式,用LaTeX排版代码:
\usepackage{listings}%排版代码的包
\begin{document}
\lstinputlisting{code/demo.c}%代码的文件名,源代码要utf-8,不然有中文的注释那些是乱码
\end{document}
需要特定的格式,代码格式找了很多地方,记录一些代码的格式。目前有C/C++,Python的listing格式。内容转自维基百科、Stack Overflow等地,稍有修改。
格式的定义放在导言区,先加排版代码的包:
\usepackage{listings}
然后定义语言的格式
C/C++:
\lstdefinestyle{customc}{% customc 是格式的名字
belowcaptionskip=1\baselineskip,
breaklines=true,
frame=l,
xleftmargin=\parindent,
language=C,
showstringspaces=false,
basicstyle=\footnotesize\ttfamily,
keywordstyle=\bfseries\color{green!40!black},
commentstyle=\itshape\color{purple!40!black},
identifierstyle=\color{blue},
stringstyle=\color{orange},
tabsize=2,
}
Python
\usepackage{color}
\definecolor{dkgreen}{rgb}{0,0.6