转载链接:http://blog.csdn.net/simple_the_best/article/details/52710830
插入程序代码
传统一点, \usepackage{listings}, 详情可以使用texdoc listings查看。
\lstset{language=C}
\begin{lstlisting}
#include <iostream>
using namespace std;
int main()
{
cout<<
"hello"<<endl;
return 0;
}
\end{lstlisting}
1234567891011
1234567891011
对效果进行一些定制:
\usepackage{listings
}
\usepackage{xcolor
}
\lstset{
numbers=left,
numberstyle=
\tiny,
keywordstyle=
\color{ blue!70
},
commentstyle=
\color{red!50!green!50!blue!50
},
frame=shadowbox,
rulesepcolor=
\color{ red!20!green!20!blue!20
} ,
escapeinside=``,
xleftmargin=2em,xrightmargin=2em, aboveskip=1em,
framexleftmargin=2em
}
12345678910111213
12345678910111213
效果:
mac 定制代码字体
\documentclass{article
}
\usepackage{listings
}
\usepackage{fontspec
}
\newfontfamily\menlo{Menlo
}
\usepackage{xcolor
}
\definecolor{mygreen
}{rgb
}{0,0.6,0
}
\definecolor{mygray
}{rgb
}{0.5,0.5,0.5
}
\definecolor{mymauve
}{rgb
}{0.58,0,0.82
}
\lstset{
backgroundcolor=
\color{white
},
basicstyle=
\footnotesize\ttfamily,
columns=fullflexible,
tabsize=4,
breaklines=true,
captionpos=b,
commentstyle=
\color{mygreen
},
escapeinside=
{\%*
}{*)
},
keywordstyle=
\color{blue
},
stringstyle=
\color{mymauve
}\ttfamily,
frame=single,
rulesepcolor=
\color{red!20!green!20!blue!20
},
language=c++,
}
\begin{document
}
\begin{lstlisting
}[language=
{[ANSI
]C
},
numbers=left,
numberstyle=
\tiny\menlo,
basicstyle=
\small\menlo]
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#define SIZE 26
int
main (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 ("
return 0;
}
\end{lstlisting
}
\end{document
}
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
效果图: