From da1010f84bf6d224cca629d8d44072f144e3bf7e Mon Sep 17 00:00:00 2001
From: Samo Penic <samo.penic@gmail.com>
Date: Tue, 13 Nov 2018 20:13:35 +0000
Subject: [PATCH] In formatting, space is always added after numerical values.

---
 testcases/dvovod31.txt |    2 
 aoi_gen/Problem.py     |    2 
 aoi_gen/Variable.py    |   62 ++++++++++++++++++-------------
 3 files changed, 38 insertions(+), 28 deletions(-)

diff --git a/aoi_gen/Problem.py b/aoi_gen/Problem.py
index 70e5ef0..ee70873 100644
--- a/aoi_gen/Problem.py
+++ b/aoi_gen/Problem.py
@@ -151,7 +151,7 @@
 
     def substitute_variables(self, text):
         for key, var in self.varDict.items():
-            text = re.sub("\/\*\/" + key + "\/\*\/", var.format_without_dollar(), text)
+            text = re.sub("\/\*\/" + key + "\/\*\/", var.format_as_tex(dollar=""), text)
         return text
 
     def substitute_octave(self, text):
diff --git a/aoi_gen/Variable.py b/aoi_gen/Variable.py
index 8b5c7f7..1196f2b 100644
--- a/aoi_gen/Variable.py
+++ b/aoi_gen/Variable.py
@@ -31,28 +31,37 @@
         formatter = FormatterFactory.get_formatter(formatting)
         self.formatted_value = formatter.getValue(self.value)
 
-    def format_as_tex(self, formatting=None, glyph=None, unit=None):
+    def format_as_tex(self, formatting=None, glyph=None, unit=None, dollar="$"):
         if formatting is None:
             formatting = self.formatting
         formatter = FormatterFactory.get_formatter(formatting)
+        if formatting.split()[0] == "prefix":
+            leading_space = ""
+        else:
+            leading_space = "~"
         if formatting == "str" or formatting == "string":
             return formatter.toFormat(self.value)
         elif glyph is None and unit is None:
-            return ("${}$").format(formatter.toFormat(self.value))
-        elif glyph is None and unit is not None:
-            return ("${}~\mathrm{{{}}}$").format(formatter.toFormat(self.value), unit)
-        elif glyph is not None and unit is None:
-            return ("${}={}$").format(glyph, formatter.toFormat(self.value))
-        else:
-            return ("${}={}~\mathrm{{{}}}$").format(
-                glyph, formatter.toFormat(self.value), unit
+            return ("{}{}{}{}").format(
+                dollar, formatter.toFormat(self.value), leading_space, dollar
             )
-
-    def format_without_dollar(self, formatting=None):
-        if formatting is None:
-            formatting = self.formatting
-        formatter = FormatterFactory.get_formatter(formatting)
-        return formatter.toFormat(self.value)
+        elif glyph is None and unit is not None:
+            return ("{}{}{}\mathrm{{{}}}{}{}").format(
+                dollar, formatter.toFormat(self.value), leading_space, unit, dollar
+            )
+        elif glyph is not None and unit is None:
+            return ("{}{}={}{}{}").format(
+                dollar, glyph, formatter.toFormat(self.value), leading_space, dollar
+            )
+        else:
+            return ("{}{}={}{}\mathrm{{{}}}{}").format(
+                dollar,
+                glyph,
+                formatter.toFormat(self.value),
+                leading_space,
+                unit,
+                dollar,
+            )
 
     def get_formatted_value(self):
         return self.formatted_value
@@ -81,7 +90,7 @@
         elif type == "prefix":
             return PrefixFloatFormatter(arglist)
         elif type == "dec" or type == "decimal":
-            return EngFloatFormatter(arglist) #fallback to engineering
+            return EngFloatFormatter(arglist)  # fallback to engineering
         else:
             return None
 
@@ -197,20 +206,21 @@
             "6": "M",
             "3": "k",
             "-3": "m",
-            "-6":"\\upmu",
-            "-9":"n",
-            "-12":"p",
-            "-15":"f",
-            "-18":"a",
-            "-21":"z",
-            "-24":"y"
+            "-6": "\\upmu",
+            "-9": "n",
+            "-12": "p",
+            "-15": "f",
+            "-18": "a",
+            "-21": "z",
+            "-24": "y",
         }
         try:
             prefix = prefixes[str(exp)]
         except KeyError:
-            raise Exceptions.PrefixError("Could not change exponent " + str(exp) + " into prefix form!")
+            raise Exceptions.PrefixError(
+                "Could not change exponent " + str(exp) + " into prefix form!"
+            )
         return prefix
-
 
     def toFormat(self, num):
         try:
@@ -225,7 +235,7 @@
         (exp, man) = self.realign3(exp, man)
         if exp == 0:
             return (
-                ("{:." + str(self.precision - 1) + "f}").format(man).replace(".", ",\!")
+                ("{:." + str(self.precision - 1) + "f}~").format(man).replace(".", ",\!")
             )
         else:
             prefix = self.exp2prefix(exp)
diff --git a/testcases/dvovod31.txt b/testcases/dvovod31.txt
index 2b70c92..a66e69c 100644
--- a/testcases/dvovod31.txt
+++ b/testcases/dvovod31.txt
@@ -43,7 +43,7 @@
 
 #StartRezultat
 TeX:		E(\mathrm{A})
-izpis:		sci 3
+izpis:		prefix 3
 enota:		V/m
 formula:	eps0=8.854e-12;EA=q/(pi*eps0*h)*abs(-1+1/(1+(d/h)^2));EA
 napacna:	q/(2*pi*eps0*h)*abs(-1+1/(1+(d/h)^2))

--
Gitblit v1.8.0