next up previous contents
Next: Commands with arguments Up: Writing Subroutines to Translate Previous: Writing Subroutines to Translate   Contents


Commands without an argument

The easiest way to explain something is via example, so let's try creating a very simple package. Suppose you want a package called, say, mydate.sty which redefines the command \today so that the date is formatted in the form year-month-day, e.g. 2005-6-30. The LaTeX code will look something like:

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mydate}

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

\endinput
Since the package is called mydate.sty, the Perl file needs to be called mydate.perl, and should look something like:
#!/usr/bin/perl

sub do_cmd_today{
   local($_) = @_;
   local($sec,$min,$hr,$day,$month,$year) = localtime(time);
   $year += 1900;
   $month++;

   "$year-$month-$day" . $_;
}

1;
Things to note: the last line in the file must always be 1; and the first line may vary depending on your system. Since the command \today does not take any arguments, the subroutine do_cmd_today does not need to read any information in from its input string, however, it must append this string to the text generated by the command, and return it. In the above example, this is done by string concatenation:
"$year-$month-$day" . $_;
but can also be done using the join function:
join('', "$year-$month-$day", $_);
or even:
join('-', $year,$month,$day) . $_;
If you do not do this, you will lose the rest of the text in that segment.


next up previous contents
Next: Commands with arguments Up: Writing Subroutines to Translate Previous: Writing Subroutines to Translate   Contents
Dr Nicola L C Talbot 2005-06-10