使用 Perl 从 bibtex 自动生成报告

使用 Perl 从 bibtex 自动生成报告

我喜欢利用现有myarticles.bib文件自动创建不同的报告,如下图所示(我使用 tikz 准备了此模板)。

最好的方法是什么?

在此处输入图片描述

答案1

我按照以下步骤解决了我的问题。这不是一个明确的tex解决方案,可能不应该在这里,但无论如何它都是有效的。

我尝试使用perltex,发现了一些有趣的错误。

我的解决方案

准备

我准备了一个 perl 脚本,

  • 找到所需的参考资料和属于该资料的信息,
  • 解析成变量,
  • 并创建包含我的 tikz 模板的 bibREF.tex 文件。

Latex 内部

我按照以下步骤让它在乳胶内部运行;

  1. 我手动调用 perl 脚本来查看每个引用 \immediate\write18{./perl.pl test2013 > scriptoutput.tex}
  2. 我使用以下代码调用 bibREF.tex\input \input{bibREF}
  3. 每次我想添加新引用时,请执行步骤 3-4

平均能量损失

\documentclass[12pt]{report}
\usepackage{hyperref}  %nice to have for link,url, cross-references etc.   
\usepackage{xcolor}    %brings the colors
\usepackage[mode=buildnew]{standalone} %magical package to call standalone document class, ignoring their preamble    
\usepackage[a4paper]{geometry}  %margin tricks
\usepackage{tikz}
\usetikzlibrary{backgrounds,calc,positioning}
\thispagestyle{empty}

\begin{document}

\newgeometry{left=2cm,bottom=1.5cm,top=1.5cm}
  \centering
  \immediate\write18{./perl.pl test2013 > scriptoutput.tex}
  \input{bibREF}
  % \restoregeometry

  % \newgeometry{left=2cm,bottom=1.5cm,top=1.5cm}
  \centering
  \immediate\write18{./perl.pl Wang2013 > scriptoutput.tex}
  \input{bibREF}
\restoregeometry

\end{document}

Perl 脚本

(这真的很糟糕,我 24 小时前就开始使用 perl 了)

#!/usr/bin/perl 
#
# Strict and warnings are recommended.
use strict;
use warnings;
  no warnings "uninitialized";  # Don't warn about unitialized variables#
use Term::ANSIColor; #Some fun
# use 5.010;

# print colored ['bold blue'], "Hey I am a Perl,\n";
# print "I can find all the informations you need for a specific reference from regular";
# print colored ['bold blue'], " bibtex ";
# print "file\n";


my $in = 0;                                                  #logical to check am I in a good line or not
my $refPattern='Wang2013';                                   #which reference I am trying to read
# my $refPattern='test2013'; 
# my $refPattern='',"$_[0]",'';                                   #which reference I am trying to read
my $filename = 'article.bib';                                #bibtex file storing informations

foreach (@ARGV) {
    $refPattern = $_;
};

# print "My variable $refPattern\n";


#possible to improve with list or hash, find them with forearch etc. (did not work yet)
my $abstract  ;                                  #lesson:perl scope has crazy/sensible way
my $author    ;                                  #lesson:perl scope has crazy/sensible way
my $doi       ;                                  #lesson:perl scope has crazy/sensible way
my $issn      ;                                  #lesson:perl scope has crazy/sensible way
my $journal   ;                                  #lesson:perl scope has crazy/sensible way
my $keywords  ;                                  #lesson:perl scope has crazy/sensible way
my $localfile ;                                  #lesson:perl scope has crazy/sensible way
my $note      ;                                  #lesson:perl scope has crazy/sensible way
my $number    ;                                  #lesson:perl scope has crazy/sensible way
my $pages     ;                                  #lesson:perl scope has crazy/sensible way
my $title     ;                                  #lesson:perl scope has crazy/sensible way
my $url       ;                                  #lesson:perl scope has crazy/sensible way
my $volume    ;                                  #lesson:perl scope has crazy/sensible way
my $year      ;                                  #lesson:perl scope has crazy/sensible way
# my %hash = ( 'abstract','author','journal','keywords','localfile','note','title','url');

my $line;                                        #raw line
my $field;                                       #field name
my $content_temp;                                #raw content, without trims
my $content   ;                                  #content for all fields, with trims


open(my $bibfile, '<:encoding(UTF-8)', $filename)
  or die "Could not open file '$filename' $!";

