\usepackage{caption}
\usepackage{listings} % 排代码用的宏包
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\lstset{frame=tb,
language=Python,
aboveskip=3mm,
belowskip=3mm,
showstringspaces=false,
columns=flexible,
basicstyle={\small\ttfamily},
numbers=none,
numberstyle=\tiny\color{gray},
keywordstyle=\color{blue},
commentstyle=\color{dkgreen},
stringstyle=\color{mauve},
breaklines=true,
breakatwhitespace=true,
tabsize=3
}
\lstset{
language = matlab,
backgroundcolor = \color{gray!3}, % 背景色
basicstyle = \small\ttfamily, % 基本样式 + 小号字体
rulesepcolor= \color{gray}, % 代码块边框颜色
breaklines = true, % 代码过长则换行
numbers = left, % 行号在左侧显示
numberstyle = \small, % 行号字体
keywordstyle = \color{blue}, % 关键字颜色
commentstyle =\color{green!10}, % 注释颜色
stringstyle = \color{red!100}, % 字符串颜色
%frame = shadowbox, % 用(带影子效果)方框框住代码块
showspaces = false, % 不显示空格
columns = fixed, % 字间距固定
%escapeinside={} % 特殊自定分隔符:
morekeywords = {as}, % 自加新的关键字(必须前后都是空格)
}
\lstset{
language = C,
numbers=left, %行号
%背景框
framexleftmargin=10mm,
frame=none,
%背景色
backgroundcolor = \color{gray!3}, % 背景色
%样式
keywordstyle=\bf\color{blue},
identifierstyle=\bf,
numberstyle=\color[RGB]{0,192,192},
commentstyle=\it\color[RGB]{0,96,96},
stringstyle=\rmfamily\slshape\color[RGB]{128,0,0},
%显示空格
showstringspaces=false
}
\usepackage{listings}
\usepackage{color}
\usepackage{xcolor}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\lstset{frame=tb,
language=Java,
aboveskip=3mm,
belowskip=3mm,
showstringspaces=false,
columns=flexible,
basicstyle = \ttfamily\small,
numbers=none,
numberstyle=\tiny\color{gray},
keywordstyle=\color{blue},
commentstyle=\color{dkgreen},
stringstyle=\color{mauve},
breaklines=true,
breakatwhitespace=true,
tabsize=3
}
示例:
\begin{lstlisting}[caption=A Sample, language=python]
#include<stdio>
int main(){
if() #ajda
print("ok asd alsd") //adwklaj
%'ada'"'a 中文
}
\end{lstlisting}
其中,python的配色参考了 LaTeX代码环境配置:使用LaTeX展示Python代码_silent56_th的博客-优快云博客_latex python代码展示
java的配色参考了 latex 插入java 代码模版_孤鸿子_的博客-优快云博客