]> git.lyx.org Git - lyx.git/commitdiff
Make a valid LaTeX length understandable to LyX.
authorAngus Leeming <leeming@lyx.org>
Tue, 25 Feb 2003 12:57:18 +0000 (12:57 +0000)
committerAngus Leeming <leeming@lyx.org>
Tue, 25 Feb 2003 12:57:18 +0000 (12:57 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@6245 a592a061-630c-0410-9148-cb99ea01b6c8

lib/ChangeLog
lib/reLyX/BasicLyX.pm

index c880a745d8f42446657a546a975871af54efa76f..a6e89e98d007b9cbb83157e415494d52023bfcba 100644 (file)
@@ -1,3 +1,7 @@
+2003-02-25  Angus Leeming  <leeming@lyx.org>
+
+       * reLyX/BasicLyX.pm (regularizeLatexLength, getAsLyXLength):
+       make a valid LaTeX length understandable to LyX.
 
 2003-02-21 André Pönitz <poenitz@gmx.net>
 
index 1b14bf4583c1b7751037b901dc61ae4f0e573052..d5ddc5c1fa6f91805a5a9f932bce6531affd88d8 100644 (file)
@@ -353,28 +353,54 @@ sub ending_math {
 }
 
 
-# Straight translation of LaTeX lengths to LyX ones.
-my %lengthAsLyXString = ('\textwidth' => 'text%',
-                        '\columnwidth' => 'col%',
-                        '\paperwidth' => 'page%',
-                        '\linewidth' => 'line%',
-                        '\paperheight' => 'pheight%',
-                        '\textheight' => 'theight%');
+sub regularizeLatexLength {
+    my $LatexLength = shift;
+
+    # Remove any whitespace
+    $LatexLength =~ s/\s//g;
+    # Remove a leading '+' as unnecessary
+    $LatexLength =~ s/^\+?(\d)/$1/;
+    # Split into value and unit parts
+    my $val;
+    my $unit;
+    if ($LatexLength =~ /(-?\d+[.,]?\d*)(\\?[a-zA-Z]*)$/) {
+       $val  = $1;
+       $unit = $2;
+    }
+    # If the input is invalid, return what we have.
+    return $LatexLength if ($val eq '' || $unit eq '');
+
+    # '4,5' is a valid LaTeX number. Change it to '4.5'
+    $val =~ s/,/./;
+    # If the unit is not a LaTeX macro, then ensure it is lower case
+    if (!($unit =~ /^\\/)) {
+       $unit =~ s/([a-z]*)/\L$1/i;
+    }
+
+    $LatexLength = $val . $unit;
+    return $LatexLength;
+}
+    
 
 sub getAsLyXLength {
+    # Straight translation of LaTeX lengths to LyX ones.
+    my %lengthAsLyXString = ('\textwidth'   => 'text%',
+                            '\columnwidth' => 'col%',
+                            '\paperwidth'  => 'page%',
+                            '\linewidth'   => 'line%',
+                            '\paperheight' => 'pheight%',
+                            '\textheight'  => 'theight%');
+
     my $LatexLength = shift;
+    $LatexLength = regularizeLatexLength($LatexLength);
 
-    my $LyXLength = '';
+    my $LyXLength = $LatexLength;
     # If $LatexLength is something like '4.5\columnwidth', translate into
     # LyXese.
-    if ($LatexLength =~ /([0-9]+\.?[0-9]*)\s*(\\[a-z]*)/) {
+    if ($LatexLength =~ /([+-]?\d+\.?\d*)(\\[a-z]*)/) {
        if (defined($lengthAsLyXString{$2})) {
            $LyXLength = ($1 * 100) . $lengthAsLyXString{$2};
        }
-    } else {
-       $LyXLength = $LatexLength;
-       # Remove any spaces from '4.5 cm'
-       $LyXLength =~ s/\s*//g;
     }
 
     return $LyXLength;