blib/lib/Number/WithError/LaTeX.pm

filter: Statementstmt 98.6% 74 / 75 Branchbran 78.5% 22 / 28 Conditioncond 66.6% 4 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 15 / 15 Podpod 100% 6 / 6 totaltotal 91.0% 122 / 134 CC 17 SCAR 23.1
CC
17
Cov
92%
latex
17.6
BEGIN
1.0
BEGIN
1.0
CRAP 10.1
↑ Summary
1 package Number::WithError::LaTeX;
2  
3 7 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 our $VERSION = '0.06';
8  
9 7 ▶use base 'Number::WithError';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 our %EXPORT_TAGS = %Number::WithError::EXPORT_TAGS;
13 7 our @EXPORT_OK = @Number::WithError::EXPORT_OK;
14  
15 7 ▶use TeX::Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 5016 ▶sub witherror { Number::WithError::LaTeX->new(@_) }
Subroutine witherror: called CC=1 SCAR=0.0
Pod documented
20 27 ▶sub witherror_big { Number::WithError::LaTeX->new_big(@_) }
Subroutine witherror_big: called CC=1 SCAR=0.0
Pod documented
21  
22 7 ▶use Params::Util qw/_ARRAY0/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24  
25 =head1 NAME
26  
27 Number::WithError::LaTeX - LaTeX output for Number::WithError
28  
29 =head1 SYNOPSIS
30  
31 use Number::WithError::LaTeX;
32
33 my $num = Number::WithError::LaTeX->new(5.647, 0.31);
34 print $num . "\n";
35 # prints '5.65e+00 +/- 3.1e-01'
36 # (I.e. it automatically does scientific rounding)
37
38 print $num->latex() . "\n";
39 # prints '5.65 \cdot 10^{0} \pm 3.1 \cdot 10^{-1}'
40
41 print $num->latex(radix => ',', enclose => '$') . "\n";
42 # prints '$5,\!65 \cdot 10^{0} \pm 3,\!1 \cdot 10^{-1}$'
43
44 print $num->encode("This will encode an e-acute (".chr(0xe9).") as \\'e") . "\n";
45 # Delegated to TeX::Encode::encode().
46 # prints 'This is a German umlaut: \"a'
47  
48 =head1 DESCRIPTION
49  
50 This class is a subclass of L<Number::WithError>. It provides the same
51 interface and the same exports.
52  
53 It adds several methods to every object. The main functionality is provided by
54 C<latex()>, which dumps the object
55 as valid LaTeX code. Also, C<encode()> is a convenient way to encode
56 any UTF-8 string into TeX. It is just a convenience thing since it is delegated
57 to L<TeX::Encode>.
58  
59 Unlike C<Number::WithError>, this module requires perl version 5.8 or later.
60 (That is the rationale for creating a separate distribution, too.)
61  
62 =head1 EXPORT
63  
64 This module exports the following subroutines on demand. It supports
65 the C<:all> Exporter tag to export all of them. The subroutines are
66 documented in L<Number::WithError>.
67  
68 =head2 witherror
69  
70 =head2 witherror_big
71  
72 =cut
73  
74 =head1 METHODS
75  
76 This is a list of public methods.
77  
78 =head2 latex
79  
80 This method stringifies the object as valid LaTeX code. The returned
81 string is valid in a LaTeX math mode. That means, you will have to
82 enclose it in dollars or in an C<equation> environment by default.
83  
84 The method takes named parameters. All parameters are optional.
85  
86 The C<enclose> parameter can set a string to enclose the produced
87 latex code in. This can be either a simple string like C<$> or an
88 array reference containing two strings. Those two strings will be
89 used for the start and end respectively. (For environments.)
90  
91 Example: (let C<$obj> be '5.6e-01 +/- 2.3e-02')
92  
93 $obj->latex(enclose => '$');
94 # returns '$5.6 \cdot 10^{-1} \pm 2.3 \cdot 10^{-2}$'
95  
96 The asymmetric environment-like C<enclose> can be used as follows:
97  
98 $obj->latex(enclose => ['\begin{equation}', '\end{equation}']);
99 # returns'\begin{equation}5.6 \cdot 10^{-1} \pm 2.3 \cdot 10^{-2}\end{equation}'
100  
101 There are two convenience methods C<latex_math> and C<latex_equation> which do
102 exactly what the above examples demonstrated.
103  
104 The C<radix> parameter can set the radix (I<decimal point>) used. The default is
105 a dot (C<.>). If you use a comma, LaTeX will generally typeset it in a way that
106 results in a space after the comma. Since that is not desireable, using a C<,>
107 as the radix results in the radix being set as C<,\!>. An example can be found
108 in the synopsis.
109  
110 =cut
111  
112 7 our $CFloatCapture = qr/([+-]?)(?=\d|\.\d)(\d*(?:\.\d*)?)((?:[Ee][+-]?\d+)?)/;
113  
114 216 ▶sub latex {
Subroutine latex: called CC=17 SCAR=28.7
Pod documented
115 216 my $self = shift;
116 216 ▶ croak("Uneven number of arguments to ".__PACKAGE__."->latex().") if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 216 216
117 216 my %opt = @_;
118 216 my $radix = $opt{radix};
119 216 ▶ if (not defined $radix) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $radix) { } 108 108 216
elsif ($radix eq '.') { } 108 0 108
120 108 $radix = '.';
121 }
122 elsif ($radix eq '.') {
123 #fine
124 }
125 else {
126 0 $radix .= '\!';
127 }
128  
129 216 my $enclose = $opt{enclose};
130 216 ▶ $enclose = '' if not defined $enclose;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $enclose 36 180 216
131 216 ▶ $enclose = '' if _ARRAY0($enclose) and @$enclose != 2;
Branch: 1 branch
BranchTrueFalseTotal
if &_ARRAY0($enclose) and @$enclose != 2 0 216 216
Condition66%2/3
&_ARRAY0($enclose) and @$enclose != 2
!ll&&!rl&&r
144720
MC/DC0%0/2
&_ARRAY0($enclose) and @$enclose != 2
&_ARRAY0($enclose) @$enclose != 2
Truth table66%2/3
A and B
A= &_ARRAY0($enclose)B= @$enclose != 2
ABresult
0X0
100
111
132
133 216 my $str = "".$self->round();
134  
135 216 my $result;
136 216 pos($str) = 0;
137 216 my $p = -1;
138 216 my $number = 1;
139 216 ▶ while (defined pos($str) and pos($str) < length($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined pos $str and pos $str < length $str) 792 216 1008
Condition66%2/3
defined pos $str and pos $str < length $str
!ll&&!rl&&r
0216792
MC/DC50%1/2
defined pos $str and pos $str < length $str
defined pos $str pos $str < length $str
Truth table66%2/3
A and B
A= defined pos $strB= pos $str < length $str
ABresult
0X0
100
111
140 792 ▶ die "Failed to advance string parser at position $p in '$str'." if pos($str) == $p;
Branch: 1 branch
BranchTrueFalseTotal
if pos $str == $p 0 792 792
141 792 $p = pos($str);
142
143 # number
144 792 ▶ if ($number) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number) { } 504 288 792
145 504 ▶ $str =~ /\G\s*$CFloatCapture\s*/cgo or die "Expected number starting at position $p in '$str'.";
Branch: 1 branch
BranchTrueFalseTotal
$str =~ /\G\s*$Number::WithError::LaTeX::CFloatCapture\s*/cog or die "Expected number starting at position $p in '${str}'." 504 0 504
146 504 my $sgn = $1;
147 504 my $num = $2;
148 504 my $exp = $3;
149  
150 504 ▶ unless ($exp =~ s/^[eE]([+-]?)(\d+)$/" \\cdot 10^{".($1eq'-'?'-':'').(0+$2)."}"/e) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($exp =~ s/^[eE]([+-]?)(\d+)$/' \\cdot 10^{' . ($1 eq "-" ? "-" : "") . (0 + $2) . '}';/e) 48 456 504
$1 eq '-' ? : 72 384 456
151 48 $exp = ' \cdot 10^{0}';
152 }
153
154 504 $num =~ s/\./$radix/;
155
156 504 $result .= "$sgn$num$exp";
157 504 $number = 0;
158 }
159 # +/-, +, -
160 else {
161 288 ▶ $str =~ /\G\s*(\+\/\-|\+|\-)\s*/cgo or die "Expected operator (+/-, +, -) starting at position $p in '$str'.";
Branch: 1 branch
BranchTrueFalseTotal
$str =~ m[\G\s*(\+/\-|\+|\-)\s*]cog or die "Expected operator (+/-, +, -) starting at position $p in '${str}'." 288 0 288
162 288 my $op = $1;
163 288 ▶ if ($op eq '+/-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "+/-") 240 48 288
164 240 $op = '\pm';
165 }
166  
167 288 $result .= " $op ";
168 288 $number = 1;
169 }
170
171 }
172
173 216 ▶ if (_ARRAY0($enclose)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_ARRAY0($enclose)) { } 72 144 216
174 72 return $enclose->[0].$result.$enclose->[1];
175 }
176 else {
177 144 return $enclose.$result.$enclose;
178 }
179 }
180  
181 =head2 latex_math
182  
183 Works exactly like C<latex()> except that the C<enclose> string defaults to C<$>.
184  
185 =cut
186  
187 36 ▶sub latex_math {
Subroutine latex_math: called CC=1 SCAR=0.0
Pod documented
188 36 shift()->latex(enclose => '$', @_)
189 }
190  
191 =head2 latex_equation
192  
193 Works exactly like C<latex()> except that the C<enclose> string defaults to the
194 environment C<\begin{equation}\n> and C<\n\end{equation}>.
195  
196 =cut
197  
198 36 ▶sub latex_equation {
Subroutine latex_equation: called CC=1 SCAR=0.0
Pod documented
199 36 shift()->latex(enclose => ["\\begin{equation}\n", "\n\\end{equation}"], @_)
200 }
201  
202 =head2 encode
203  
204 This method encodes an arbitrary UTF-8 string as TeX. Syntax:
205  
206 my $encoded = $obj->encode($string);
207  
208 For detailed documentation, please refer to L<TeX::Encode>.
209  
210 =cut
211  
212 10 ▶sub encode {
Subroutine encode: called CC=1 SCAR=0.0
Pod documented
213 10 my $self = shift;
214 10 return Encode::encode('latex', shift);
215 }
216  
217  
218 7 1;
219  
220 __END__
↑ Summary