使用Perl脚本编译Latex
脚本能实现Latex文本的初级编译,并将生成的中间文件移动到同一个目录
调用方法
chmod +x xelatex2pdf.pl
xelatex2pdf.pl -n 2 -f test.tex # 将test.tex编译两次
Perl 代码
脚本名:xelatex2pdf.pl
#!/usr/bin/perl
use strict;
use warnings;
use File::Path;
use File::Copy;
use File::Find;
use Cwd;
my $Has_Help = "";
my $comp_run = 1;
my $texfile = "";
my $def_timers = "";
my $def_tex = "";
my $run_path = getcwd;
my $log_path = "$run_path"."/log";
#print("$run_path
");
#print("$log_path
");
#------------------------------------------------
# 主函数
#------------------------------------------------
if( $#ARGV < 0 ) {
&print_usage;
exit;
}
my $filename ="";
&parse_argv;
if( $Has_Help =~ /TRUE/ ) #显示帮助说明
{
&print_usage;
exit;
}
&build;
&clean;
#------------------------------------------------
# 函数结束
#------------------------------------------------
#################################################
# 处理编译文件
#################################################
sub build{
for(my $i=0; $i<$comp_run; $i++) {
if($i==1){
my $bibfile = $texfile;
$bibfile =~ s/.tex/.aux/;
system("bibtex $bibfile");
}
system("xelatex $texfile");
}
my $pdf_name = $texfile;
$pdf_name =~ s/.tex$/.pdf/;
#print("$pdf_name
");
system("evince $pdf_name &");
}
#################################################
# 删除中间文件
#################################################
sub wanted {
my $findfile = $File::Find::name;
if($findfile =~ /log//){
#print("Next...
"); #跳过 log/ 否则移动会出错
}
elsif ( $findfile =~ /.aux|.lof|.out|.toc|.log|.lot|.ilg|..bbl|.blg|.gz/ ) {
# 得到绝对路径
$findfile =~ s/^.//;
$findfile = "$run_path"."$findfile";
print("move $findfile to log/
");
#print("
findfile = $findfile
");
#print("log_path = $log_path
");
move("$findfile","$log_path")||warn "could not move files :$!" ; # 移动使用绝对路径,避免出错
}
}
sub clean{
my $folder = "./log";
if(-e $folder){
rmtree("log/");
}
mkdir $folder;
my $local_file = ".";
find(&wanted, $local_file);
}
#################################################
# Sub-routine: print_usage() 帮助说明
#################################################
sub print_usage {
print "
Usage: $0 -n <compile timers> -f <tex file> \
";
print " [-n <compile timers> -f <tex file>] \
";
print " [-h]
";
print "For example:
";
print " $0 -n 2 -f main.tex
";
print " $0 -h
";
print "
";
}
#################################################
# Sub-routine : parse_argv() 参数读入
#################################################
sub parse_argv {
my $all_arg = "-h|-n|-f";
for(my $i=0; $i<=$#ARGV; $i++) {
if( $ARGV[$i] =~ /-n/ ) {
$i++;
if(!defined $ARGV[$i])
{
$Has_Help = "TRUE";
}
$def_timers = "TRUE";
$comp_run = $ARGV[$i];
}
elsif( $ARGV[$i] =~ /-f/ ) {
$i++;
if(!defined $ARGV[$i])
{
$Has_Help = "TRUE";
}
$def_tex = "TRUE";
$texfile = $ARGV[$i];
}
elsif( $ARGV[$i] =~ /-h/ ) {
$Has_Help = "TRUE";
}
else { ### other options
$Has_Help = "TRUE";
}
}
}
Latex测试代码
文件名: test.tex
documentclass{ctexart}
egin{document}
Hello LaTeX!
你好,世界!
clearpage
这是一个测试
section{章标题}
这是章的介绍
subsection{节的标题}
这是节的介绍
subsubsection{子节的标题}
这是子节的介绍 \
数学公式的测试:\
Huge{$y=2^x+frac{4}{6^z}$}
footnote{这仍然是个测试,一个脚注}
ewpage
end{document}
参考资料
[1].bash脚本编译Latex
[2].Perl 获得当前路径
[3].Perl遍历查找文件