TexBuilder.h
Go to the documentation of this file.
1 
2 typedef struct Table Table;
3 struct Table {
7  std::vector< std::string > header;
8  std::vector< std::vector< std::string > > rows;
9  // Always make a csv for the table as well
12 };
13 
14 
15 class TexBuilder{
16 
17 public:
18 
21  std::string authors,
22  std::string abstract );
23  ~TexBuilder();
24 
25  void AddText( std::string text);
26 
27  void Section ( std::string secname );
28  void SubSection( std::string subsecname );
29  void SectionNoNum ( std::string secname );
30  void SubSectionNoNum( std::string subsecname );
31 
32  void AddFigureRow( std::vector<std::string> figure,
33  std::vector<std::string> caption,
34  std::vector<std::string> label);
35 
36  void WriteHeader();
37  void NewPage();
38 
39  void AddTable( Table table, bool drawnow=false, bool makecsv=true );
40  void DrawTable( std::string tabname );
41 
42  void Close();
43 
44 private:
49  std::ofstream fTex;
50 
51  std::map< std::string, Table > fTables;
52 };
53 
54 
55 //^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^
58  std::string authors,
59  std::string abstract )
60  : fFileName(filename)
61  , fTitle(title)
62  , fAuthors(authors)
63  , fAbstract(abstract)
64 {
65  fTex.open(filename);
66 
67  if(!fTex.is_open())
68  std::cout << "\nFailed to open " << filename << std::endl;
69  else
70  std::cout << "\nBuilding Tex in " << filename << std::endl;
71 
72  WriteHeader();
73 
74  fTex << "\n\\newpage\n";
75 }
76 
78 {
79  // doesn't always work here?
80  //fTex << "\\end{document}\n";
81  //fTex.close();
82 }
83 
84 //^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^
86 {
87  fTex << "\\end{document}\n";
88  fTex.close();
89 }
90 
91 //^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^
93 {
94  fTex << text;
95 }
96 
97 //^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^
98 void TexBuilder::AddFigureRow( std::vector<std::string> figure,
99  std::vector<std::string> caption,
100  std::vector<std::string> label)
101 {
102  unsigned short nfig = figure.size();
103  if( nfig!= caption.size() ){
104  std::cout << "Bad number of captions, skipping " << figure[0] << std::endl; return; }
105  if( nfig!= label.size() ){
106  std::cout << "Bad number of labels, skipping " << figure[0] << std::endl; return; }
107 
108  float size;
109  if (nfig==1) size = 0.2; // height
110  else if(nfig==2) size = 0.49; // width
111  else if(nfig==3) size = 0.32; // width
112  else if(nfig==4) size = 0.24; // width
113 
114 
115  fTex << "\\begin{figure}[h!]\n";
116 
117  // set single figures by height
118  if(nfig==1){
119  fTex << "\\centering\n";
120  fTex << " \\includegraphics[height="<<size<<"\\linewidth]{" << figure[0] <<".pdf}\n";
121  if(caption[0]!="")
122  fTex << " \\caption{" << caption[0] << "}\n";
123  if(label[0]!="")
124  fTex << " \\label{fig:"<< label[0] << "}\n";
125  } else {
126 
127  // set multiple subfigures by width
128  for( unsigned short f=0; f<nfig; f++){
129  fTex << " \\begin{subfigure}{"<<size<<"\\textwidth}\n";
130  fTex << " \\includegraphics[width=\\linewidth]{" << figure[f] <<".pdf}\n";
131  if(caption[f]!="")
132  fTex << " \\caption{" << caption[f] << "}\n";
133  if(label[f]!="")
134  fTex << " \\label{fig:"<< label[f] << "}\n";
135  fTex << " \\end{subfigure}\n";
136  if(f<nfig-1)
137  fTex << " \\quad\n";
138 
139  } // each figure
140  }
141 
142  fTex << "\\end{figure}\n";
143 
144  return;
145 }
146 
147 
148 //^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^
150 {
151  fTex << "\\section{"+name+"}\n\n";
152 }
154 {
155  fTex << "\\section*{"+name+"}\n\n";
156 }
158 {
159  fTex << "\\subsection{"+name+"}\n\n";
160 }
162 {
163  fTex << "\\subsection*{"+name+"}\n\n";
164 }
166 {
167  fTex << "\n\\newpage\n";
168 }
169 
170 
171 
172 //^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^
174 {
175 
176  fTex << "\\documentclass[12pt]{article}\n";
177 
178  fTex << "\\usepackage[utf8]{inputenc}\n";
179  fTex << "\\usepackage{graphicx}\n";
180  fTex << "\\usepackage{caption}\n";
181  fTex << "\\usepackage{subcaption}\n";
182  fTex << "\\usepackage[dvipsnames,table]{xcolor}\n";
183  fTex << "\\usepackage[a4paper, margin=1.5cm]{geometry}\n";
184  fTex << "\\usepackage{fancyhdr}\n";
185  fTex << "\\usepackage[colorlinks=true]{hyperref}\n";
186 
187  fTex << "\\newcommand{\\mycolor}{WildStrawberry}\n";
188  fTex << "\\newcommand{\\myhighlightA}{Goldenrod!30!white}\n";
189  fTex << "\\newcommand{\\myhighlightB}{Apricot!50!white}\n";
190  fTex << "\\newcommand{\\myhighlightC}{Rhodamine!30!white}\n";
191 
192  fTex << "\\pagestyle{fancy}\n";
193  fTex << "\\rhead{\\nouppercase{\\leftmark}}\n";
194 
195  fTex << "\\usepackage{etoolbox}\n";
196  fTex << "\\renewcommand{\\headrulewidth}{1pt}\n";
197  fTex << "\\fancyhead[CO,CE]{\\hyperlink{toc}{Back To Top}}\n";
198  fTex << "%\\fancyhead[LO,LE]{\\hyperlink{sec}{Section Top}}\n";
199 
200  fTex << "\\makeatletter";
201  fTex << "\\patchcmd{\\@fancyhead}{\\rlap}{\\color{\\mycolor!60!white}\\rlap}{}{}\n";
202  fTex << "\\patchcmd{\\headrule}{\\hrule}{\\color{\\mycolor!60!white}\\hrule}{}{}\n";
203  fTex << "\\patchcmd{\\@fancyfoot}{\\rlap}{\\color{\\mycolor!60!white}\\rlap}{}{}\n";
204  fTex << "\\patchcmd{\\footrule}{\\hrule}{\\color{\\mycolor!60!white}\\hrule}{}{}\n";
205  fTex << "\\makeatother";
206  fTex << "\\usepackage{sectsty}\n";
207  fTex << "\\allsectionsfont{\\color{\\mycolor}\\bfseries}\n";
208  fTex << "\\usepackage{longtable}\n";
209 
210  fTex << "\\hypersetup{linkcolor=black}\n";
211 
212  fTex << "\\usepackage{geometry}\n";
213  fTex << "\\geometry{headheight=15pt}\n";
214 
215  fTex << "\\setlength{\\intextsep}{0.005\\textheight}\n";
216 
217  fTex << std::endl;
218  fTex << "\\begin{document}\n\n";
219  fTex << std::endl;
220 
221  fTex << "\\title{\\textcolor{\\mycolor}{\\bf " << fTitle << "}}\n";
222  fTex << "\\author{" << fAuthors << "}\n";
223  fTex << "\\date{\\today}\n\n";
224 
225  fTex << "\\maketitle\n\n";
226 
227  fTex << "\\begin{abstract}\n";
228  fTex << fAbstract;
229  fTex << "\\end{abstract}\n\n";
230 
231  fTex << "\\tableofcontents\n\n";
232 
233 } // WriteHeader()
234 
235 
236 //^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^
237 void TexBuilder::AddTable( Table table, bool addnow, bool makecsv )
238 {
239  if(fTables.count(table.name)){
240  std::cout << "Table " << table.name << " already exists, skip" << std::endl;
241  return;
242  }
243  // Rester the new table
244  fTables[table.name] = table;
245 
246  // Immediately make CSV
247  if(makecsv){
248  ofstream csv (Form("%s/%s",
249  table.csvdir.c_str(),
250  table.csvname.c_str()) );
251  if(!csv.is_open()) std::cout << "File not found" << std::endl;
252 
253  csv << "#";
254  for(size_t i=0; i<table.header.size(); i++){
255  csv << table.header[i];
256  if(i!=table.header.size()-1) csv << ",";
257  }
258  csv << std::endl;
259 
260  for( auto& row: table.rows ){
261  for( size_t c=0; c<row.size(); c++ ){
262  csv << row[c];
263  if(c!=row.size()-1) csv << ",";
264  }
265  csv << std::endl;
266  }
267  csv.close();
268  }
269 
270  // Maybe add to Tex now
271  if(addnow) DrawTable(table.name);
272 }
273 
274 
275 //^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^
277 {
278  if(!fTables.count(tabname)){
279  std::cout << "no table for " << tabname << ", skip" << std::endl;
280  for(auto& t : fTables) std::cout << " " << t.first << std::endl;
281  return;
282  }
283  const Table &tab = fTables[tabname];
284 
285  // center all for now
286  std::string tabulararg = "|";
287  for(auto& cell : tab.header) tabulararg += "c|";
288 
289  fTex << "\\begin{center}\n";
290  fTex << "\\begin{table}[h!]\n";
291  fTex << "\\begin{tabular}{ " << tabulararg << "}\n\n";
292 
293  fTex << "\\hline" << std::endl;
294  for( size_t c=0; c<tab.header.size(); c++ ){
295  std::string head = tab.header[c];
296  std::replace( head.begin(), head.end(), '_', ' ');
297  if( c<tab.header.size()-1 )
298  fTex << head << " & ";
299  else
300  fTex << head << "\\\\ \\hline" << std::endl;
301  }
302 
303  for( auto& row: tab.rows ){
304  for( size_t c=0; c<row.size(); c++ ){
305  std::string cell = row[c];
306  std::replace( cell.begin(), cell.end(), '_', ' ');
307  if( c<row.size()-1 )
308  fTex << cell << " & ";
309  else
310  fTex << cell;
311  }
312  fTex << "\\\\ \\hline" << std::endl;
313  }
314 
315  fTex << "\\end{tabular}\n";
316  if( tab.caption!="" )
317  fTex << "\\caption{" << tab.caption << "}\n";
318  fTex << "\\label{tab:" << tab.name << "}\n";
319  fTex << "\\end{table}\n";
320  fTex << "\\end{center}\n\n";
321 
322 }
std::vector< std::string > header
Definition: TexBuilder.h:7
const XML_Char * name
Definition: expat.h:151
void AddTable(Table table, bool drawnow=false, bool makecsv=true)
Definition: TexBuilder.h:237
std::string caption
Definition: TexBuilder.h:6
void DrawTable(std::string tabname)
Definition: TexBuilder.h:276
void NewPage()
Definition: TexBuilder.h:165
void WriteHeader()
Definition: TexBuilder.h:173
string fFileName
Definition: nue_pid_effs.C:43
std::string fAbstract
Definition: TexBuilder.h:48
string filename
Definition: shutoffs.py:106
TexBuilder(std::string filename, std::string title, std::string authors, std::string abstract)
Definition: TexBuilder.h:56
void Close()
Definition: TexBuilder.h:85
void AddText(std::string text)
Definition: TexBuilder.h:92
Eigen::Matrix< T, Eigen::Dynamic, 1 > head(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &v, size_t n)
Definition: head.hpp:24
const char * label
std::string name
Definition: TexBuilder.h:4
std::vector< std::vector< std::string > > rows
Definition: TexBuilder.h:8
std::map< std::string, Table > fTables
Definition: TexBuilder.h:51
std::string fAuthors
Definition: TexBuilder.h:47
std::ofstream fTex
Definition: TexBuilder.h:49
std::string csvdir
Definition: TexBuilder.h:11
c1 Close()
void SubSectionNoNum(std::string subsecname)
Definition: TexBuilder.h:161
void SectionNoNum(std::string secname)
Definition: TexBuilder.h:153
OStream cout
Definition: OStream.cxx:6
std::string fTitle
Definition: TexBuilder.h:46
std::string title
Definition: TexBuilder.h:5
std::string csvname
Definition: TexBuilder.h:10
void Section(std::string secname)
Definition: TexBuilder.h:149
std::string fFileName
Definition: TexBuilder.h:45
void SubSection(std::string subsecname)
Definition: TexBuilder.h:157
void AddFigureRow(std::vector< std::string > figure, std::vector< std::string > caption, std::vector< std::string > label)
Definition: TexBuilder.h:98
enum BeamMode string