use Tk; use warnings; use strict; use Template; my ( $file, $title, $maketitle, $author ); my ( $type, $fontSizeCheckButton, $geometry ); my ( $pdftex, $paper, $font, $fontSize ); my ( @spinbox, $spinbox, $sizeSpin, $i ); my ( $frameIncludes, $margin, %margin, $includes, %includes ); my ( $contents, $frontmatter, $backmatter, $mainmatter, $citeExample); $i = 0; my $mw = MainWindow -> new; $mw -> bind('' => sub { exit } ); # use escape key to exit # **************** M A I N D E T A I L S F R A M E **************** my $frameMain = $mw -> Frame( -label => 'MAIN DETAILS', -borderwidth => 2, -relief => 'groove') -> pack(-side => 'top', -fill => 'x'); $frameMain -> Label( -text => 'LaTeX filename') -> pack(-side => 'left'); my $entryFile = $frameMain -> Entry( -textvariable => \$file) -> pack(-side => 'left', -fill => 'x', -expand => 1); $entryFile -> insert('end', "default.tex"); $entryFile -> focus; $frameMain -> Label( -text => 'Title') -> pack(-side => 'left'); my $entryTitle = $frameMain -> Entry( -textvariable => \$title) -> pack(-side => 'left', -fill => 'x', -expand => 1); $frameMain -> Label( -text => 'Author') -> pack(-side => 'left'); my $entryAuthor = $frameMain -> Entry( -textvariable => \$author) -> pack(-side => 'left', -fill => 'x', -expand => 1); # *************** D O C U M E N T C L A S S ***************** my $frameDocClass = $mw -> Frame( -label => 'DOCUMENT CLASS', -borderwidth => 2, -relief => 'groove') -> pack(-side => 'top', -fill => 'x'); my $frameType = $frameDocClass -> Frame(-label => 'TYPE', -borderwidth => 2, -relief => 'groove'); my $framePaper = $frameDocClass -> Frame(-label => 'PAPER (uncheck = default)', -borderwidth => 2, -relief => 'groove'); my $frameFont = $frameDocClass -> Frame(-label => 'FONT SIZE', -borderwidth => 2, -relief => 'groove'); $frameDocClass -> Checkbutton( -text => 'use pdf', -variable => \$pdftex, -onvalue => "pdftex, ", -offvalue => "", ) -> pack(-side => 'left'); foreach (qw/article book report/) { $frameType -> Radiobutton( -text => $_, -value => $_, -variable => \$type) -> pack(-side => 'left'); } foreach (qw/a4paper letter/) { $framePaper -> Radiobutton( -text => $_, -value => $_.", ", -variable => \$paper) -> pack(-side => 'left'); } $frameFont -> Checkbutton( -text => 'customize', -variable => \$fontSizeCheckButton, -onvalue => "on", -offvalue => "off", -command => sub { if ($fontSizeCheckButton eq "on") { $sizeSpin -> configure(-state => 'normal') } else { $sizeSpin -> configure(-state => 'disabled') } } ) -> pack(-side => 'left'); $frameFont -> Label ( -text => '') -> pack(-side => 'left'); $sizeSpin = $frameFont -> Spinbox( -width => 6, -from => 10, -to => 14, -textvariable => \$fontSize, -state => 'disabled') -> pack(-side => 'left'); # ************************ G E O M E T R Y *************************** my $frameGeometry = $mw -> Frame; $frameGeometry = $mw -> Frame( -label => 'GEOMETRY', -borderwidth => 2, -relief => 'groove') -> pack(-side => 'top', -fill => 'x'); $frameGeometry -> Checkbutton( -text => 'customize', -variable => \$geometry, -onvalue => "on", -offvalue => "off", -command => sub { foreach (@spinbox) { if ($geometry eq "on") { $_ -> configure(-state => 'normal') } else { $_ -> configure(-state => 'disabled') } } } ) -> pack(-side => 'left'); foreach (qw/left right top bottom/) { $frameGeometry -> Label ( -text => $_) -> pack(-side => 'left'); $spinbox[$i] = $frameGeometry -> Spinbox( -text => 0, -textvariable => \$margin{$_}, -state => 'disable', -width => 6, -increment => .1, -from => 0, -to => 100) -> pack(-side => "left"); $i++ } # ********************* I N C L U D E S ****************************** $frameIncludes = $mw -> Frame; $frameIncludes = $mw -> Frame( -label => 'INCLUDES', -borderwidth => 2, -relief => 'groove') -> pack(-side => 'top', -fill => 'x'); foreach (qw/graphicx contents bibliography titledParagraph pdfBookmarks/) { $frameIncludes -> Checkbutton( -text => $_, -variable => \$includes{$_}, -onvalue => 1, -offvalue => "") -> pack(-side => 'left');} $mw -> Button(-text => 'Execute', -command => \&execute ) -> pack(-side => 'bottom'); $frameMain -> pack(); $frameDocClass -> pack; $frameType -> pack(-side=>'left'); $framePaper -> pack(-side=>'left'); $frameFont -> pack(-side=>'left'); $frameGeometry -> pack; $frameIncludes -> pack; MainLoop; sub execute { &load_template_vars; &execute_template; } sub load_template_vars { if ( $title ) { $title = "\\title{"."$title"."}\n"; $maketitle = "\\maketitle\n"; } if ( $author ) { $author = "\\author{"."$author"."}\n"; } $geometry = "\\usepackage[left = $margin{left}cm, right = $margin{right}cm, top = $margin{top}cm, bottom = $margin{bottom}cm]{geometry}\n" if $geometry; $includes{pdfBookmarks} = "\\usepackage[bookmarks, colorlinks, plainpages=false, pdfpagelabels]{hyperref}\n" if $includes{pdfBookmarks}; $includes{graphicx} = "\\usepackage[pdftex]{graphicx}\n" if $includes{graphicx}; $includes{titledParagraph} = '\\makeatletter \\renewcommand\\paragraph{\\@startsection{paragraph}{4}{\\z@}% {-3.25ex \\@plus-1ex \\@minus-.2ex}% {0.5ex \\@plus0.2ex}% {\\normalfont\\large\\bfseries}} \\makeatother'."\n" if $includes{titledParagraph}; if ( $includes{contents} ) { $frontmatter = "\\frontmatter\n"; $mainmatter = "\\mainmatter\n"; $backmatter = "\\backmatter\n"; $contents = "\\tableofcontents\n"; } if ( $includes{bibliography} ) { $citeExample = "% Here is a Reference Example ~\\cite{example}\n"; $includes{bibliography} = "\\addcontentsline{toc}{section}{Bibliography} \\begin{thebibliography}{99} \\bibitem{example} This is an example \\end{thebibliography}\n"; } } sub execute_template { my $config = { INTERPOLATE => 1, # expand "$var" in plain text POST_CHOMP => 1, # cleanup whitespaces EVAL_PERL => 1, # evaluate Perl code blocks }; my $tt = Template->new($config); my $input = 'latex.tt'; my $vars = { title => $title, maketitle => $maketitle, author => $author, pdftex => $pdftex, fontSize => $fontSize, paper => $paper, type => $type, geometry => $geometry, pdfBookmarks => $includes{pdfBookmarks}, graphicx => $includes{graphicx}, titlepar => $includes{titledParagraph}, contents => $contents, frontmatter => $frontmatter, mainmatter => $mainmatter, backmatter => $backmatter, cite => $citeExample, bibliography => $includes{bibliography} }; $tt->process($input, $vars, $file) || die $tt->error( ); exit }