16 #include <deal.II/base/table_handler.h> 17 #include <deal.II/base/table.h> 19 #include <boost/io/ios_state.hpp> 26 DEAL_II_NAMESPACE_OPEN
44 return boost::get<int>(
value);
53 return boost::get<unsigned int>(
value);
61 return boost::get<unsigned long long int>(
value);
69 return boost::get<double>(
value);
74 "table is not a number."))
82 std::ostringstream ss;
84 ss << std::setprecision(precision);
87 ss.setf(std::ios::scientific, std::ios::floatfield);
89 ss.setf(std::ios::fixed, std::ios::floatfield);
108 struct GetDefaultValue :
public boost::static_visitor<>
110 template <
typename T>
111 void operator()( T &operand )
const 121 boost::apply_visitor(Local::GetDefaultValue(), new_entry.
value);
133 tex_caption(tex_caption),
163 while (entries.size() < size)
165 entries.push_back (entries.back().get_default_constructed_copy());
168 max_length = std::max(max_length, static_cast<unsigned int>(entry.
get_cached_string().length()));
178 for (std::vector<::internal::TableEntry>::iterator it=entries.begin(); it!=entries.end(); ++it)
180 it->cache_string(this->scientific, this->precision);
181 max_length = std::max(max_length, static_cast<unsigned int>(it->get_cached_string().length()));
200 ExcMessage (
"You are trying to declare a column with key <" + key +
201 "> but such a column already exists."));
212 unsigned int max_col_length = 0;
213 for (std::map< std::string, Column >::iterator p =
columns.begin(); p !=
columns.end(); ++p)
214 max_col_length = std::max(max_col_length,
215 static_cast<unsigned int>(p->second.entries.size()));
219 for (std::map<std::string,Column>::iterator col=
columns.begin(); col!=
columns.end(); ++col)
220 while (col->second.entries.size() < max_col_length)
222 col->second.entries.emplace_back(
"");
224 entry.
cache_string(col->second.scientific, col->second.precision);
225 col->second.max_length = std::max(col->second.max_length,
240 const std::string &superkey)
246 std::pair<std::string, std::vector<std::string> >
247 new_column(superkey, std::vector<std::string>());
262 for (std::vector<std::string>::iterator order_iter=
column_order.begin();
264 if (*order_iter==key)
276 std::pair<std::string, std::string> new_tex_supercaption(superkey, superkey);
287 for (
unsigned int j=0; j<new_order.size(); ++j)
296 const std::string &tex_caption)
299 columns[key].tex_caption=tex_caption;
319 const std::string &tex_supercaption)
329 const std::string &tex_format)
332 Assert(tex_format==
"l" || tex_format==
"c" || tex_format==
"r",
334 columns[key].tex_format=tex_format;
340 const unsigned int precision)
343 if (
columns[key].precision!=precision)
345 columns[key].precision = precision;
346 columns[key].invalidate_cache();
352 const bool scientific)
355 if (
columns[key].scientific!=scientific)
357 columns[key].scientific = scientific;
358 columns[key].invalidate_cache();
367 boost::io::ios_flags_saver restore_flags(out);
372 unsigned int max_rows = 0;
373 for (std::map<std::string, Column>::const_iterator p =
columns.begin();
375 max_rows = std::max<unsigned int>(max_rows, p->second.entries.size());
377 for (std::map<std::string, Column>::iterator p =
columns.begin();
379 p->second.pad_column_below (max_rows);
382 std::vector<std::string> sel_columns;
385 const unsigned int nrows =
n_rows();
386 const unsigned int n_cols = sel_columns.size();
389 std::vector<const Column *> cols;
390 std::vector<unsigned int> column_widths (n_cols, 0);
391 for (
unsigned int j=0; j<n_cols; ++j)
393 std::string key=sel_columns[j];
394 const std::map<std::string, Column>::const_iterator
397 cols.push_back(&(col_iter->second));
399 column_widths[j] = col_iter->second.max_length;
407 out <<
"| " << std::left;
408 for (
unsigned int j=0; j<n_cols; ++j)
410 const std::string &key = sel_columns[j];
411 column_widths[j] = std::max(column_widths[j],
412 (
unsigned int)key.length());
413 out << std::setw(column_widths[j]);
419 for (
unsigned int i=0; i<nrows; ++i)
422 for (
unsigned int j=0; j<n_cols; ++j)
424 const Column &column=*(cols[j]);
426 out << std::setw(column_widths[j]);
427 out << column.
entries[i].get_cached_string();
440 for (
unsigned int j=0; j<n_cols; ++j)
442 const std::string &key = sel_columns[j];
443 out <<
"# " << j+1 <<
": " << key <<
'\n';
447 for (
unsigned int i=0; i<nrows; ++i)
449 for (
unsigned int j=0; j<n_cols; ++j)
451 const Column &column=*(cols[j]);
453 out << column.
entries[i].get_cached_string();
468 for (
unsigned int j=0; j<n_cols; ++j)
470 std::string key=sel_columns[j];
471 out <<
"# " << j+1 <<
": " << key <<
'\n';
483 unsigned int width=0;
486 const std::map<std::string, std::vector<std::string> >::const_iterator
490 const unsigned int n_subcolumns=super_iter->second.size();
491 for (
unsigned int k=0; k<n_subcolumns; ++k)
493 const std::map<std::string, Column>::const_iterator
494 col_iter=
columns.find(super_iter->second[k]);
497 width += col_iter->second.max_length;
499 width += n_subcolumns - 1;
503 const std::map<std::string, Column>::const_iterator
506 width = col_iter->second.max_length;
511 if (width<key.length())
517 const std::map<std::string, std::vector<std::string> >::const_iterator
520 colname = super_iter->second.back();
525 for (
unsigned int i=0; i<n_cols; ++i)
527 if (sel_columns[i]==colname)
529 column_widths[i] += key.length() - width;
538 const unsigned int front_padding = (width-key.length())/2,
539 rear_padding = (width-key.length()) -
541 for (
unsigned int i=0; i<front_padding; ++i)
544 for (
unsigned int i=0; i<rear_padding; ++i)
560 for (
unsigned int i=0; i<nrows; ++i)
562 for (
unsigned int j=0; j<n_cols; ++j)
564 const Column &column=*(cols[j]);
565 out << std::setw(column_widths[j]);
566 out << column.
entries[i].get_cached_string();
583 out <<
"\\documentclass[10pt]{report}" << std::endl
584 <<
"\\usepackage{float}" << std::endl << std::endl << std::endl
585 <<
"\\begin{document}" << std::endl;
587 out <<
"\\begin{table}[H]" << std::endl
588 <<
"\\begin{center}" << std::endl
589 <<
"\\begin{tabular}{|";
594 unsigned int max_rows = 0;
595 for (std::map<std::string, Column>::const_iterator p =
columns.begin();
597 max_rows = std::max<unsigned int>(max_rows, p->second.entries.size());
599 for (std::map<std::string, Column>::iterator p =
columns.begin();
601 p->second.pad_column_below (max_rows);
604 std::vector<std::string> sel_columns;
612 const std::map<std::string, std::vector<std::string> >::const_iterator
617 const unsigned int n_subcolumns=super_iter->second.size();
618 for (
unsigned int k=0; k<n_subcolumns; ++k)
621 const std::map<std::string, Column>::const_iterator
622 col_iter=
columns.find(super_iter->second[k]);
625 out << col_iter->second.tex_format <<
"|";
631 const std::map<std::string, Column>::const_iterator
634 out << col_iter->second.tex_format <<
"|";
637 out <<
"} \\hline" << std::endl;
644 const std::map<std::string, std::vector<std::string> >::const_iterator
649 const unsigned int n_subcolumns=super_iter->second.size();
651 std::map<std::string,std::string>::const_iterator
653 out << std::endl <<
"\\multicolumn{" << n_subcolumns <<
"}{|c|}{" 654 << tex_super_cap_iter->second <<
"}";
659 const std::map<std::string, Column>::const_iterator
662 out << col_iter->second.tex_caption;
667 out <<
"\\\\ \\hline" << std::endl;
670 const unsigned int nrows=
n_rows();
671 for (
unsigned int i=0; i<nrows; ++i)
673 const unsigned int n_cols=sel_columns.size();
675 for (
unsigned int j=0; j<n_cols; ++j)
677 std::string key=sel_columns[j];
679 const std::map<std::string, Column>::const_iterator
683 const Column &column=col_iter->second;
685 out << std::setprecision(column.precision);
687 if (col_iter->second.scientific)
688 out.setf(std::ios::scientific, std::ios::floatfield);
690 out.setf(std::ios::fixed, std::ios::floatfield);
692 out << column.entries[i].value;
697 out <<
"\\\\ \\hline" << std::endl;
700 out <<
"\\end{tabular}" << std::endl
701 <<
"\\end{center}" << std::endl;
706 out <<
"\\end{table}" << std::endl;
708 out <<
"\\end{document}" << std::endl;
731 std::map<std::string, Column>::const_iterator col_iter =
columns.begin();
732 unsigned int n = col_iter->second.entries.size();
733 std::string first_name=col_iter->first;
735 for (++col_iter; col_iter!=
columns.end(); ++col_iter)
736 Assert(col_iter->second.entries.size()==n,
738 col_iter->second.entries.size(),
752 const std::map<std::string, std::vector<std::string> >::const_iterator
758 const unsigned int n_subcolumns=super_iter->second.size();
759 for (
unsigned int k=0; k<n_subcolumns; ++k)
761 const std::string subkey=super_iter->second[k];
763 sel_columns.push_back(subkey);
770 sel_columns.push_back(key);
780 std::vector<internal::TableEntry>::size_type n = 0;
781 for (std::map< std::string, Column >::iterator p =
columns.begin(); p !=
columns.end(); ++p)
782 n = std::max(n, p->second.entries.size());
786 for (std::map< std::string, Column >::iterator p =
columns.begin(); p !=
columns.end(); ++p)
787 if (p->second.entries.size() == n)
788 p->second.entries.pop_back();
792 DEAL_II_NAMESPACE_CLOSE
std::map< std::string, std::string > tex_supercaptions
void set_precision(const std::string &key, const unsigned int precision)
void declare_column(const std::string &key)
static ::ExceptionBase & ExcIO()
void set_tex_supercaption(const std::string &superkey, const std::string &tex_supercaption)
static ::ExceptionBase & ExcColumnOrSuperColumnNotExistent(std::string arg1)
const std::string & get_cached_string() const
void set_tex_caption(const std::string &key, const std::string &tex_caption)
#define AssertThrow(cond, exc)
void cache_string(bool scientific, unsigned int precision) const
void add_column_to_supercolumn(const std::string &key, const std::string &superkey)
void set_tex_format(const std::string &key, const std::string &format="c")
void get_selected_columns(std::vector< std::string > &sel_columns) const
static ::ExceptionBase & ExcSuperColumnNotExistent(std::string arg1)
static ::ExceptionBase & ExcWrongNumberOfDataEntries(std::string arg1, int arg2, std::string arg3, int arg4)
std::vector< std::string > column_order
static ::ExceptionBase & ExcMessage(std::string arg1)
std::string tex_table_caption
double get_numeric_value() const
TableEntry get_default_constructed_copy() const
void write_text(std::ostream &out, const TextOutputFormat format=table_with_headers) const
#define Assert(cond, exc)
void set_tex_table_label(const std::string &table_label)
void write_tex(std::ostream &file, const bool with_header=true) const
static ::ExceptionBase & ExcUndefinedTexFormat(std::string arg1)
void set_column_order(const std::vector< std::string > &new_order)
void set_scientific(const std::string &key, const bool scientific)
void pad_column_below(const unsigned int length)
static ::ExceptionBase & ExcColumnNotExistent(std::string arg1)
std::vector< internal::TableEntry > entries
std::map< std::string, Column > columns
unsigned int n_rows() const
void set_tex_table_caption(const std::string &table_caption)
std::map< std::string, std::vector< std::string > > supercolumns
std::string tex_table_label
static ::ExceptionBase & ExcInternalError()
void set_auto_fill_mode(const bool state)