篇首语:本文由小编为大家整理,主要介绍了LaTeX实战经验:如何插入程序代码相关的知识,希望对你有一定的参考价值。
>> 点击此处查看 【 LaTeX实战经验:新手须知 】
插入程序代码
传统一点, \usepackagelistings
, 详情可以使用texdoc listings
查看。
\lstsetlanguage=C\beginlstlisting#include <iostream>using namespace std;int main() cout<<"hello"<return 0;\endlstlisting
对效果进行一些定制:
\usepackagelistings\usepackagexcolor\lstset numbers=left, numberstyle= \tiny, keywordstyle= \color blue!70, commentstyle= \colorred!50!green!50!blue!50, frame=shadowbox, % 阴影效果 rulesepcolor= \color red!20!green!20!blue!20 , escapeinside=``, % 英文分号中可写入中文 xleftmargin=2em,xrightmargin=2em, aboveskip=1em, framexleftmargin=2em
效果:
mac 定制代码字体
%!TEX program = xelatex\documentclassarticle\usepackagelistings\usepackagefontspec % 定制字体\newfontfamily\menloMenlo\usepackagexcolor % 定制颜色\definecolormygreenrgb0,0.6,0\definecolormygrayrgb0.5,0.5,0.5\definecolormymauvergb0.58,0,0.82\lstset %backgroundcolor=\colorwhite, % choose the background colorbasicstyle=\footnotesize\ttfamily, % size of fonts used for the codecolumns=fullflexible,tabsize=4,breaklines=true, % automatic line breaking only at whitespacecaptionpos=b, % sets the caption-position to bottomcommentstyle=\colormygreen, % comment styleescapeinside=\%**), % if you want to add LaTeX within your codekeywordstyle=\colorblue, % keyword stylestringstyle=\colormymauve\ttfamily, % string literal styleframe=single,rulesepcolor=\colorred!20!green!20!blue!20,% identifierstyle=\colorred,language=c++,\begindocument\beginlstlisting[language=[ANSI]C, numbers=left, numberstyle=\tiny\menlo, basicstyle=\small\menlo]#include #include #include #define SIZE 26intmain (int argc, char *argv[]) int array[SIZE]; int i; char c; for (i = 0; i < SIZE; i++) array[i] = 0; while ((c = getchar ()) != EOF) if (isupper (c)) array[c - "A"]++; for (i = 0; i < 26; i++) printf ("%c:%5d\n", (char) ("A" + i), array[i]); return 0;\endlstlisting\enddocument% Local Variables:% TeX-engine: xetex% End:
效果图:
参考:https://www.zhihu.com/question/30957600
以上是关于LaTeX实战经验:如何插入程序代码的主要内容,如果未能解决你的问题,请参考以下文章