• Main Page
  • Namespaces
  • Classes
  • Files
  • File List

/home/mark/model/software/ScrumPy/ScrumPy/Util/LaTeX.py

00001 import sys,types
00002 
00003 braces = ("{","}")
00004 sqbracs = ("[","]")
00005 
00006 backsl = "\\"
00007 eol = " \\\\"
00008 tabsep = " & "
00009 
00010 textwidth="\\textwidth"
00011 textheight="\\textheight"
00012 bang = "!"
00013 reschars = r"\#$%&~_^{}"
00014 
00015 def DeTex(s):
00016     
00017     for ch in reschars:
00018         s = s.replace(ch, "\\"+ch)
00019     return s
00020 
00021 
00022 
00023 def FloatToTex(f, sf=3):
00024 
00025 
00026     if type(f) != types.FloatType:
00027         return ""
00028 
00029     if f == 0.0:
00030         return "0"
00031 
00032     format = "".join(("%1.",str(sf-1),"e"))
00033 
00034     fs = format % f
00035     mant,exp = fs.split("e")
00036 
00037     af = abs(f)
00038     if af<10 and af>1:
00039         exp=""
00040     else:
00041         if abs(int(exp))<10:
00042             exp = exp.replace("0","")
00043         exp = exp.replace("+","")
00044         exp = exp.join((r"\times 10^{","}"))
00045         
00046     return "".join(("$",mant,exp,"$"))
00047 
00048 def Option(text):
00049 
00050     if text == "":
00051         return ""
00052     return text.join(sqbracs)
00053     
00054 def OptList(opts):
00055 
00056     if len(opts)==0:
00057         return ""
00058     return "".join(map(Option, opts))
00059 
00060 
00061 def Argument(text):
00062 
00063     if text == "":
00064         return ""
00065     return text.join(braces)
00066 
00067 
00068 def ArgList(args):
00069 
00070     if len(args) == 0:
00071         return ""
00072     return "".join(map(Argument,args))
00073 
00074 
00075 def StartCom(com, opts=[], args=[]):
00076     return backsl + com + OptList(opts) + ArgList(args)
00077     
00078 
00079 
00080 def Command(com, opts=[], args=[], text=""):
00081 
00082     return backsl + com + OptList(opts) + ArgList(args) + Argument(text)
00083 
00084 
00085 def Begin(env, opts=[], args=[]):
00086 
00087     return Command("begin", opts, [env]+args)
00088 
00089 
00090 def End(env):
00091 
00092     return Command("end", text=env)
00093 
00094 
00095 def Environ(env, opts=[], args=[], text=""):
00096 
00097     return Begin(env, opts, args) + text + End(env)
00098 
00099 
00100 def Center(text):
00101 
00102     return Environ("center", text=text)
00103 
00104 
00105 def IncGfx(file, opts=[]):
00106 
00107     return Command("includegraphics", opts=opts, text=file)
00108 
00109 
00110 def TabRow(items):
00111 
00112     return tabsep.join(items) + eol
00113 
00114 
00115 
00116 def MakeNiceString(s):
00117 
00118     s = s.replace("_","\\_")
00119 
00120     return s # maybe more later
00121 
00122 
00123 class LaTeX:
00124 
00125     def __init__(self):
00126 
00127         self.indent = ""
00128 
00129         self.Begin = ""
00130         self.Content = []
00131         self.End = ""
00132 
00133 
00134     def Copy(self):
00135 
00136         rv = LaTeX()
00137         rv.Begin = self.Begin
00138         rv.End = self.End
00139         for c in self.Content:
00140             if hasattr(c, "Copy"):
00141                 rv.Content.append(c.Copy())
00142             else:
00143                 rv.Content.append(c)
00144         return rv
00145 
00146 
00147     def MakeCommand(self, com, opts=[], args=[]):
00148 
00149         self.Begin =  Command(com, opts, args) + "{"
00150         self.End = "}"
00151 
00152 
00153     def MakeEnvironment(self, env, opts=[], args=[]):
00154 
00155         self.Begin = Begin(env, opts, args)
00156         self.End = End(env)
00157 
00158 
00159 
00160     def AddContent(self, Content):
00161     
00162         self.Content.append(Content)
00163 
00164 
00165     def __str__(self):
00166 
00167         strlist =  [self.indent + self.Begin]
00168         nextindent = self.indent + "\t"
00169         for c in self.Content:
00170             if self.__class__ == c.__class__ or self.__class__ in c.__class__.__bases__ :
00171                 c.indent = nextindent
00172                 cstr = str(c)
00173             else:
00174                 cstr= nextindent + str(c)
00175            
00176             strlist.append(cstr)
00177         strlist.append(self.indent+self.End)
00178 
00179         return  "\n".join(strlist)
00180 
00181 
00182     def AddCommand(self,com, opts=[], args=[]):
00183 
00184         rv = LaTeX()
00185         rv.MakeCommand(com, opts, args)
00186         self.AddContent(rv)
00187         return rv
00188 
00189 
00190     def AddEnvironment(self, env, opts=[], args=[]):
00191 
00192         rv = LaTeX()
00193         rv.MakeEnvironment(env, opts, args)
00194         self.AddContent(rv)
00195         return rv
00196 
00197 
00198     def AddTabular(self, ncols=2, format="c"):
00199 
00200         rv = Tabular(ncols, format)
00201         self.AddContent(rv)
00202         return rv
00203         
00204 
00205     def AddMultiTabular(self, items, nrows =3, ncols = 2, format="c", holder=None):
00206 
00207      
00208         step = nrows*ncols
00209 
00210         while len(items)>step:
00211             
00212             cur, items = items[:step], items[step:]
00213             tab = Tabular(ncols, format)
00214             tab.AddRows(cur)
00215             if holder != None:
00216                 content = holder.Copy()
00217                 content.Content.append(tab)
00218             else:
00219                 content = tab
00220             self.Content.append(content)
00221 
00222         if len(items) != 0:
00223             
00224             tab = Tabular(ncols, format)
00225             tab.AddRows(items)
00226             if holder != None:
00227                 content = holder.Copy()
00228                 content.Content.append(tab)
00229             else:
00230                 content = tab
00231             self.Content.append(content)
00232 
00233 
00234     def AddOneLine(self, line):
00235         self.Content.append(line)
00236 
00237     def InsertOneLine(self, line):
00238         self.Content.insert(line)
00239 
00240 
00241     def Resize(self, width=textwidth, height=bang):
00242 
00243         self.Begin = Command("resizebox", args=[width,height]) +"{"+ self.Begin
00244         self.End += "}"
00245                              
00246             
00247 
00248 
00249     
00250 class Tabular(LaTeX):
00251 
00252     def __init__(self, ncols, format="c"):
00253 
00254         LaTeX.__init__(self)
00255 
00256         self.ncols = ncols
00257 
00258         self.MakeEnvironment("tabular", args=[format*ncols])
00259 
00260 
00261   
00262 
00263     def AddRow(self, row):
00264 
00265         missing = self.ncols - len(row)
00266         if missing <0:
00267             raise "row too long"
00268 
00269         row = row + [""] * missing
00270 
00271         self.AddContent(TabRow(row))
00272 
00273 
00274     def AddRows(self, items):
00275 
00276         while len(items) > self.ncols:
00277 
00278             self.AddRow(items[:self.ncols])
00279             items = items[self.ncols:]
00280 
00281         self.AddRow(items)
00282 
00283         
00284         
00285         
00286     
00287         
00288 
00289         
00290 
00291     
00292 
00293 
00294 
00295     
00296     

Generated on Tue Sep 4 2012 15:38:02 for ScrumPy by  doxygen 1.7.1