LaTeX利用lstlistings排版IDL程序
载入宏包
由于我是mac系统所以在可选参数加了fontset
\documentclass[UTF8,fontset = macnew] {ctexart} %mac字体设置
\usepackage{listings}
\usepackage{xcolor}
\usepackage{framed}
listings的宏包支持多种语言,具体可以见下图,而xcolor主要是用来设置一些字体颜色或者边框等颜色,framed的话主要是为了避免程序过长超过一页.
这里面是lstlistings支持的语言.需要的时候只需要在可选参数里设置langauage = 即可
由于很多时候代码太长,会导致超出一页,边框无法封闭,在lstlistings的doc里面说的需要使用framed宏包进行封闭.
xcolor的话只需要定义自己需要的颜色.一些基本的颜色如图
\lstset{numbers=left,
numberstyle=\tiny,
stepnumber=1}
这是对lst环境进行全局的设置,如果设置则会对每个环境进行设置,但是如果在后续的可选参数进行修改的话仍会被覆盖(没试过但是应该跟tikz差不多)
面的话主要是设置旁边的代码行数显示进行设置number显示在左边,stepnumber就是步距…
\begin{lstlisting}[language=IDL,
firstnumber=1, %行标开始字数
frame = single, %边框,有single等
xrightmargin=2cm,
framexrightmargin = 3cm,
breaklines = true, %如果过长自动断行
keywordstyle=\color{red}, %关键字颜色
commentstyle=\color{brown}, %注释颜色
ndkeywordstyle=\corlor{yellow},
stringstyle = \color{blue}, %字符串颜色
basicstyle=\color{teal}, %基本字体的颜色
rulesepcolor=\color{blue},
numbersep=3.2pt, %行数显示距离边框的距离
]
;coding = GBK
;to draw classified graphics and colorbar
pro READCLASS
file = 'C:\Users\黄\Desktop\classimage.dat'
data = make_array(5963,5988,type = 1)
openr,lun,file,/get_lun
readu,lun,data
free_lun,lun
data = congird(data,596,599)
window,1,xsize = 800,ysize=700
device, decomposed = 0
lookup = [255,255,255,90,178,115,233,255,
190,240,240,240,151,219,242,214,133,137]
lookup = reform(lookup, 3, 6)
tvlct,lookup[0,*],lookup[1,*],lookup[2,*]
erase,0
tv,data,50,50,order = 1
colbar = rebin(indgen(1, 6),40,240)
tv,colbar,650,50
loadct,0
shadowdata = bytarr(3,240)+180
tv,shadowdata,690,50
bottomshad = bytarr(40,3)+180
tv,bottomshad,653,47
classnames = ['background','forest','agriculture','city','water','other']
xpos = 705+intarr(6)
ypos = 60+indgen(6)*40
xyouts,xpos,ypos,classnames,color=0,/device
end
\end{lstlisting}
这里我都写了一些注释,需要注意的是这里的缩进是非常严格的.你可以先全部缩进之后然后再空格设置(我还没想到好方法).
这个是没有超出一页边框的程序效果如图.
具体的还是需要多读读lstlistings的documentation的.有许多的参数