# Library for easy LaTeX formatting of data v21102016 # Copyright (C) 2016 Peter Bosch # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2 of the License, or at your option any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 # USA import numpy as np def print_tabhdr( fmtrow, caprow ): print "\\begin{tabular}{" + fmtrow + "}" print "\\hline\n" + caprow + "\\\\\n\\hline" def texnumfmt( sig, num ): if sig >= 0: sci = True sig = sig - 1 else: sci = False sig = (-sig)-1 if sci: ns = ("%."+str(sig)+"e") % num else: ns = ("%."+str(sig)+"f") % num ns = ns.replace("e+", "\\cdot10^{").replace("e","\\cdot10^{") ns = ns.replace("{-0","{-").replace("{0","{") if ns.find("{")!=-1: ns = "$" + ns + "}$" return ns def print_tabftr( ): print "\\hline\n\\end{tabular}" def table_fmtentry( sig, entry ): if isinstance( entry, np.number ): return texnumfmt(sig, entry) if isinstance( entry, float ): return texnumfmt(sig, entry) return str(entry) def print_table( caprow, *args ): nar = len(args) nm = [] sg = [] sig = 3 align = "r" fmtrow = "" row = "" for arid,ar in enumerate(args): if type(ar)==str: align = ar elif type(ar)==int: #skip sig=ar else: fmtrow = fmtrow + align + " | " sg.append(sig) nm.append(arid) align = "r" fmtrow = (fmtrow + "end").replace("| end","").replace("end","") print_tabhdr( fmtrow, caprow ) for idx,el in enumerate(args[nm[0]]): row = "" for i,arid in enumerate(nm): row = row + table_fmtentry(sg[i], args[arid][idx]) + " & " row = ( row + "\\\\" ).replace("& \\\\","\\\\") print row print_tabftr()