while (<$bibfile>) {                                         #remember perl default variable is in use, $_
    $in = 1 if /$refPattern/i;
    last if (/^$/ && $in == 1);                              #break the while on the first following empty line after $refPattern
    if ($in == 1) {
      $line = $_ if($in);
      $field = substr($line, 1, index($line, ' = '));        #it finds bibtex vartype
      $field =~ s/^\s+|\s+$//g;                       #trim both ends, may be not necessary, do not know how to check if there are spaces around the $field
      print "$field\n";
      $line =~ s/^\s*\S+\s*//;                               #split field and content, field is the first word always
      $content_temp = $line; 
      $content_temp =~ s/^\s+|\s+$//g;                       #trim both ends
      $content_temp =~ s/^\s*\S+\s*//;                       #like split field and content, to delete = on the beginning
      $content_temp =~ s/,\z//;                              #trim last character "," if exist
      next if (length($content_temp) < 1);                   #Check if you really read something
      $content = substr($content_temp, 1, -1);               #If you know the first and last character are quotes, use it, no need to regec



#       print "$content\n";
      $abstract  = $content if ($field eq 'abstract');
      $author    = $content if ($field eq 'author');
      $doi       = $content if ($field eq 'doi');
      $issn      = $content if ($field eq 'issn');
      $journal   = $content if ($field eq 'journal');
      $keywords  = $content if ($field eq 'keywords');
      $localfile = $content if ($field eq 'localfile');
      $note      = $content if ($field eq 'note');
      $number    = $content if ($field eq 'number');
      $pages     = $content if ($field eq 'pages');
      $title     = $content if ($field eq 'title');
      $url       = $content if ($field eq 'url');
      $volume    = $content if ($field eq 'volume');
      $year      = $content if ($field eq 'year');           
#       print "$title\n";
    }                          
}

# print "$journal\n";
# print "$content\n";
# print "$title\n";

