package LATEX;
# $Id: LATEX.pm,v 1.9 2017/11/09 10:33:06 sofc Exp $
############################################################
############################################################
###                                                      ###
###   Originally designed by Soeren Koch                 ###
###                                                      ###
###   Impelemented by Soeren Koch                        ###
###                                                      ###
###   Copyright (C) by Soeren Koch January 2005          ###
###                                                      ###
############################################################
############################################################

use CGI qw(:standard :netscape);
use strict;
use warnings;

BEGIN {
    use Exporter ();
    our ($VERSION, @ISA, @EXPORT, @EXPORT_OK);
    $VERSION = 0.02;
    @ISA = qw(Exporter);
    @EXPORT = qw(latex_header start_table end_table hline row end_document section subsection figure);
    @EXPORT_OK = qw();
}

our $LHEAD = 'DTU Energy Conversion';
our $CHEAD = '';
our $RHEAD = '';
our $LFOOT = '';
our $CFOOT = '';
our $RFOOT = 'Implemented by S{\o}ren Koch';
our $NICE = 0;
our $utf8 = 0;
return 1;

sub compile_doc_ps($)
{
    my $file = shift;
    $file =~ /(.*)\.tex$/;
    my $base = $1;
    my $dvi = $base.'.dvi';
    my $pdf = $base.'.pdf';
    my $tex = '/bin/nice -'.$NICE.' /usr/bin/latex '.$file.' > /dev/null 2> /dev/null';
    $tex = '/bin/nice -'.$NICE.' /usr/bin/latex '.$file if (@_);
    my $dvipdf = '/bin/nice -'.$NICE.' /usr/bin/dvips -f '.$dvi.' > '.$pdf;
    eval {
	system($tex);
	system($tex);
	system($tex);
	system($dvipdf);
	unlink $base.'.aux';
	unlink $base.'.log';
	unlink $base.'.dvi';
    };
    if ($@)
    {
	return (undef,$@);
    }
    return $pdf;
}

sub compile_doc($)
{
    my $file = shift;
    $file =~ /(.*)\.tex$/;
    my $base = $1;
    my $dvi = $base.'.dvi';
    my $pdf = $base.'.pdf';
    my $tex = '/bin/nice -'.$NICE.' /usr/bin/latex '.$file.' > /dev/null 2> /dev/null';
    $tex = '/bin/nice -'.$NICE.' /usr/bin/latex '.$file if (@_);
    my $dvipdf = '/bin/nice -'.$NICE.' /usr/bin/dvipdf -f '.$dvi.' > '.$pdf;
    eval {
	system($tex);
	system($tex);
	system($tex);
	system($dvipdf);
	unlink $base.'.aux';
	unlink $base.'.log';
	unlink $base.'.dvi';
    };
    if ($@)
    {
	return (undef,$@);
    }
    return $pdf;
}

sub latex_header_simple($$)
{
    my $title = shift;
    my $name = shift;
    $title = '' unless ($title);
    $name = '' unless ($name);
    $title =~ s/_/\\_/g;
    $name =~ s/_/\\_/g;
    my $res = <<END;
    \\documentclass\[a4paper, 12pt\]\{article\}
    \\usepackage\{epsfig\}
    \\usepackage\{latexsym\}
    \\usepackage\{graphicx\}
    \\usepackage\{makeidx\}
    \\usepackage\{fullpage, graphicx, ,rotating\}
    \\usepackage\{fancyhdr\}
    \\usepackage\{lastpage\}
    \\pagestyle\{fancy\}
    \\lhead\{$LHEAD\}
    \\chead\{$CHEAD\}
    \\rhead\{$RHEAD\}
    \\lfoot\{$LFOOT\}
    \\cfoot\{$CFOOT\}
    \\rfoot\{$RFOOT\}
    \\title{$title}
    \\author\{$name\}
    \\begin\{document\}
    \\maketitle
    \\thispagestyle\{fancy\}


END

    return $res;
}


