在LaTeX环境下用listing排版C语言简洁样式分享(转)

      最近在忙手准备着这阵子暑假集训里所用到的大部分的算法模板,拿来作为比赛的时候用作参考的资料,一想到ms的word或wps的word的时候就不禁感到心寒,拿拿东西来放置模板的话简直毫无美感,看着就让人恶心,于是想到了用LaTeX来对我的模板进行排版,可是LaTeX的学习代价似乎又挺高的,犹如学习一门新的编程语言一般,哎~不管啦,管他到时来不来得及呢,反正这是个好东西,学习一下肯定无妨大碍,假若真的来不及,到时就捧着我的白书训练指南去吧。。。。=。=!!

  二话不说,这篇文章是转过来的,对于LaTeX排版的文档中如果需要插入代码的话,那应该是非常有用的【ps,本人的LaTeX运行环境是windows7   Texmaker    Texmaker是一款不错的LaTeX排版工具,具有自动补齐代码,提示的功能 非常好用就像IDE一样,且开源免费~

  之前,看过不少用listings排版的伪代码,在设置listings方面上,不少有些在配色和设置上还有些小问题,建议能多多参考其他作者做好的设置,下面的这个排版C语言的设置非常贴合C语言常用的编程样式。推荐推荐,其listings选项设置也并不复杂。

效果图:                        

latex-listing-c

 

其实现的代码如下:

 1 \documentclass[a4paper,10pt]{article}
 2 \usepackage{fancybox}
 3 \usepackage{listings}
 4 \usepackage{xcolor}
 5 \usepackage{times}
 6 \begin{document}
 7 \section{Counting Numbers of Uppercase Characters}
 8 \begin{center}
 9 \begin{lstlisting}[language={[ANSI]C},
10         numbers=left,
11         numberstyle=\tiny,
12         basicstyle=\small\ttfamily,
13         stringstyle=\color{purple},
14         keywordstyle=\color{blue}\bfseries,
15         commentstyle=\color{olive},
16         directivestyle=\color{blue},
17         frame=shadowbox,
18         %framerule=0pt,
19         %backgroundcolor=\color{pink},
20         rulesepcolor=\color{red!20!green!20!blue!20}
21         %rulesepcolor=\color{brown}
22         %xleftmargin=2em,xrightmargin=2em,aboveskip=1em
23         ]
24 /*===================================================
25 * FileName: counter.c
26  * Author  : Shaobin Li <shawpinlee@gmail.com>
27  * Date    : 2007-09-13
28 * Description: count numbers of uppercase
29 * characters from input streams, and output
30 * the numbers respectively.
31 *=================================================*/
32 #include <stdio.h>
33 #include <stdbool.h>
34 #include <ctype.h>
35  
36 #define SIZE 26
37  
38 int
39 main (int argc, char *argv[])
40 {
41   int array[SIZE];
42   int i;
43   char c;
44  
45   for (i = 0; i < SIZE; i++)
46     array[i] = 0;
47  
48   while ((c = getchar ()) != EOF)
49     {
50       if (isupper (c))
51         {
52           array[c - 'A']++;
53         }
54     }
55   for (i = 0; i < 26; i++)
56     printf ("%c:%5d\n", (char) ('A' + i), array[i]);
57  
58   return 0;
59 }
60  
61 \end{lstlisting}
62 \end{center}
63 \end{document}
View Code

2014-08-16    21:17:44

posted @ 2014-08-16 21:18  jusonalien  阅读(1952)  评论(0)    收藏  举报