next up previous contents
Next: Reading Other Files Up: Converting LaTeX .sty Style Previous: Counters   Contents

Defining Package Options

Given a package called name, each package option opt is dealt with by the subroutine &do_name_opt. Returning to the mydate package example, described earlier, suppose this package now has two options: dash (e.g. 2005-6-30) and dot (e.g. 2005.6.30). The LaTeX code will now look something like:

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mydate}
\newcommand{\datesep}{-}
\DeclareOption{dash}{\renewcommand{\datesep}{-}}
\DeclareOption{dot}{\renewcommand{\datesep}{.}}
\ProcessOptions

\renewcommand{\today}{\number\year\datesep
\number\month\datesep\number\day}

\endinput
The Perl code will now need the subroutines &do_mydate_dash and &do_mydate_dot in order to implement the package options. The file mydate.perl will now look something like:
#!/usr/bin/perl

$datesep = '-';

sub do_mydate_dash{
   $datesep = '-';
}

sub do_mydate_dot{
   $datesep = '.';
}

sub do_cmd_datesep{
   local($_) = @_;
   $datesep . $_;
}

sub do_cmd_today{
   local($_) = @_;
   local($sec,$min,$hr,$day,$month,$year) = localtime(time);
   $year += 1900;
   $month++;
                                                                  
   "$year\\datesep $month\\datesep $day" . $_;
}
                                                                  
1;
Suppose you now want to use the keyval package to specify your package options. For example, you might want to do:
\usepackage[style=dash]{mydate}
or
\usepackage[style=dot]{mydate}
For each package option in the form key=value, you need to supply the subroutine &do_packagename_key_value. So for the above example, you will need:
sub do_mydate_style_dash{
   $datesep = '-';
}

sub do_mydate_style_dot{
   $datesep = '.';
}



Dr Nicola L C Talbot 2005-06-10