使用Perl脚本编译Latex
使用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的更多相关文章
- 把perl脚本编译成exe
来源:http://www.veryhuo.com/a/view/38338.html 今天想把 perl 脚本编译成 exe 以便脱离 perl 解释器独立运行.都可以生成PERL的PE文件,在PE ...
- 【转】FTP自动上传文件的perl脚本以及配置文件
这个perl文件将执行的任务是, 按照指定的文件夹目录,自动将该文件夹下的所有文件上传到指定ftp站点的指定目录下 本应用程序设计的几个基本理念是:工具箱再利用:尽可能利用已有的工具:简化运行步骤:不 ...
- perl 脚本测试
原文地址: http://blog.csdn.net/johnny710vip/article/details/8905239 这是一篇关于perl脚本测试的总结性文章,其中提到了很多实用的 ...
- perl静态编译DBD
编译DBD 项目中经常使用perl,但perl在连接数据库时,需要依赖DBI,DBD驱动,但默认安装DBD驱动时,需要依赖数据库的lib库. 比如perl连接MySQL,需要安装MySQL clien ...
- C#怎么在生成解决方案的过程中执行perl脚本(C#早期绑定)
转载 怎么在生成解决方案的过程中执行perl脚本 早期绑定在编译期间识别并检查方法.属性.函数,并在应用程序执行之前执行其他优化.在这个绑定中,编译器已经知道它是什么类型的对象以及它拥有的方法或属性. ...
- crontab不执行perl脚本分析
在新装的Linux服务器上部署了一个作业监控磁盘空间并提前告警,在shell脚本里面调用了一个perl脚本发送告警邮件.结果出现了一个很奇怪的现象:如果手工执行该脚本/home/oracle/scri ...
- unity3d 特殊文件夹和脚本编译顺序
unity3d 特殊文件夹和脚本编译顺序 转自http://blog.csdn.net/u010019717/article/details/40474631 大多数情况下,您可以选择任何你喜欢的文件 ...
- 转:Android开发实践:用脚本编译Android工程
转自: http://ticktick.blog.51cto.com/823160/1365947 一般情况下,我们都是使用Eclipse+ADT插件或者Android studio软件来编译Andr ...
- linux 全自动提权 exp perl脚本
linux 全自动提权 exp perl脚本 作者: admin 日期: 2013/01/19发表评论 (0) 查看评论 国外流传过来的 地址 http://dl.packetstormsecur ...
随机推荐
- js常用代码示例及解决跨域的几种方法
1.阻止默认行为 // 原生js document.getElementById('btn').addEventListener('click', function (event) { event = ...
- pstree---树状图的方式展现进程
pstree命令以树状图的方式展现进程之间的派生关系,显示效果比较直观. 语法 pstree(选项) 选项 -a:显示每个程序的完整指令,包含路径,参数或是常驻服务的标示: -c:不使用精简标示法: ...
- 洛谷 P2437 蜜蜂路线
P2437 蜜蜂路线 题目描述 一只蜜蜂在下图所示的数字蜂房上爬动,已知它只能从标号小的蜂房爬到标号大的相邻蜂房,现在问你:蜜蜂从蜂房M开始爬到蜂房N,M<N,有多少种爬行路线? 输入输出格式 ...
- python ATM机 案例代码
利用目前学的流程控制写的 ''' ATM机 需求: 1.登陆 输入账号输入密码 每日只有3次登陆密码错误的机会,超过3次禁止登陆 2.查询余额 3.存款 4.取款 5.转帐 6.退出 ''' info ...
- Spring学习总结(10)——Spring JMS---三种消息监听器
消息监听器MessageListener 在spring整合JMS的应用中我们在定义消息监听器的时候一共可以定义三种类型的消息监听器,分别是MessageListener.SessionAwareMe ...
- 《ASP.NET》数据绑定—DropDownList、ListBox
DropDownList和ListBox实现两级联动功能.他们也能够将从后台数据库中搜选的出来的信息加以绑定.这里要实现的功能是在DropDownList中选择"省",然后让Lis ...
- 解决iOS空指针数据的问题
iOS开发中常常会遇到空指针的问题. 如从后台传回来的Json数据,程序中不做推断就直接赋值操作,非常有可能出现崩溃闪退. 为了解决空指针的问题,治标的方法就是遇到一个处理一个.这样业务代码里面就插了 ...
- Android Studio运行报错,Cannot find System Java Compiler. Ensure that you have installed a JDK......
详细报错信息如下 Error:Execution failed for task ':app:compileDebugJavaWithJavac'. > Cannot find System J ...
- upf用到的工具
emulator : PXP zebu simulator :
- 自己定义progressdialog载入动画,这里还有旋转的载入条,美团,多个图片动画
自己定义progressdialog载入动画,这里还有旋转的载入条,美团,多个图片动画 下载Demo:http://download.csdn.net/detail/menglele1314/8775 ...