在编译tex文档时,经常要运行xelatex(or latex etc.)两遍才能得到正确的输出。另外,xelatex命令还会输出一些对用户来说没有用处的临时文件比如*.aux、*.out、*.snm、*.nav、*.toc。因此写了个工具xelatexgo,该工具自动运行xelatex命令两遍,结束后删除临时文件以保持目录的整洁。需要可执行文件的请到下列地址下载:
http://www.live-share.com/files/282550/xltgo.rar.html
下载后解压到Tex系统的bin目录或windows的system32目录中。
用法:xltgo myfile.tex
或者: xltgo myfile.tex -showpdf -encrypt
(-showpdf:编译完后调用系统默认的PDF阅读器自动显示PDF;
-encrypt:加密生成的PDF)
源码如下:
http://www.live-share.com/files/282550/xltgo.rar.html
下载后解压到Tex系统的bin目录或windows的system32目录中。
用法:xltgo myfile.tex
或者: xltgo myfile.tex -showpdf -encrypt
(-showpdf:编译完后调用系统默认的PDF阅读器自动显示PDF;
-encrypt:加密生成的PDF)
源码如下:
1#-*- coding: gbk -*-
2#执行两遍 xelatex -halt-on-error sys.argv[1], 然后显示pdf
3import os,sys,WConio
4
5helpmsg="""usage: xltgo texfile [options]
6options:
7 -showpdf: show pdf after compiled
8 -encrypt: encrtypt pdf
9e.g: xltgo youfile
10 xltgo youfile.tex -showpdf -encrypt"""
11
12def printgoodnews(message):
13 global attr
14 WConio.textcolor(WConio.GREEN)
15 total_chars=80
16 ll=len(message)
17 start=(total_chars-ll)/2
18 print "-"*start+message+"-"*(80-start-ll)
19 WConio.textcolor(attr)
20
21def printbadnews(message):
22 global attr
23 WConio.textcolor(WConio.GREEN)
24 total_chars=80
25 ll=len(message)
26 start=(total_chars-ll)/2
27 print "-"*start+message+"-"*(80-start-ll)
28 WConio.textcolor(attr)
29
30def cleartmpfiles(file):
31 dir=os.path.dirname(file)
32 filename=os.path.basename(file)
33 if (filename[-4:]).upper()=='.TEX':
34 filename=filename[:-4]
35 tmpexts=['.aux','.log','.nav','.out','.snm','.toc','vrb','.xdv']
36
37 for ext in tmpexts:
38 tmpfile=os.path.join(dir,filename)+ext
39 #print tmpfile
40 if os.path.exists(tmpfile):
41 print "Deleting %s" % tmpfile
42 os.unlink(tmpfile)
43
44if __name__=='__main__':
45 attr=WConio.gettextinfo()[4]
46 if len(sys.argv)==1:
47 print helpmsg
48 if len(sys.argv)>1 :
49 options=[option.upper() for option in sys.argv[1:]]
50
51 exist=False
52 out_dir=os.path.dirname(os.path.abspath(sys.argv[1]))
53 filename=os.path.abspath(sys.argv[1])
54 filename_noext=filename
55 if os.path.abspath(sys.argv[1])[-4:].upper()=='.TEX':
56 filename_noext=sys.argv[1][:-4]
57 if os.path.abspath(sys.argv[1])[-4:].upper()=='.TEX' :
58 exist= os.path.exists(filename)
59 else:
60 exist= os.path.exists(filename_noext+".tex")
61
62 if exist:
63 printgoodnews('xelatex -halt-on-error -no-pdf -output-directory="%s" "%s"' %(out_dir,filename))
64 i=-1
65 j=-1
66 k=-1
67 i=os.system('xelatex -halt-on-error -no-pdf -output-directory="%s" "%s"' %(out_dir, filename))
68 if i==0:
69 printgoodnews("The xelatex 1st run finished!")
70 j=os.system('xelatex -halt-on-error -no-pdf -output-directory="%s" "%s"' %(out_dir, filename))
71 if j==0:
72 printgoodnews("The xelatex 2nd run finished!")
73
74 if '-ENCRYPT' in options:
75 k=os.system('xdvipdfmx -r 600 -E -V 5 -S -K 128 -P 0x0804 -o "%s.pdf" "%s.xdv"' % (filename_noext,filename_noext))
76 else:
77 k=os.system('xdvipdfmx -r 600 -E -V 5 -o "%s.pdf" "%s.xdv"' % (filename_noext,filename_noext))
78
79 if k==0:
80 printgoodnews("xdvipdfmx finished!")
81 if '-SHOWPDF' in options:
82 k=os.startfile(filename_noext+".pdf")
83 printgoodnews("Command 'showpdf' was executed!")
84 cleartmpfiles(sys.argv[1])
85 printgoodnews("All finished!")
86 sys.exit(0)
87 else:
88 printbadnews("Error on xdvipdfmx!")
89 sys.exit(-1)
90 else:
91 printbadnews("Error on the 2rd run !")
92 cleartmpfiles(sys.argv[1])
93 sys.exit(-1)
94 else:
95 printbadnews("Error on the 1st run !")
96 cleartmpfiles(sys.argv[1])
97 sys.exit(-1)
98 else:
99 printbadnews( "Error: where is your tex file %s?'"%sys.argv[1])
100 sys.exit(-1)
101
102
2#执行两遍 xelatex -halt-on-error sys.argv[1], 然后显示pdf
3import os,sys,WConio
4
5helpmsg="""usage: xltgo texfile [options]
6options:
7 -showpdf: show pdf after compiled
8 -encrypt: encrtypt pdf
9e.g: xltgo youfile
10 xltgo youfile.tex -showpdf -encrypt"""
11
12def printgoodnews(message):
13 global attr
14 WConio.textcolor(WConio.GREEN)
15 total_chars=80
16 ll=len(message)
17 start=(total_chars-ll)/2
18 print "-"*start+message+"-"*(80-start-ll)
19 WConio.textcolor(attr)
20
21def printbadnews(message):
22 global attr
23 WConio.textcolor(WConio.GREEN)
24 total_chars=80
25 ll=len(message)
26 start=(total_chars-ll)/2
27 print "-"*start+message+"-"*(80-start-ll)
28 WConio.textcolor(attr)
29
30def cleartmpfiles(file):
31 dir=os.path.dirname(file)
32 filename=os.path.basename(file)
33 if (filename[-4:]).upper()=='.TEX':
34 filename=filename[:-4]
35 tmpexts=['.aux','.log','.nav','.out','.snm','.toc','vrb','.xdv']
36
37 for ext in tmpexts:
38 tmpfile=os.path.join(dir,filename)+ext
39 #print tmpfile
40 if os.path.exists(tmpfile):
41 print "Deleting %s" % tmpfile
42 os.unlink(tmpfile)
43
44if __name__=='__main__':
45 attr=WConio.gettextinfo()[4]
46 if len(sys.argv)==1:
47 print helpmsg
48 if len(sys.argv)>1 :
49 options=[option.upper() for option in sys.argv[1:]]
50
51 exist=False
52 out_dir=os.path.dirname(os.path.abspath(sys.argv[1]))
53 filename=os.path.abspath(sys.argv[1])
54 filename_noext=filename
55 if os.path.abspath(sys.argv[1])[-4:].upper()=='.TEX':
56 filename_noext=sys.argv[1][:-4]
57 if os.path.abspath(sys.argv[1])[-4:].upper()=='.TEX' :
58 exist= os.path.exists(filename)
59 else:
60 exist= os.path.exists(filename_noext+".tex")
61
62 if exist:
63 printgoodnews('xelatex -halt-on-error -no-pdf -output-directory="%s" "%s"' %(out_dir,filename))
64 i=-1
65 j=-1
66 k=-1
67 i=os.system('xelatex -halt-on-error -no-pdf -output-directory="%s" "%s"' %(out_dir, filename))
68 if i==0:
69 printgoodnews("The xelatex 1st run finished!")
70 j=os.system('xelatex -halt-on-error -no-pdf -output-directory="%s" "%s"' %(out_dir, filename))
71 if j==0:
72 printgoodnews("The xelatex 2nd run finished!")
73
74 if '-ENCRYPT' in options:
75 k=os.system('xdvipdfmx -r 600 -E -V 5 -S -K 128 -P 0x0804 -o "%s.pdf" "%s.xdv"' % (filename_noext,filename_noext))
76 else:
77 k=os.system('xdvipdfmx -r 600 -E -V 5 -o "%s.pdf" "%s.xdv"' % (filename_noext,filename_noext))
78
79 if k==0:
80 printgoodnews("xdvipdfmx finished!")
81 if '-SHOWPDF' in options:
82 k=os.startfile(filename_noext+".pdf")
83 printgoodnews("Command 'showpdf' was executed!")
84 cleartmpfiles(sys.argv[1])
85 printgoodnews("All finished!")
86 sys.exit(0)
87 else:
88 printbadnews("Error on xdvipdfmx!")
89 sys.exit(-1)
90 else:
91 printbadnews("Error on the 2rd run !")
92 cleartmpfiles(sys.argv[1])
93 sys.exit(-1)
94 else:
95 printbadnews("Error on the 1st run !")
96 cleartmpfiles(sys.argv[1])
97 sys.exit(-1)
98 else:
99 printbadnews( "Error: where is your tex file %s?'"%sys.argv[1])
100 sys.exit(-1)
101
102