Skip to content


A PERL script to generate LaTeX documents

I write a lot of LaTeX in my work.  In fact, after most experiments, I write up a quick latex document to describe my results.  As a result I tend to start a lot of latex documents, and as such I wanted a quick way to generate a skeleton document that I can quickly fill out.

So, rather than just producing a template and making a copy of that, or using a latex IDE, I decided I’d write a PERL script — its the obvious thing to do really…

Basically, inside the script there are a load of default options which you can set if there are settings you commonly use, then for anything else there are command line options.  The latex is output to standard out, and you can simply pipe that into a file.  Magic :o

Here’s the code:

#!/usr/bin/perl

use Getopt::Long;
use Data::Dumper;

# Defaults

	my $authorName = "Freud";
	my $authorEmail = '...';
	my $documentClass = "article";
	my %documentOptions = {"article" => ""};

	my @packages=( "url","hyperref","graphicx" );

# End of Configuration

	my $overrideOptions;
	my $packageString = "";
	my $docopts = "";
	my $title = "";

	# Specify default options for each document type here.

	# Allow grouping of options into single blocks like -abc etc (although that wouldnt work here anyway
	# we'll put it here just to make it easier for future Harries to edit this document and not break thigns).

	Getopt::Long::Configure('bundling_override');
	GetOptions(	'a|author=s' => \$authorName,
			'e|email=s' => \$authorEmail,
			'c|class=s' => \$documentClass,
			'o|options=s' => \$overrideOptions,
			'oo|appendOptions=s' => \$appendOptions,
			'u|usepackage=s' => \@packages
		);

	foreach (@packages)
	{
		my $options = "";
		if($_ =~ m/([^\[]*)(\[.+\])?/)
		{
			$package = $1;
			$options = $2;

			$packageString .= "\\usepackage$options\{$package\}\n";
		}
	}

	if($#ARGV < 0)
	{
		print STDERR "Please specify a title as the final command line argument.";
		die;
	}

	$title = $ARGV[0];

	if(exists($documentOptions{$documentClass}))
	{
		$docopts = $documentOptions{$documentClass};
	}

	if(length($overrideOptions) > 0)
	{
		$docopts = $overrideOptions;
	}

	if(length($appendOptions) > 0)
	{
		$docopts = "$docopts,$appendOptions";
	}

	if(length($authorEmail) > 0 && length($authorName) > 0)
	{
		$authorEmail = "\\thanks{$authorEmail}";
	}

	print STDOUT <<OUTPUT;
\\documentclass[$docopts]{$documentClass}

$packageString

\\author{$authorName$authorEmail}
\\title{$title}
\\begin{document}
	\\maketitle

\\end{document}
OUTPUT

Posted in Programming, Research.

Tagged with , , , , , , .


One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Sascha Stanojevi? says

    16) Gestern ging mein Motorroller kaputt, wer hat Ahnung was kaputt sein könnte?



Some HTML is OK

or, reply to this post via trackback.