Classes | |
struct | Column |
class | ExcColumnNotExistent |
class | ExcColumnOrSuperColumnNotExistent |
class | ExcSuperColumnNotExistent |
class | ExcUndefinedTexFormat |
class | ExcWrongNumberOfDataEntries |
Public Member Functions | |
TableHandler () | |
template<typename value_type > | |
void | add_value (const std::string &key, const value_type value) |
void | add_column_to_supercolumn (const std::string &key, const std::string &superkey) |
void | set_column_order (const std::vector< std::string > &new_order) |
void | set_precision (const std::string &key, const unsigned int precision) |
void | set_scientific (const std::string &key, const bool scientific) |
void | set_tex_caption (const std::string &key, const std::string &tex_caption) |
void | set_tex_table_caption (const std::string &table_caption) |
void | set_tex_table_label (const std::string &table_label) |
void | set_tex_supercaption (const std::string &superkey, const std::string &tex_supercaption) |
void | set_tex_format (const std::string &key, const std::string &format="c") |
void | write_text (std::ostream &out) const |
void | write_tex (std::ostream &file, const bool with_header=true) const |
Protected Member Functions | |
void | get_selected_columns (std::vector< std::string > &sel_columns) const |
unsigned int | n_rows () const |
Protected Attributes | |
std::vector< std::string > | column_order |
std::map< std::string, Column > | columns |
std::map< std::string, std::vector< std::string > > | supercolumns |
std::map< std::string, std::string > | tex_supercaptions |
std::string | tex_table_caption |
std::string | tex_table_label |
The TableHandler stores TableEntries of arbitrary value type and writes the table as text or in tex format to an output stream. The value type actually may vary from column to column and from row to row.
The most important function is the templatized function add_value(const std::string &key, const value_type value), that adds a column with the name key
to the table if this column does not yet exist and adds the value of value_type
(e.g. unsigned int
, double
, std::string
, ...) to this column. After the table is complete there are different possibilities of output, e.g. into a tex file with write_tex() or as text with write_text().
Two (or more) columns may be merged into a "supercolumn" by twice (or multiple) calling add_column_to_supercolumn(), see there. Additionally there is a function to set for each column the precision of the output of numbers, and there are several functions to prescribe the format and the captions the columns are written with in tex mode.
A detailed explanation of this class is also given in the step-13 tutorial program.
This is a simple example demonstrating the usage of this class. The first column includes the numbers i=1..n
, the second ...
, the third
, where the second and third columns are merged into one supercolumn with the superkey
squares and roots
. Additionally the first column is aligned to the right (the default was centered
) and the precision of the square roots are set to be 6 (instead of 4 as default).
TableHandler table(); for (unsigned int i=1; i<=n; ++i) { table.add_value("numbers", i); table.add_value("squares", i*i); table.add_value("square roots", sqrt(i)); } // merge the second and third column table.add_column_to_supercolumn("squares", "squares and roots"); table.add_column_to_supercolumn("square roots", "squares and roots"); // additional settings table.set_tex_format("numbers", "r"); table.set_precision("square roots", 6); // output std::ofstream out_file("number_table.tex"); table.write_tex(out_file); out_file.close();
TableHandler::TableHandler | ( | ) |
Constructor.
void TableHandler::add_value | ( | const std::string & | key, | |
const value_type | value | |||
) | [inline] |
Adds a column (if not yet existent) with the key key
and adds the value of value_type
to the column.
void TableHandler::add_column_to_supercolumn | ( | const std::string & | key, | |
const std::string & | superkey | |||
) |
Creates a supercolumn (if not yet existent) and includes column to it. The keys of the column and the supercolumn are key
and superkey
, respectively. To merge two columns c1
and c2
to a supercolumn sc
hence call add_column_to_supercolumn(c1,sc)
and add_column_to_supercolumn(c2,sc)
.
Concerning the order of the columns, the supercolumn replaces the first column that is added to the supercolumn. Within the supercolumn the order of output follows the order the columns are added to the supercolumn.
void TableHandler::set_column_order | ( | const std::vector< std::string > & | new_order | ) |
Change the order of columns and supercolumns in the table.
new_order
includes the keys and superkeys of the columns and supercolumns in the order the user like to. If a superkey is included the keys of the subcolumns need not to be additionally mentioned in this vector. The order of subcolumns within a supercolumn is not changeable and keeps the order in which the columns are added to the supercolumn.
This function may also be used to break big tables with too many columns into smaller ones. Call this function with the first e.g. five columns and then write_*
. Afterwards call this function with the next e.g. five columns and again write_*
, and so on.
Sets the precision
e.g. double or float variables are written with. precision
is the same as in calling out<<setprecision(precision)
.
void TableHandler::set_scientific | ( | const std::string & | key, | |
const bool | scientific | |||
) |
Sets the scientific_flag
. True means scientific, false means fixed point notation.
void TableHandler::set_tex_caption | ( | const std::string & | key, | |
const std::string & | tex_caption | |||
) |
Sets the caption of the column key
for tex output. You may want to chose this different from key
, if it contains formulas or similar constructs.
void TableHandler::set_tex_table_caption | ( | const std::string & | table_caption | ) |
Sets the tex caption of the entire table
for tex output.
void TableHandler::set_tex_table_label | ( | const std::string & | table_label | ) |
Sets the label of this table
for tex output.
void TableHandler::set_tex_supercaption | ( | const std::string & | superkey, | |
const std::string & | tex_supercaption | |||
) |
Sets the caption the the supercolumn superkey
for tex output. You may want to chose this different from superkey
, if it contains formulas or similar constructs.
void TableHandler::set_tex_format | ( | const std::string & | key, | |
const std::string & | format = "c" | |||
) |
Sets the tex output format of a column, e.g. c
, r
, l
, or p{3cm
}. The default is c
. Also if this function is not called for a column, the default is preset to be c
.
void TableHandler::write_text | ( | std::ostream & | out | ) | const |
Write table as formatted text, e.g. to the standard output.
void TableHandler::write_tex | ( | std::ostream & | file, | |
const bool | with_header = true | |||
) | const |
Write table as a tex file. If with_header is set to false (it is true by default), then no "\documentclass{...}", "\begin{document}" and "\end{document}" are used. In this way the file can be included into an existing tex file using a command like "\input{table_file}".
void TableHandler::get_selected_columns | ( | std::vector< std::string > & | sel_columns | ) | const [protected] |
Help function that gives a vector of the keys of all columns that are mentioned in column_order
, where each supercolumn key is replaced by its subcolumn keys.
This function implicitly checks the consistency of the data. The result is returned in sel_columns
.
Builtin function, that gives the number of rows in the table and that checks if the number of rows is equal in every column. This function is e.g. called before writing output.
std::vector<std::string> TableHandler::column_order [protected] |
Stores the column and supercolumn keys in the order desired by the user. By default this is the order of adding the columns. This order may be changed by set_column_order(...)
.
std::map<std::string,Column> TableHandler::columns [protected] |
Maps the column keys to the columns (not supercolumns).
std::map<std::string, std::vector<std::string> > TableHandler::supercolumns [protected] |
Maps each supercolumn key to the the keys of its subcolumns in the right order. It is allowed that a supercolumn has got the same key as a column.
Note that we do not use a multimap
here since the order of column keys for each supercolumn key is relevant.
std::map<std::string, std::string> TableHandler::tex_supercaptions [protected] |
Maps the supercolumn keys to the captions of the supercolumns that are used in tex output.
By default these are just the supercolumn keys but they may be changed by set_tex_supercaptions(...)
.
std::string TableHandler::tex_table_caption [protected] |
The caption of the table itself.
std::string TableHandler::tex_table_label [protected] |
The label of the table.