Latex文档中的C源代码

Ric*_*d23 7 latex

任何人都可以向我推荐一个好的模板来包含在Latex中使用行编号的C源代码吗?例如,采用经典的Hello world程序,我想使它看起来如下:

(1)   /* Hello World program */
(2)
(3)   #include<stdio.h>
(4)
(5)   main()
(6)   {
(7)    printf("Hello World");
(8)   }
Run Code Online (Sandbox Code Playgroud)

典型的,我总是使用逐字环境,但我想知道是否有更好,更好的方法来做到这一点.

非常感谢理查德

Edd*_*Edd 10

正如其他人所说,listings软件包可能会使用以下内容执行您想要的操作:

\lstset{
  language=C,                % choose the language of the code
  numbers=left,                   % where to put the line-numbers
  stepnumber=1,                   % the step between two line-numbers.        
  numbersep=5pt,                  % how far the line-numbers are from the code
  backgroundcolor=\color{white},  % choose the background color. You must add \usepackage{color}
  showspaces=false,               % show spaces adding particular underscores
  showstringspaces=false,         % underline spaces within strings
  showtabs=false,                 % show tabs within strings adding particular underscores
  tabsize=2,                      % sets default tabsize to 2 spaces
  captionpos=b,                   % sets the caption-position to bottom
  breaklines=true,                % sets automatic line breaking
  breakatwhitespace=true,         % sets if automatic breaks should only happen at whitespace
  title=\lstname,                 % show the filename of files included with \lstinputlisting;
}

\lstinputlisting{HelloWorld.c}
Run Code Online (Sandbox Code Playgroud)

一个更强大的替代方法是使用该minted软件包,虽然这将比您当前要求的更多,因为它使用/要求在系统上安装pygments,以便它可以完全标记您提供的代码.


Sve*_*ach 9

您可能想要查看该listings.它非常灵活且易于使用.


Juh*_*nen 1

查看LaTeX 中的代码清单。您会在那里找到几个替代方案。一些选项是: