From d67d13752f6bd2f84610c2f1af5eef03623af50e Mon Sep 17 00:00:00 2001 From: Tias Date: Thu, 29 May 2025 13:29:08 +0300 Subject: [PATCH 1/2] add latex like print --- include/VariadicTable.h | 119 ++++++++++++++++++++++++++++++++++++++++ src/main.C | 4 +- 2 files changed, 122 insertions(+), 1 deletion(-) diff --git a/include/VariadicTable.h b/include/VariadicTable.h index 031f2cf..9c28ae0 100644 --- a/include/VariadicTable.h +++ b/include/VariadicTable.h @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -151,6 +152,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); } } From 7ab5cefb26ced93db4fb80a3bcf6fa3a2cc16f35 Mon Sep 17 00:00:00 2001 From: Tias Date: Thu, 29 May 2025 13:32:50 +0300 Subject: [PATCH 2/2] hotfix --- include/VariadicTable.h | 61 ++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/include/VariadicTable.h b/include/VariadicTable.h index 9c28ae0..91ec4c5 100644 --- a/include/VariadicTable.h +++ b/include/VariadicTable.h @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include @@ -158,43 +157,43 @@ class VariadicTable 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"; + 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]; + 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"; + 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 << "\\\\"; + if(use_horizontal_separator) + stream << "\\hline\n"; } - stream << "\\end{tabular}\n"; - if(do_centering) - stream << "\\end{center}\n"; + stream << "\\end{tabular}\n"; + if(do_centering) + stream << "\\end{center}\n"; } /** * This ends the recursion @@ -202,7 +201,7 @@ class VariadicTable template void print_each_latex(TupleType &&, StreamType & /*stream*/, - size_t, + size_t, std::integral_constant< size_t, std::tuple_size::type>::value>) @@ -245,9 +244,9 @@ class VariadicTable stream << std::fixed << std::setprecision(2); } - if(idx != 0) - stream << " & "; - + if(idx != 0) + stream << " & "; + stream << val;