my $filename = 'bibREF.tex';
open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
print $fh '%HEYY This is generated by perl.pl, do not touch it, if you need changes go to mother file!!!                                                                             ',"\n";                                                                                                                                   
print $fh '\documentclass[12pt]{standalone}                                                                                                                                          ',"\n";
print $fh '\usepackage{amsmath}                                                                                                                                                      ',"\n";
print $fh '\usepackage{amssymb}                                                                                                                                                      ',"\n";
print $fh '\usepackage{tikz}                                                                                                                                                         ',"\n";
print $fh '\usepackage{gnuplot-lua-tikz}                                                                                                                                             ',"\n";
print $fh '\usepackage[shell]{gnuplottex}                                                                                                                                            ',"\n";
print $fh '\usepackage[]{tcolorbox}                                                                                                                                                  ',"\n";
print $fh '\usetikzlibrary{backgrounds,calc,positioning}                                                                                                                             ',"\n";
print $fh '\thispagestyle{empty}                                                                                                                                                     ',"\n";
print $fh '                                                                                                                                                                          ',"\n";
print $fh '\begin{document}                                                                                                                                                          ',"\n";
print $fh '                                                                                                                                                                          ',"\n";
print $fh '\begin{tikzpicture}[inner sep=0pt]                                                                                                                                        ',"\n";
print $fh '% block/.style={draw,fill=white,rectangle, minimum width={width("Magnetometer")+2pt},font=\small},                                                                        ',"\n";
print $fh '% blockl/.style={draw,fill=white,rectangle, minimum width={400pt},font=\large}]                                                                                           ',"\n";
print $fh '\tikzstyle{styBIBR}  = [draw,fill=black!60,minimum height={30pt},rectangle,text width=5.5cm   ,text centered,font=\bf,text=white,font=\huge];                             ',"\n";
print $fh '\tikzstyle{styYEAR}  = [draw,fill=blue!60,minimum height={20pt},rectangle,text width=5.5cm   ,text centered,font=\bf,text=white];                                         ',"\n";
print $fh '\tikzstyle{styTITLE}  = [draw,,rectangle,minimum height={60pt},text width=11.5cm,                            ,font=\bf, font=\Large];                                     ',"\n";
print $fh '\tikzstyle{styATHR}  = [draw,,rectangle,minimum height={30pt},text width=17cm,                            , font=\large];                                                 ',"\n";
print $fh '\tikzstyle{styJRNL}  = [draw,,minimum height={30pt},rectangle                       ,text width=5.5cm   ,text centered,text=black,font=\small];                           ',"\n";
print $fh '\tikzstyle{styKWRD}  = [draw,rectangle,minimum height={30pt},text width=17cm,                            ,font=\bf, font=\small];                                         ',"\n";
print $fh '\tikzstyle{styLCLF}  = [draw,rectangle,minimum height={10pt},text width=17cm,                             text=blue, font=\footnotesize];  %local file                    ',"\n";
print $fh '\tikzstyle{styURL}  = [draw,fill=blue!20,rectangle,minimum height={20pt},text width=11.5cm,                            ,font=\bf, font=\Large];  %local file              ',"\n";
print $fh '\tikzstyle{styNOTES} = [draw,rectangle,minimum height={436pt},text width=17cm,];                                                                                          ',"\n";
print $fh '\tikzstyle{styTODO} = [draw,rectangle,minimum height={110pt},text width=17cm];                                                                                            ',"\n";
print $fh '\tikzstyle{styCTD} = [draw,rectangle,minimum height={90pt},text width=8.5cm];                                                                                             ',"\n";
print $fh '\draw [draw,use as bounding box] (0cm,0cm) rectangle (17cm, 27cm);                                                                                                        ',"\n";
print $fh '                                                                                                                                                                          ',"\n";
print $fh '                                                                                                                                                                          ',"\n";
print $fh '                                                                                                                                                                          ',"\n";
print $fh '%%%%%%%%%%%%%%%                                                                                                                                                           ',"\n";
print $fh '%top group                                                                                                                                                                ',"\n";
print $fh '%%%%%%%%%%%%%%%                                                                                                                                                           ',"\n";
print $fh '  \node[styTITLE,left=0pt of current bounding box.north west, ,anchor=north west](nodeTITLE) {',"$title",'};                                                              ',"\n";
print $fh '  \node[styATHR,left=0pt of nodeTITLE.south west, anchor=north west](nodeATHR) {',"$refPattern",'};                                                                       ',"\n";
print $fh '  \node[styKWRD,left=0pt of nodeATHR.south, anchor=north](nodeKWRD) {Keywords: ',"$author",'};                                                                            ',"\n";
print $fh '  \node[styBIBR,left=0pt of nodeTITLE.north east, anchor=north west] (nodeBIBR) {',"$refPattern",'};                                                                      ',"\n";
print $fh '\node[styJRNL,left=0pt of nodeBIBR.south, anchor=north] (nodeJRNL) {',"$journal",'};                                                                                      ',"\n";
print $fh '                                                                                                                                                                          ',"\n";
print $fh '                                                                                                                                                                          ',"\n";
print $fh '%%%%%%%%%%%%%%%                                                                                                                                                           ',"\n";
print $fh '%bottom group                                                                                                                                                             ',"\n";
print $fh '%%%%%%%%%%%%%%%                                                                                                                                                           ',"\n";
print $fh '%local file link                                                                                                                                                          ',"\n";
print $fh '\node[styLCLF,left=0pt of current bounding box.south west,anchor=south west](nodeLCLF) {localfile link: ',"$refPattern",'};                                               ',"\n";
print $fh '\node[styCTD,left=0pt of nodeLCLF.north west,anchor=south west](nodeCTD) {\textbf{Cited:} ',"$refPattern",' };                                                            ',"\n";
print $fh '\node[styCTD,left=0pt of nodeLCLF.north east,anchor=south east](nodeCTDBY) {\textbf{Cited by:} ',"$refPattern",'};                                                        ',"\n";
print $fh '\node[styTODO,left=0pt of nodeCTD.north west,anchor=south west](nodeCTD) {\textbf{TODO:} ',"$refPattern",' };                                                             ',"\n";
print $fh '                                                                                                                                                                          ',"\n";
print $fh '%%%%%%%%%%%%%%%                                                                                                                                                           ',"\n";
print $fh '%mid group                                                                                                                                                                ',"\n";
print $fh '%%%%%%%%%%%%%%%                                                                                                                                                           ',"\n";
print $fh ' \node[styNOTES,below=0.pt of nodeKWRD](NOTES) {NOTES:};                                                                                                                  ',"\n";
print $fh '                                                                                                                                                                          ',"\n";
print $fh '\end{tikzpicture}                                                                                                                                                         ',"\n";
print $fh '                                                                                                                                                                          ',"\n";
print $fh '\end{document}                                                                                                                                                            ',"\n";

close $fh;                                                                                                                                    
print "done\n";

相关内容