next up previous contents
Next: Defining Package Options Up: Converting LaTeX .sty Style Previous: Environments with an argument   Contents

Counters

LaTeX counters are stored in the hash table %global. For example, consider the exercise environment defined in the previous section. Suppose each exercise should have a corresponding counter, also called exercise. The LaTeX code will now look something like:

\newcounter{exercise}
                                                          
\newenvironment{exercise}[1][]%
{\begin{quote}
\refstepcounter{exercise}%
\textbf{Exercise \theexercise\ifthenelse{\equal{#1}{}}{}{ : #1}}\par}
{\end{quote}}
The Perl code will now look something like:
$global{'exercise'} = 0;

sub do_env_exercise{
   local($_) = @_;
   local($title,$pat);
                                                          
   ($title, $pat) = &get_next_optional_argument;
                                                          
   if ($title ne '')
   {
      $title = " : $title";
   }
                                                          
   $global{'exercise'}++;
                                                          
   "<BLOCKQUOTE><B>Exercise ".
   $global{'exercise'} .
   "$title</B><P>" . $_ . "</BLOCKQUOTE>";
}

You can also obtain the value of a counter using the subroutine &get_counter_value:

$val = &get_counter_value($ctr);
where $ctr contains the name of the counter, and $val is the value of that counter.

If you have a LaTeX command that has the name of a counter passed as an argument, you can read it in using &read_counter_value. This reads in a string, extracts the name of the counter at the start of the string and returns the counter name, its value, the unique identifier delimiting it and the remainder of the input string. For example, suppose you want a LaTeX command called, say, \bfroman which takes the name of a counter as the argument, and typesets it in bold roman numerals:

\newcommand{\bfroman}[1]{\textbf{\roman{#1}}}
then the Perl subroutine would look something like:
sub do_cmd_bfroman{
   local($ctr,$val,$id,$_) = &read_counter_value($_[0]);

   if ($val < 0)
   {
      $val = join('', "-", &froman(-$val));
   }
   else
   {
      $val = &froman($val);
   }

   "<B>$val</B>" . $_;
}


next up previous contents
Next: Defining Package Options Up: Converting LaTeX .sty Style Previous: Environments with an argument   Contents
Dr Nicola L C Talbot 2005-06-10