使用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\n");
#print("$log_path\n"); #------------------------------------------------
# 主函数
#------------------------------------------------
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\n");
system("evince $pdf_name &");
} #################################################
# 删除中间文件
#################################################
sub wanted {
my $findfile = $File::Find::name;
if($findfile =~ /log\//){
#print("Next...\n"); #跳过 log/ 否则移动会出错
}
elsif ( $findfile =~ /\.aux|\.lof|\.out|\.toc|\.log|\.lot|\.ilg|.\.bbl|.blg|\.gz/ ) { # 得到绝对路径
$findfile =~ s/^\.//;
$findfile = "$run_path"."$findfile";
print("move $findfile to log/\n"); #print("\nfindfile = $findfile\n");
#print("log_path = $log_path\n");
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 "\nUsage: $0 -n <compile timers> -f <tex file> \\\n";
print " [-n <compile timers> -f <tex file>] \\\n";
print " [-h] \n\n";
print "For example:\n";
print " $0 -n 2 -f main.tex\n";
print " $0 -h \n";
print "\n";
} #################################################
# 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}
\begin{document}
Hello \LaTeX! 你好,世界! \clearpage 这是一个测试
\section{章标题}
这是章的介绍
\subsection{节的标题}
这是节的介绍
\subsubsection{子节的标题}
这是子节的介绍 \\
数学公式的测试:\\
\Huge{$y=2^x+\frac{4}{6^z}$}
\footnote{这仍然是个测试,一个脚注} \newpage \end{document}

参考资料


[1].bash脚本编译Latex

[2].Perl 获得当前路径

[3].Perl遍历查找文件

使用Perl脚本编译Latex的更多相关文章

  1. 把perl脚本编译成exe

    来源:http://www.veryhuo.com/a/view/38338.html 今天想把 perl 脚本编译成 exe 以便脱离 perl 解释器独立运行.都可以生成PERL的PE文件,在PE ...

  2. 【转】FTP自动上传文件的perl脚本以及配置文件

    这个perl文件将执行的任务是, 按照指定的文件夹目录,自动将该文件夹下的所有文件上传到指定ftp站点的指定目录下 本应用程序设计的几个基本理念是:工具箱再利用:尽可能利用已有的工具:简化运行步骤:不 ...

  3. perl 脚本测试

      原文地址:  http://blog.csdn.net/johnny710vip/article/details/8905239   这是一篇关于perl脚本测试的总结性文章,其中提到了很多实用的 ...

  4. perl静态编译DBD

    编译DBD 项目中经常使用perl,但perl在连接数据库时,需要依赖DBI,DBD驱动,但默认安装DBD驱动时,需要依赖数据库的lib库. 比如perl连接MySQL,需要安装MySQL clien ...

  5. C#怎么在生成解决方案的过程中执行perl脚本(C#早期绑定)

    转载 怎么在生成解决方案的过程中执行perl脚本 早期绑定在编译期间识别并检查方法.属性.函数,并在应用程序执行之前执行其他优化.在这个绑定中,编译器已经知道它是什么类型的对象以及它拥有的方法或属性. ...

  6. crontab不执行perl脚本分析

    在新装的Linux服务器上部署了一个作业监控磁盘空间并提前告警,在shell脚本里面调用了一个perl脚本发送告警邮件.结果出现了一个很奇怪的现象:如果手工执行该脚本/home/oracle/scri ...

  7. unity3d 特殊文件夹和脚本编译顺序

    unity3d 特殊文件夹和脚本编译顺序 转自http://blog.csdn.net/u010019717/article/details/40474631 大多数情况下,您可以选择任何你喜欢的文件 ...

  8. 转:Android开发实践:用脚本编译Android工程

    转自: http://ticktick.blog.51cto.com/823160/1365947 一般情况下,我们都是使用Eclipse+ADT插件或者Android studio软件来编译Andr ...

  9. linux 全自动提权 exp perl脚本

    linux 全自动提权 exp perl脚本 作者: admin 日期: 2013/01/19发表评论 (0) 查看评论   国外流传过来的 地址 http://dl.packetstormsecur ...

随机推荐

  1. 经典的横线中间文字css布局---flex布局

    html: <div class="title"> <div class="line"></div> <div cla ...

  2. HDU 5188 zhx and contest(带限制条件的 01背包)

    Problem Description As one of the most powerful brushes in the world, zhx usually takes part in all ...

  3. 使用wpaint绘图软件时:Uncaught DOMException: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The image argument is a canvas element with a width or height of 0.

    使用wpaint绘图软件时:Uncaught DOMException: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': Th ...

  4. Qt之界面换肤

    简述 常用的软件基本都有换肤功能,例如:QQ.360.迅雷等.换肤其实很简单,并没有想象中那么难,利用前面分享过的QSS系列文章,沃我们完全可以实现各种样式的定制! 简述 实现原理 效果 新建QSS文 ...

  5. 理性分析 C++(-O2) 和 JS 的性能差距

    laptop: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz.. Test1: 最后一行:时间(ms) #pragma GCC optimize("O2& ...

  6. Unix下后门查找{上}

    本文出自 "李晨光原创技术博客" 博客,请务必保留此出处http://chenguang.blog.51cto.com/350944/683699

  7. C#异步编程的实现方式(4)——Task任务

    最基本的是知道怎么启动一个Task. 1.Task类构造函数 使用Task类的构造函数.实例化Task对象时,任务不会立即运行,而是指定Created状态.接着调用Task类的Start()方法来启动 ...

  8. windows7下安装Office2010提示需要安装MSXML6.10.1129

    平台:Windows 7 问题:刚刚下载的ghost Win 7,安装过程一切顺利,进入系统后把集成的软件全部卸载,清理完垃圾,安装了VC库,在安装Office2010时提示需要安装MSXML6.10 ...

  9. CISP/CISA 每日一题 17

     CISSP 每日一题(答) What are often added to passwords to maketheir resultant hash secure and resistant to ...

  10. 用c实现的各种排序的方法

    #include <stdio.h> void swap(int *a, int *b); void bubble_sort(int a[], int n); void select_so ...