sub latex_header($$)
{
    my $title = shift;
    $title =~ s/_/\\_/g if ($title);
    my $name = shift;
    $name =~ s/_/\\_/g if ($name);
    my $res = <<END;
    \\documentclass\[a4paper, 12pt\]\{article\}
    \\usepackage\{epsfig\}
    \\usepackage\{latexsym\}
    \\usepackage\{graphicx\}
    \\usepackage\{makeidx\}
    \\usepackage\{fullpage, graphicx, url,dcolumn,rotating\}
END
    $res .= '   \usepackage[utf8]{inputenc}'."\n" if ($utf8);
    $title = '' unless ($title);
    $name = '' unless ($name);
    $res .= <<END;
    \\setlength\{\\parskip\}\{1ex\}
    \\setlength\{\\parindent\}\{0ex\}
    \\setlength\{\\tabcolsep\}\{ 1.0 mm\}
    \\setlength\{\\headsep\}\{ 1 cm\}
    \\renewcommand\\topfraction\{.99\}
    \\renewcommand\\bottomfraction\{.99\}
    \\renewcommand\\textfraction\{.01\}
    \\renewcommand\\floatpagefraction\{.99\}
    \\newcolumntype\{d\}\[1\]\{D\{.\}\{.\}\{\#1\}\}
    \\usepackage\{fancyhdr\}
    \\usepackage\{lastpage\}
    \\usepackage\{longtable\}
    \\abovecaptionskip\=2pt
    \\floatsep\=2.0mm
    \\intextsep\=2.0mm
    \\pagestyle\{fancy\}
    \\lhead\{$LHEAD\}
    \\chead\{$CHEAD\}
    \\rhead\{$RHEAD\}
    \\lfoot\{$LFOOT\}
    \\cfoot\{$CFOOT\}
    \\rfoot\{$RFOOT\}
    \\title{$title}
    \\author\{$name\}
    \\begin\{document\}
    \\maketitle
    \\thispagestyle\{fancy\}


END

    return $res;
}


sub start_table($@)
{
    my $caption = shift;
    $caption =~ s/_/\\_/;
    return undef unless (@_);
    my $res = '\begin{table}[htbp]'."\n";
    $res .= '\belowcaptionskip=5pt'."\n";
    $res .= '  \caption{'.$caption.'}'."\n";
    $res .= '  \centering'."\n";
    my $st = '|';
    while (my $col = shift)
    {
	$st = $st.$col.'|';
    }
    $res .= '  \begin{tabular}{'.$st.'}'."\n";
    return $res;
}


sub end_table()
{
    my $res = '  \end{tabular}'."\n";
    if (@_)
    {
	my $label = shift;
	$res .= '  \label{'.$label."\}\n";
    }
    $res .= '\end{table}'."\n\n\n\n";
    return $res;
}

sub start_longtable($$@)
{
    my $caption = shift;
    $caption =~ s/_/\\_/;
    my $label = shift;
    return undef unless (@_);
    my $st = '|';
    while (my $col = shift)
    {
	$st = $st.$col.'|';
    }
    my $res .= '\begin{center}'."\n";
    $res .= '\begin{longtable}[htbp]{'.$st."\}\n";
    $res .= '  \caption{'.$caption."\}\n";
    $res .= '  \label{'.$label."\}\n";
    $res .= '  \\\\'."\n\n";
    return $res;
}

sub print_long_firsthead(@)
{
    my $res = &hline;
    $res .= &row(@_);
    $res .= &hline;
    $res .= '\endfirsthead'."\n\n";
    return $res;
}

sub print_long_head(@)
{
    my $res = &hline;
    $res .= &row(@_);
    $res .= &hline;
    $res .= '\endhead'."\n\n";
    return $res;
}

sub print_long_foot(@)
{
    my $res = &row(@_);
    $res .= '\endfoot'."\n\n";
}

sub print_long_foot_last(@)
{
    my $res = &row(@_);
    $res .= '\endlastfoot'."\n\n";
}

sub end_longtable()
{
    my $res .= '\end{longtable}'."\n";
    $res .= '\end{center}'."\n\n\n\n";
    return $res;
}


sub row(@)
{
    my $res = '     ';
    my $st = shift if (@_);
    $st =~ s/_/\\_/g if ($st);
    $res .= $st if ($st);
    while (my $st = shift)
    {
	$st =~ s/_/\\_/g;
	$res .= ' & '.$st;
    }
    $res .= '\\\\'."\n";
    return $res;
}

sub hline()
{
    return "    \\hline\n";
}

sub section($)
{
    my $str = '\section{'.shift(@_)."}\n";
    $str .= '\label{'.shift(@_)."}\n\n" if (@_);
    return $str;
}

sub subsection($)
{
    my $str = '\subsection{'.shift(@_)."}\n";
    $str .= '\label{'.shift(@_)."}\n\n" if (@_);
    return $str;
}

sub end_document
{
    return '\end{document}'."\n";
}

sub figure($$)
{
    my $fn = shift;
    my $caption = shift;
    return undef unless (-e $fn);
    $caption = ' ' unless ($caption);
    my $str = '\begin{figure}[htbp]'."\n";
    $str .= '   \abovecaptionskip=0pt'."\n";
    $str .= '   \begin{center}'."\n";
    $str .= '   \includegraphics[width=0.9 \linewidth]{'.$fn."}\n";
    $str .= '   \end{center}'."\n";
    $str .= '   \caption{'.$caption."}\n";
    $str .= '   \label{'.shift(@_)."}\n" if (@_);
    $str .= '\end{figure}'."\n\n";
    return $str;
}
