Latex中插入C代碼

來源:互聯網
上載者:User

用LaTeX寫論文時,插入C++原始碼有專門的宏包,方法如下:

 1 \usepackage{listings}
2
3 \lstset{language=C++}%這條命令可以讓LaTeX排版時將C++鍵字反白
4
5 \lstset{breaklines}%這條命令可以讓LaTeX自動將長的程式碼換行排版
6
7 \lstset{extendedchars=false}%這一條命令可以解決代碼跨頁時,章區段標頭,頁首等漢字不顯示的問題
8
9 \begin{lstlisting}
10
11 %paste your C++ code here
12
13 \end{lstlisting}

  

下面是我常用的設定:

 

 1 \lstset{                        %Settings for listings package.
2 language=[ANSI]{C},
3 backgroundcolor=\color{lightgray},
4 basicstyle=\footnotesize,
5 breakatwhitespace=false,
6 breaklines=true,
7 captionpos=b,
8 commentstyle=\color{olive},
9 directivestyle=\color{blue},
10 extendedchars=false,
11 % frame=single,%shadowbox
12 framerule=0pt,
13 keywordstyle=\color{blue}\bfseries,
14 morekeywords={*,define,*,include...},
15 numbersep=5pt,
16 rulesepcolor=\color{red!20!green!20!blue!20},
17 showspaces=false,
18 showstringspaces=false,
19 showtabs=false,
20 stepnumber=2,
21 stringstyle=\color{purple},
22 tabsize=4,
23 title=\lstname
24 }

 



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.