pytexutils.tables.table
1import numpy as np 2 3def table(columns_name : list, data : np.ndarray, round_val : int = 4, bold_axis : int = None, caption : str = "table_caption", label : str = "table_label", preamble : bool = False) -> str: 4 ''' 5 Produces LaTeX code to display a table. 6 7 Parameters: 8 ----------- 9 - columns_name 10 list of strings containing table columns name 11 - data : np.ndarray 12 2D ndarray containing data used to fill the table 13 - round_val : int 14 integer representing the decimal rounding 15 - bold_axis : int 16 integer representing the axis to wich get the max value and to set bold, if None no bold text will be added 17 if 0 the maximum will be calculated column-wise, if 1 the maximum will be calculated row-wise 18 - caption : str 19 string for the caption of LaTeX table (default: "table_caption") 20 - label : str 21 string for the label of LaTeX table (default: "table_label") 22 - preamble : bool 23 If True the function will return a full LaTeX document, if False the function will return only the table (default: False) 24 25 Returns: 26 -------- 27 - p : str 28 LaTeX code to display a table 29 30 Usage: 31 ------ 32 33 ```python 34 import numpy as np 35 36 columns_name = ['A', 'B', 'C'] 37 data = np.array( 38 [ 39 [0.1, 0.2, 0.3], 40 [0.4, 0.5, 0.6], 41 [0.7, 0.8, 0.9] 42 ] 43 ) 44 45 latex_table = table(columns_name, data, caption='My table 1', label='tab1', preamble=True) 46 ``` 47 48 Output: 49 ------- 50 51 ```latex 52 \\documentclass[11pt]{article} 53 \\usepackage{booktabs} 54 \\usepackage{graphicx} 55 56 \\begin{document} 57 58 \\begin{table}[!ht] 59 \\centering 60 \\caption{My table 1}\\label{tab:tab1} 61 \\resizebox{\\columnwidth}{!}{ 62 \\begin{tabular}{ccc} 63 \\toprule 64 A & B & C \\\\ 65 \\midrule 66 0.1 & 0.2 & 0.3 \\\\ 67 0.4 & 0.5 & 0.6 \\\\ 68 0.7 & 0.8 & 0.9 \\\\ 69 1.1 & 1.2 & 1.3 \\\\ 70 \\bottomrule 71 \\end{tabular}} 72 \\end{table} 73 74 \\end{document} 75 ``` 76 ''' 77 78 if len(data.shape) != 2: 79 raise Exception("Error Message: shape of data must be equals to two.") 80 81 if columns_name is not None: 82 if data.shape[1] != len(columns_name): 83 raise Exception("Error Message: mismatch between number of columns and shape of data") 84 85 if round_val < 1: 86 round_val = 1 87 88 if bold_axis is not None: 89 if bold_axis < 0 or bold_axis > 1: 90 bold_axis = None 91 92 p = "" 93 # LaTeX preamble 94 if preamble: 95 p += "\\documentclass[11pt]{article}\n" 96 p += "\\usepackage{booktabs}\n" 97 p += "\\usepackage{graphicx}\n\n" 98 p += "\\begin{document}\n\n" 99 100 # Table 101 p += "\\begin{table}[!ht]\n" 102 p += "\t\\centering\n" 103 p += "\t\\caption{"+str(caption)+"}\\label{tab:"+label+"}\n" 104 p += "\t\\resizebox{\\columnwidth}{!}{\n" 105 p += "\t\\begin{tabular}{" + "".join([char*data.shape[1] for char in "c"]) + '}\n' 106 p += "\t\t\\toprule\n" 107 108 if columns_name is not None: 109 # Columns name 110 l = "\t\t" 111 for i in range(len(columns_name)): 112 l+= "{:<1s}{}{:<1s}{}".format("", str(columns_name[i].strip()), "", "&") 113 l = l[:-1] 114 p += l + "\\\\\n" 115 p += "\t\t\\midrule\n" 116 117 if bold_axis is not None: 118 119 new_data_min = [] 120 new_data_max = [] 121 for x in range(data.shape[0]): 122 dmin = [] 123 dmax = [] 124 for y in range(data.shape[1]): 125 try: 126 dmin.append(float(data[x,y])) 127 dmax.append(float(data[x,y])) 128 except Exception: 129 dmin.append(+1e100) 130 dmax.append(-1e100) 131 new_data_min.append(dmin) 132 new_data_max.append(dmax) 133 134 new_data_min = np.array(new_data_min).astype(float) 135 new_data_max = np.array(new_data_max).astype(float) 136 min_pos = np.nanargmin(new_data_min, axis=bold_axis) 137 max_pos = np.nanargmax(new_data_max, axis=bold_axis) 138 139 # Data 140 for i in range(data.shape[0]): 141 l = "\t\t" 142 for j in range(data.shape[1]): 143 144 d = data[i,j] 145 if type(d) is float: d = round(d, round_val) 146 d = str(d).strip() 147 148 if bold_axis is None: 149 l+= "{:<1s}{}{:<1s}{}".format("", d, "", "&") 150 elif bold_axis == 0: 151 if min_pos[j] == i: 152 l+= "{:<1s}{}{:<1s}{}".format("", "\\bfseries{"+d+"}", "", "&") 153 elif max_pos[j] == i: 154 l+= "{:<1s}{}{:<1s}{}".format("", "\\textit{"+d+"}", "", "&") 155 else: 156 l+= "{:<1s}{}{:<1s}{}".format("", d, "", "&") 157 elif bold_axis == 1: 158 if min_pos[i] == j: 159 l+= "{:<1s}{}{:<1s}{}".format("", "\\bfseries{"+d+"}", "", "&") 160 elif max_pos[i] == j: 161 l+= "{:<1s}{}{:<1s}{}".format("", "\\textit{"+d+"}", "", "&") 162 else: 163 l+= "{:<1s}{}{:<1s}{}".format("", d, "", "&") 164 165 l = l[:-1] 166 167 p += l + "\\\\\n" 168 p += "\t\t\\bottomrule\n" 169 p += "\t\\end{tabular}}\n" 170 p += "\\end{table}\n" 171 172 if preamble: 173 # End document 174 p += "\n\\end{document}\n" 175 176 return p
def
table( columns_name: list, data: numpy.ndarray, round_val: int = 4, bold_axis: int = None, caption: str = 'table_caption', label: str = 'table_label', preamble: bool = False) -> str:
4def table(columns_name : list, data : np.ndarray, round_val : int = 4, bold_axis : int = None, caption : str = "table_caption", label : str = "table_label", preamble : bool = False) -> str: 5 ''' 6 Produces LaTeX code to display a table. 7 8 Parameters: 9 ----------- 10 - columns_name 11 list of strings containing table columns name 12 - data : np.ndarray 13 2D ndarray containing data used to fill the table 14 - round_val : int 15 integer representing the decimal rounding 16 - bold_axis : int 17 integer representing the axis to wich get the max value and to set bold, if None no bold text will be added 18 if 0 the maximum will be calculated column-wise, if 1 the maximum will be calculated row-wise 19 - caption : str 20 string for the caption of LaTeX table (default: "table_caption") 21 - label : str 22 string for the label of LaTeX table (default: "table_label") 23 - preamble : bool 24 If True the function will return a full LaTeX document, if False the function will return only the table (default: False) 25 26 Returns: 27 -------- 28 - p : str 29 LaTeX code to display a table 30 31 Usage: 32 ------ 33 34 ```python 35 import numpy as np 36 37 columns_name = ['A', 'B', 'C'] 38 data = np.array( 39 [ 40 [0.1, 0.2, 0.3], 41 [0.4, 0.5, 0.6], 42 [0.7, 0.8, 0.9] 43 ] 44 ) 45 46 latex_table = table(columns_name, data, caption='My table 1', label='tab1', preamble=True) 47 ``` 48 49 Output: 50 ------- 51 52 ```latex 53 \\documentclass[11pt]{article} 54 \\usepackage{booktabs} 55 \\usepackage{graphicx} 56 57 \\begin{document} 58 59 \\begin{table}[!ht] 60 \\centering 61 \\caption{My table 1}\\label{tab:tab1} 62 \\resizebox{\\columnwidth}{!}{ 63 \\begin{tabular}{ccc} 64 \\toprule 65 A & B & C \\\\ 66 \\midrule 67 0.1 & 0.2 & 0.3 \\\\ 68 0.4 & 0.5 & 0.6 \\\\ 69 0.7 & 0.8 & 0.9 \\\\ 70 1.1 & 1.2 & 1.3 \\\\ 71 \\bottomrule 72 \\end{tabular}} 73 \\end{table} 74 75 \\end{document} 76 ``` 77 ''' 78 79 if len(data.shape) != 2: 80 raise Exception("Error Message: shape of data must be equals to two.") 81 82 if columns_name is not None: 83 if data.shape[1] != len(columns_name): 84 raise Exception("Error Message: mismatch between number of columns and shape of data") 85 86 if round_val < 1: 87 round_val = 1 88 89 if bold_axis is not None: 90 if bold_axis < 0 or bold_axis > 1: 91 bold_axis = None 92 93 p = "" 94 # LaTeX preamble 95 if preamble: 96 p += "\\documentclass[11pt]{article}\n" 97 p += "\\usepackage{booktabs}\n" 98 p += "\\usepackage{graphicx}\n\n" 99 p += "\\begin{document}\n\n" 100 101 # Table 102 p += "\\begin{table}[!ht]\n" 103 p += "\t\\centering\n" 104 p += "\t\\caption{"+str(caption)+"}\\label{tab:"+label+"}\n" 105 p += "\t\\resizebox{\\columnwidth}{!}{\n" 106 p += "\t\\begin{tabular}{" + "".join([char*data.shape[1] for char in "c"]) + '}\n' 107 p += "\t\t\\toprule\n" 108 109 if columns_name is not None: 110 # Columns name 111 l = "\t\t" 112 for i in range(len(columns_name)): 113 l+= "{:<1s}{}{:<1s}{}".format("", str(columns_name[i].strip()), "", "&") 114 l = l[:-1] 115 p += l + "\\\\\n" 116 p += "\t\t\\midrule\n" 117 118 if bold_axis is not None: 119 120 new_data_min = [] 121 new_data_max = [] 122 for x in range(data.shape[0]): 123 dmin = [] 124 dmax = [] 125 for y in range(data.shape[1]): 126 try: 127 dmin.append(float(data[x,y])) 128 dmax.append(float(data[x,y])) 129 except Exception: 130 dmin.append(+1e100) 131 dmax.append(-1e100) 132 new_data_min.append(dmin) 133 new_data_max.append(dmax) 134 135 new_data_min = np.array(new_data_min).astype(float) 136 new_data_max = np.array(new_data_max).astype(float) 137 min_pos = np.nanargmin(new_data_min, axis=bold_axis) 138 max_pos = np.nanargmax(new_data_max, axis=bold_axis) 139 140 # Data 141 for i in range(data.shape[0]): 142 l = "\t\t" 143 for j in range(data.shape[1]): 144 145 d = data[i,j] 146 if type(d) is float: d = round(d, round_val) 147 d = str(d).strip() 148 149 if bold_axis is None: 150 l+= "{:<1s}{}{:<1s}{}".format("", d, "", "&") 151 elif bold_axis == 0: 152 if min_pos[j] == i: 153 l+= "{:<1s}{}{:<1s}{}".format("", "\\bfseries{"+d+"}", "", "&") 154 elif max_pos[j] == i: 155 l+= "{:<1s}{}{:<1s}{}".format("", "\\textit{"+d+"}", "", "&") 156 else: 157 l+= "{:<1s}{}{:<1s}{}".format("", d, "", "&") 158 elif bold_axis == 1: 159 if min_pos[i] == j: 160 l+= "{:<1s}{}{:<1s}{}".format("", "\\bfseries{"+d+"}", "", "&") 161 elif max_pos[i] == j: 162 l+= "{:<1s}{}{:<1s}{}".format("", "\\textit{"+d+"}", "", "&") 163 else: 164 l+= "{:<1s}{}{:<1s}{}".format("", d, "", "&") 165 166 l = l[:-1] 167 168 p += l + "\\\\\n" 169 p += "\t\t\\bottomrule\n" 170 p += "\t\\end{tabular}}\n" 171 p += "\\end{table}\n" 172 173 if preamble: 174 # End document 175 p += "\n\\end{document}\n" 176 177 return p
Produces LaTeX code to display a table.
Parameters:
- columns_name
list of strings containing table columns name - data : np.ndarray 2D ndarray containing data used to fill the table
- round_val : int integer representing the decimal rounding
- bold_axis : int integer representing the axis to wich get the max value and to set bold, if None no bold text will be added if 0 the maximum will be calculated column-wise, if 1 the maximum will be calculated row-wise
- caption : str
string for the caption of LaTeX table (default: "table_caption") - label : str
string for the label of LaTeX table (default: "table_label") - preamble : bool
If True the function will return a full LaTeX document, if False the function will return only the table (default: False)
Returns:
- p : str
LaTeX code to display a table
Usage:
import numpy as np
columns_name = ['A', 'B', 'C']
data = np.array(
[
[0.1, 0.2, 0.3],
[0.4, 0.5, 0.6],
[0.7, 0.8, 0.9]
]
)
latex_table = table(columns_name, data, caption='My table 1', label='tab1', preamble=True)
Output:
\documentclass[11pt]{article}
\usepackage{booktabs}
\usepackage{graphicx}
\begin{document}
\begin{table}[!ht]
\centering
\caption{My table 1}\label{tab:tab1}
\resizebox{\columnwidth}{!}{
\begin{tabular}{ccc}
\toprule
A & B & C \\
\midrule
0.1 & 0.2 & 0.3 \\
0.4 & 0.5 & 0.6 \\
0.7 & 0.8 & 0.9 \\
1.1 & 1.2 & 1.3 \\
\bottomrule
\end{tabular}}
\end{table}
\end{document}