Sunday 17 October 2010

TeX code generation with perl

A couple of months ago I started to learn some basic perl, because perl is one of the programming languages that one should know. It can be used from text processing and web applications to system administration and many other applications. So I came with the idea to write something simple in order to automate some TeX code generation done by some other part of my program. So I wrote some basic perl functions that can be found here:

#!/usr/bin/env perl

sub initialize{
$text = '\documentclass{article}'."\n";
$text .= '% Created by '."\n";
$text .= '\begin{document}'."\n";
}

sub usepackage{
$text = '\usepackage{'.$_[0]."}\n";
}

sub finalize{
$text = '\end{document}'."\n";
}

sub itemize{
$text = '\begin{itemize}'."\n";
$text .= "\t\item"."\n";
$text .= '\end{itemize}'."\n";
}

sub enumerate{
$text = '\begin{enumerate}'."\n";
$text .= "\t\item"."\n";
$text .= '\end{enumerate}'."\n";
}

sub figure{
$text = '\begin{figure}'."\n";
$text .= "\t".'\includegraphics{}'."\n";
$text .= '\end{figure}'."\n";
}

sub table{
$text = '\begin{tabular}'."\n";
#$text = 'table';
$text .= '\end{tabular}'."\n";
}

sub tabbing{
$a = '\begin{tabbing}'."\n";
$a .= '\end{tabbing}'."\n";
}

sub section{
$a = '\section{'.$_[0]."}\n";
}

sub subsection{
$a = '\subsection{'.$_[0]."}\n";
}

sub equation{
$a = '\begin{equation}'."\n";
$a .= $_[0];
$a .= '\end{equation}'."\n";
}

And a simple example is given below:

print &initialize;
print &usepackage("graphicx");

print &itemize;
print &enumerate;
print &figure;
print &table;

print &finalize;

No comments:

Post a Comment