File Coverage

blib/lib/HEP/Names/LaTeX.pm
Criterion Covered Total %
statement 25 25 100.0
branch 17 18 94.4
condition n/a
subroutine 4 4 100.0
pod 1 1 100.0
total 47 48 97.9


line stmt bran cond sub pod time code
1 1     1   97683 use strict;
  1         3  
  1         62  
2 1     1   7 use warnings;
  1         2  
  1         220  
3              
4             package HEP::Names::LaTeX;
5             $HEP::Names::LaTeX::VERSION = '0.01';
6             # ABSTRACT: formats plain names of high energy physics (HEP) particles to LaTeX
7              
8              
9 1     1   8 use Exporter qw( import );
  1         7  
  1         779  
10              
11             our @EXPORT = qw( particle_to_latex );
12              
13              
14             my $names_to_escape = qr(psi|upsilon|gamma|tau|rho|pi|mu|eta|nu|omega);
15              
16             sub particle_to_latex
17             {
18 10 50   10 1 44 return '' unless @_;
19              
20             # in case of string literal in argument, we cannot modify @_!
21 10         23 my @names = @_;
22 10         20 foreach my $name ( @names ) {
23              
24 12         20 my ( $charge, $has_apo ) = ( '', '' );
25             # this probably should be done better :)
26 12         34 my $is_anti = $name =~ s/^anti-//;
27             # charge is at last position (plus *)
28 12 100       90 $charge = $1 if $name =~ s/(\**[0+-]+$)//;
29 12 100       39 $has_apo = $1 if $name =~ s/('+)//;
30 12 100       39 my $subscript = $1 if $name =~ s/_(.*)$//;
31              
32 12         17 my $total_charge = $has_apo.$charge;
33             #$total_charge = undef unless length( $total_charge )
34              
35             # escape greek names
36 12         153 $name =~ s/($names_to_escape)/\\$1/gi;
37 12 100       99 $subscript =~ s/($names_to_escape)/\\$1/gi if $subscript;
38              
39             # ad put it together
40 12 100       33 $name = "\\bar{$name}" if $is_anti;
41 12 100       24 $name = $name."_{$subscript}" if $subscript;
42 12 100       37 $name = $name."^{$total_charge}" if length($total_charge);
43 12         41 $name = "\$".$name."\$";
44             }
45              
46 10 100       73 return wantarray ? @names : $names[0];
47             }
48              
49              
50             1;
51              
52             __END__