首发于 计算机

LaTeX listings 宏包使用说明

\small\color{blue}{预设}

\lstset{ \small\color{purple}{基本设置}
basicstyle =\small, %环境中的代码字体变小 [在结尾要加逗号!]
basicstyle =\tt, %使用teletype字体(一种等宽字体)
basicstyle =\it, %使用罗马斜体
%其他可选的还有:\bf, \sc, \st, \sl, \em, \nocorr等
}

\lstset{ \small\color{purple}{关键字设置}
keywordstyle =\color{black}, %设置关键字颜色为黑色
keywordstyle =\color{blue}, %设置关键字颜色为蓝色
keywordstyle =\bfseries, %不改变当前字体的族与形状,但转变成bold加粗序列
keywordstyle =\mdseries, %不改变当前字体的族与形状,但转变成中等粗细medium序列
keywordstyle =\underbar, %添加下划线
keywordstyle =\color{black}\bfseries\underbar, %添加下划线的加粗黑色关键字
}

\lstset{
identifierstyle =\color{red},
identifierstyle = , %不设置 }

showstringspaces =false %去掉空格时产生的下划的空格标志, 设置为true则出现
showtabs =true

\lstset{ \small\color{purple}{注释设置}
commentstyle =\it\color[RGB]{100,100,100}, }

示例:

\lstset{
    basicstyle=\tt,
    keywordstyle=\color{purple}\bfseries,
    identifierstyle=\color{brown!80!black},
    commentstyle=\color{gray}
    showstringspaces=false,
\begin{lstlisting}[language=C]      
#include <stdio.h>
    //Example
    printf("Hello World");
    int i, sum = 0;
    for (i = 0; i < 10; i++){
        sum += i;
    printf("%d", sum);
    return 0;
\end{lstlisting}

\lstset{
numbers =left, %在左侧显示行数
numbers =right %在右侧显示行数
numbers =none, %去掉行数显示
numberstyle =\tiny\color{red}, %数字大小,颜色调整
numberstyle =\it, %数字字体调整
stepnumber =2 , %每两行标号一次
numbersep =5pt, %数字右端(若为左侧显示数字)水平距离代码5pt
}

示例:

\lstset{
    numbers=left, numberstyle=\it, stepnumber=2, numbersep=5pt,
\begin{lstlisting}[language=Java]
public class HelloWorld {
    public static void main(String[] args) {