diff --git a/include/VariadicTable.h b/include/VariadicTable.h index 031f2cf..91ec4c5 100644 --- a/include/VariadicTable.h +++ b/include/VariadicTable.h @@ -151,6 +151,124 @@ class VariadicTable _precision = precision; } + /** + * Print table date as latex code + */ + template + void print_latex(StreamType & stream, char column_type = 'c', bool do_centering = true, bool use_horizontal_separator = true, bool use_vertical_separator = true) + { + if(do_centering) + stream << "\\begin{center}\n"; + + stream << "\\begin{tabular}{"; + if(use_vertical_separator) { + stream << "|"; + for(size_t i = 0; i < _num_columns; ++i) + stream << column_type << "|"; + } else + for(size_t i = 0; i < _num_columns; ++i) + stream << column_type; + stream << "}\n"; + + if(use_horizontal_separator) + stream << "\\hline\n"; + + // Print out the headers + if(_num_columns > 0) + stream << _headers[0]; + for (unsigned int i = 1; i < _num_columns; i++) + stream << " & " << _headers[i]; + stream << "\\\\\n"; + if(use_horizontal_separator) + stream << "\\hline\n"; + + // Now print the rows of the table + for (auto & row : _data) + { + print_each_latex(row, stream); + stream << "\\\\"; + if(use_horizontal_separator) + stream << "\\hline\n"; + } + + stream << "\\end{tabular}\n"; + if(do_centering) + stream << "\\end{center}\n"; + } + /** + * This ends the recursion + */ + template + void print_each_latex(TupleType &&, + StreamType & /*stream*/, + size_t, + std::integral_constant< + size_t, + std::tuple_size::type>::value>) + { } + + /** + * This gets called on each item + */ + template ::type>::value>::type> + void print_each_latex(TupleType && t, StreamType & stream, size_t idx, std::integral_constant) + { + auto & val = std::get(t); + + // Set the precision + if (!_precision.empty()) + { + assert(_precision.size() == + std::tuple_size::type>::value); + + stream << std::setprecision(_precision[I]); + } + + // Set the format + if (!_column_format.empty()) + { + assert(_column_format.size() == + std::tuple_size::type>::value); + + if (_column_format[I] == VariadicTableColumnFormat::SCIENTIFIC) + stream << std::scientific; + + if (_column_format[I] == VariadicTableColumnFormat::FIXED) + stream << std::fixed; + + if (_column_format[I] == VariadicTableColumnFormat::PERCENT) + stream << std::fixed << std::setprecision(2); + } + + if(idx != 0) + stream << " & "; + + + stream << val; + + // Unset the format + if (!_column_format.empty()) + { + // Because "stream << std::defaultfloat;" won't compile with old GCC or Clang + stream.unsetf(std::ios_base::floatfield); + } + + // Recursive call to print the next item + print_each_latex(std::forward(t), stream, idx + 1, std::integral_constant()); + } + /** + * his is what gets called first + */ + template + void print_each_latex(TupleType && t, StreamType & stream) + { + print_each_latex(std::forward(t), stream, 0, std::integral_constant()); + } + protected: // Just some handy typedefs for the following two functions typedef decltype(&std::right) right_type; diff --git a/src/main.C b/src/main.C index 005de88..2d82f6b 100644 --- a/src/main.C +++ b/src/main.C @@ -1,4 +1,4 @@ -#include "VariadicTable.h" +#include "../include/VariadicTable.h" int main() @@ -13,6 +13,7 @@ main() vt.addRow("Robert", 140.3, 27, "Fande"); vt.print(std::cout); + vt.print_latex(std::cout); } // More Data @@ -51,5 +52,6 @@ main() vt.addRow(" Console::outputStep", 5.8e-05, 0, 5.8e-05); vt.print(std::cout); + vt.print_latex(std::cout); } }