16 #include <deal.II/base/table.h> 17 #include <deal.II/base/table_handler.h> 19 #include <boost/io/ios_state.hpp> 26 DEAL_II_NAMESPACE_OPEN
45 return boost::get<int>(
value);
54 return boost::get<unsigned int>(
value);
62 return boost::get<unsigned long long int>(
value);
70 return boost::get<double>(
value);
75 ExcMessage(
"The number stored by this element of the " 76 "table is not a number."))
85 std::ostringstream ss;
87 ss << std::setprecision(precision);
90 ss.setf(std::ios::scientific, std::ios::floatfield);
92 ss.setf(std::ios::fixed, std::ios::floatfield);
112 struct GetDefaultValue :
public boost::static_visitor<>
114 template <
typename T>
116 operator()(T &operand)
const 127 boost::apply_visitor(Local::GetDefaultValue(), new_entry.
value);
138 : tex_caption(tex_caption)
184 for (std::vector<::internal::TableEntry>::iterator it =
entries.begin();
191 static_cast<unsigned int>(it->get_cached_string().length()));
210 ExcMessage(
"You are trying to declare a column with key <" + key +
211 "> but such a column already exists."));
223 unsigned int max_col_length = 0;
224 for (std::map<std::string, Column>::iterator p =
columns.begin();
228 std::max(max_col_length,
229 static_cast<unsigned int>(p->second.entries.size()));
233 for (std::map<std::string, Column>::iterator col =
columns.begin();
236 while (col->second.entries.size() < max_col_length)
238 col->second.entries.emplace_back(
"");
240 entry.
cache_string(col->second.scientific, col->second.precision);
241 col->second.max_length =
242 std::max(col->second.max_length,
243 static_cast<unsigned int>(
259 const std::string &superkey)
265 std::pair<std::string, std::vector<std::string>> new_column(
266 superkey, std::vector<std::string>());
281 for (std::vector<std::string>::iterator order_iter =
column_order.begin();
284 if (*order_iter == key)
296 std::pair<std::string, std::string> new_tex_supercaption(superkey,
309 for (
unsigned int j = 0; j < new_order.size(); ++j)
319 const std::string &tex_caption)
322 columns[key].tex_caption = tex_caption;
345 const std::string &tex_supercaption)
356 const std::string &tex_format)
359 Assert(tex_format ==
"l" || tex_format ==
"c" || tex_format ==
"r",
361 columns[key].tex_format = tex_format;
368 const unsigned int precision)
371 if (
columns[key].precision != precision)
373 columns[key].precision = precision;
374 columns[key].invalidate_cache();
383 if (
columns[key].scientific != scientific)
385 columns[key].scientific = scientific;
386 columns[key].invalidate_cache();
395 boost::io::ios_flags_saver restore_flags(out);
400 unsigned int max_rows = 0;
401 for (std::map<std::string, Column>::const_iterator p =
columns.begin();
404 max_rows = std::max<unsigned int>(max_rows, p->second.entries.size());
406 for (std::map<std::string, Column>::iterator p =
columns.begin();
409 p->second.pad_column_below(max_rows);
412 std::vector<std::string> sel_columns;
415 const unsigned int nrows =
n_rows();
416 const unsigned int n_cols = sel_columns.size();
419 std::vector<const Column *> cols;
420 std::vector<unsigned int> column_widths(n_cols, 0);
421 for (
unsigned int j = 0; j < n_cols; ++j)
423 std::string key = sel_columns[j];
424 const std::map<std::string, Column>::const_iterator col_iter =
427 cols.push_back(&(col_iter->second));
429 column_widths[j] = col_iter->second.max_length;
437 out <<
"| " << std::left;
438 for (
unsigned int j = 0; j < n_cols; ++j)
440 const std::string &key = sel_columns[j];
442 std::max(column_widths[j], (
unsigned int)key.length());
443 out << std::setw(column_widths[j]);
449 for (
unsigned int i = 0; i < nrows; ++i)
452 for (
unsigned int j = 0; j < n_cols; ++j)
454 const Column &column = *(cols[j]);
456 out << std::setw(column_widths[j]);
457 out << column.
entries[i].get_cached_string();
470 for (
unsigned int j = 0; j < n_cols; ++j)
472 const std::string &key = sel_columns[j];
473 out <<
"# " << j + 1 <<
": " << key <<
'\n';
477 for (
unsigned int i = 0; i < nrows; ++i)
479 for (
unsigned int j = 0; j < n_cols; ++j)
481 const Column &column = *(cols[j]);
483 out << column.
entries[i].get_cached_string();
498 for (
unsigned int j = 0; j < n_cols; ++j)
500 std::string key = sel_columns[j];
501 out <<
"# " << j + 1 <<
": " << key <<
'\n';
513 unsigned int width = 0;
516 const std::map<std::string,
517 std::vector<std::string>>::const_iterator
521 const unsigned int n_subcolumns = super_iter->second.size();
522 for (
unsigned int k = 0; k < n_subcolumns; ++k)
524 const std::map<std::string, Column>::const_iterator
525 col_iter =
columns.find(super_iter->second[k]);
528 width += col_iter->second.max_length;
530 width += n_subcolumns - 1;
534 const std::map<std::string, Column>::const_iterator
537 width = col_iter->second.max_length;
542 if (width < key.length())
548 const std::map<std::string,
549 std::vector<std::string>>::const_iterator
552 colname = super_iter->second.back();
557 for (
unsigned int i = 0; i < n_cols; ++i)
559 if (sel_columns[i] == colname)
561 column_widths[i] += key.length() - width;
566 width = key.length();
570 const unsigned int front_padding = (width - key.length()) / 2,
572 (width - key.length()) - front_padding;
573 for (
unsigned int i = 0; i < front_padding; ++i)
576 for (
unsigned int i = 0; i < rear_padding; ++i)
592 for (
unsigned int i = 0; i < nrows; ++i)
594 for (
unsigned int j = 0; j < n_cols; ++j)
596 const Column &column = *(cols[j]);
597 out << std::setw(column_widths[j]);
598 out << column.
entries[i].get_cached_string();
616 out <<
"\\documentclass[10pt]{report}" << std::endl
617 <<
"\\usepackage{float}" << std::endl
620 <<
"\\begin{document}" << std::endl;
622 out <<
"\\begin{table}[H]" << std::endl
623 <<
"\\begin{center}" << std::endl
624 <<
"\\begin{tabular}{|";
629 unsigned int max_rows = 0;
630 for (std::map<std::string, Column>::const_iterator p =
columns.begin();
633 max_rows = std::max<unsigned int>(max_rows, p->second.entries.size());
635 for (std::map<std::string, Column>::iterator p =
columns.begin();
638 p->second.pad_column_below(max_rows);
641 std::vector<std::string> sel_columns;
649 const std::map<std::string, std::vector<std::string>>::const_iterator
654 const unsigned int n_subcolumns = super_iter->second.size();
655 for (
unsigned int k = 0; k < n_subcolumns; ++k)
658 const std::map<std::string, Column>::const_iterator col_iter =
659 columns.find(super_iter->second[k]);
662 out << col_iter->second.tex_format <<
"|";
668 const std::map<std::string, Column>::const_iterator col_iter =
671 out << col_iter->second.tex_format <<
"|";
674 out <<
"} \\hline" << std::endl;
681 const std::map<std::string, std::vector<std::string>>::const_iterator
686 const unsigned int n_subcolumns = super_iter->second.size();
688 std::map<std::string, std::string>::const_iterator
691 <<
"\\multicolumn{" << n_subcolumns <<
"}{|c|}{" 692 << tex_super_cap_iter->second <<
"}";
697 const std::map<std::string, Column>::const_iterator col_iter =
700 out << col_iter->second.tex_caption;
705 out <<
"\\\\ \\hline" << std::endl;
708 const unsigned int nrows =
n_rows();
709 for (
unsigned int i = 0; i < nrows; ++i)
711 const unsigned int n_cols = sel_columns.size();
713 for (
unsigned int j = 0; j < n_cols; ++j)
715 std::string key = sel_columns[j];
717 const std::map<std::string, Column>::const_iterator col_iter =
721 const Column &column = col_iter->second;
723 out << std::setprecision(column.precision);
725 if (col_iter->second.scientific)
726 out.setf(std::ios::scientific, std::ios::floatfield);
728 out.setf(std::ios::fixed, std::ios::floatfield);
730 out << column.entries[i].value;
735 out <<
"\\\\ \\hline" << std::endl;
738 out <<
"\\end{tabular}" << std::endl <<
"\\end{center}" << std::endl;
743 out <<
"\\end{table}" << std::endl;
745 out <<
"\\end{document}" << std::endl;
768 std::map<std::string, Column>::const_iterator col_iter =
columns.begin();
769 unsigned int n = col_iter->second.entries.size();
770 std::string first_name = col_iter->first;
772 for (++col_iter; col_iter !=
columns.end(); ++col_iter)
773 Assert(col_iter->second.entries.size() == n,
775 col_iter->first, col_iter->second.entries.size(), first_name, n));
789 const std::map<std::string, std::vector<std::string>>::const_iterator
795 const unsigned int n_subcolumns = super_iter->second.size();
796 for (
unsigned int k = 0; k < n_subcolumns; ++k)
798 const std::string subkey = super_iter->second[k];
800 sel_columns.push_back(subkey);
807 sel_columns.push_back(key);
818 std::vector<internal::TableEntry>::size_type n = 0;
819 for (std::map<std::string, Column>::iterator p =
columns.begin();
822 n = std::max(n, p->second.entries.size());
826 for (std::map<std::string, Column>::iterator p =
columns.begin();
829 if (p->second.entries.size() == n)
830 p->second.entries.pop_back();
834 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)
std::map< std::string, Column > columns
static::ExceptionBase & ExcColumnOrSuperColumnNotExistent(std::string arg1)
void set_tex_caption(const std::string &key, const std::string &tex_caption)
#define AssertThrow(cond, exc)
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")
static::ExceptionBase & ExcSuperColumnNotExistent(std::string arg1)
void get_selected_columns(std::vector< std::string > &sel_columns) const
static::ExceptionBase & ExcWrongNumberOfDataEntries(std::string arg1, int arg2, std::string arg3, int arg4)
std::vector< std::string > column_order
void cache_string(bool scientific, unsigned int precision) const
static::ExceptionBase & ExcMessage(std::string arg1)
std::string tex_table_caption
#define Assert(cond, exc)
void write_tex(std::ostream &file, const bool with_header=true) const
void set_tex_table_label(const std::string &table_label)
const std::string & get_cached_string() const
static::ExceptionBase & ExcUndefinedTexFormat(std::string arg1)
double get_numeric_value() const
void set_column_order(const std::vector< std::string > &new_order)
unsigned int n_rows() const
void set_scientific(const std::string &key, const bool scientific)
void pad_column_below(const unsigned int length)
TableEntry get_default_constructed_copy() const
static::ExceptionBase & ExcColumnNotExistent(std::string arg1)
std::vector< internal::TableEntry > entries
void set_tex_table_caption(const std::string &table_caption)
std::map< std::string, std::vector< std::string > > supercolumns
void write_text(std::ostream &out, const TextOutputFormat format=table_with_headers) const
std::string tex_table_label
static::ExceptionBase & ExcInternalError()
void set_auto_fill_mode(const bool state)