使用perl创建latex pdf

时间:2013-01-17 13:34:06

标签: perl pdf latex

enter image description here我正在尝试根据从我们的数据库中获取的统计信息自动生成报告。我使用perl与数据库进行通信,并将有趣的变量存储为标量/数组等。然后我想在使用pdflatex之前创建一个latex文件。但是,我在尝试将enter image description here标量值传递到pdf时遇到了一些问题。例如,请参阅下面的代码和输出

#!/usr/bin/perl
#
#
# Description: Write a monthly report from the database using latex
#    
# Parameters:
#
# History: 
#
# CVS ID: $Date: 2012/03/01 16:13:03 $ $Revision: 1.1 $
########################################################################

#Add library
use lib "$ENV{HOME}/perllib";

#Call modules
use DBI;                     # Postgres communication functions
use File::Temp qw/tempfile/;
use File::Copy;
use Cwd;

my $date_start = '01/12/12';
my $date_stop  = '31/12/12';


%hash=("01"=>"Jan","02"=>"Feb","03"=>"Mar","04"=>"Apr","05"=>"May","06"=>"Jun","07"=>"Jul","08"=>"Aug","09"=>"Sep","10"=>"Oct","11"=>"Nov","12"=>"Dec");
$date_start=~/([0-9]{2})\/([0-9]{2})\/([0-9]{2}).*/;
my $date_string = "$hash{$2}$3";
my $report_name = "$date_string"."_monthly_report.pdf";



#Pragmas

use warnings;
no warnings "uninitialized";  # Don't warn about unitialized variables#
use strict 'vars';          # Force all variables to have defined scope

my $repdir = "/home/nm/Desktop/";
my ($fh, $filename) = tempfile( SUFFIX => '.tex', DIR => $repdir);

# LaTeX Header information
print $fh <<'END';
\documentclass{article}
\usepackage{underscore}
\usepackage{fullpage}
\usepackage{multicol}
\usepackage[margin=0.5in]{geometry}
\usepackage{graphicx}
\usepackage{array}

\newenvironment{nstabbing}
  {\setlength{\topsep}{0pt}%
   \setlength{\partopsep}{0pt}%
   \tabbing}
  {\endtabbing}

\begin{document}


\begin{center}
\LARGE \bf MONTHLY SUMMARY
\end{center}


END

my $blank = " ";
# Body of LaTeX document
print $fh <<END;
This report details clinical and imaging statistics from $date_start to $date_stop

END
print $fh <<'END';
This report details clinical and imaging statistics from $date_start to $date_stop


\large \bf Clinical Statistics

\normalsize \rm 

\begin{table}[h]
\centering
\begin{tabular}{cccc} 
\hline
Task & AR & DB & GM \\
Cannulation & 1 & 2 & 3 \\
\hline
\end{tabular}
\end{table}

END

# Closing of LaTeX document
print $fh <<'END';
\end{document}
END

# Run LaTeX compiler to generate PDF
system('pdflatex -output-directory ' . $repdir . ' ' . $filename);
my $file_prefix = substr($filename, 0, -4);

#remove aux, log and tex files
my $auxfile = $file_prefix . ".aux";
my $logfile = $file_prefix . ".log";
my $pdffile = $file_prefix . ".pdf";

system('rm ' . $auxfile . ' ' .  $logfile . ' ' . $filename);
$filename="$repdir$report_name";
move($pdffile,$filename);

system('acroread ' . $filename);

为什么

print $fh <<END;
This report details clinical and imaging statistics from $date_start to $date_stop
END

给出正确的输出,而以下没有?

print $fh <<'END';
This report details clinical and imaging statistics from $date_start to $date_stop
END

我尝试使用前者在显示器上创建表格,但我无法以一种不错的格式获得它,例如它会创建一个额外的列,并在通用汽车旁边放置插管。此外,当使用前者时,我必须放置一个额外/之前的乳胶关键字,例如// bf而不是/ bf,而在后者('END')我不需要。

理想情况下,我更喜欢使用'END'作为我的表格和乳胶格式按预期工作。但是,我无法传递标量和数组值。我该如何管理?

1 个答案:

答案 0 :(得分:3)

'END'周围的单引号表示您的here文档被视为单引号字符串:不会在其中插入变量。

默认情况下将其视为双引号字符串,这就是您的第一个示例有效的原因。您还可以使用双引号明确指定您希望它像双引号字符串:<<"END";(这类似于默认行为,但可能更清晰)。

使用插值(双引号)字符串的一个必然结果是你必须转义像反斜杠这样的东西 - 反斜杠在这种字符串中有特殊含义。你真的必须选择一个或另一个:一个插入变量并需要转义的字符串,或一个不插入且不需要转义的字符串。