1#ifndef DUNE_FEM_LATEXTABLEWRITER_HH
2#define DUNE_FEM_LATEXTABLEWRITER_HH
23 struct NoDataException {};
37 template<
class DataTuple >
41 enum Alignment { AlignLeft, AlignCenter, AlignRight };
49 virtual std::string
entry (
const DataTuple & )
const = 0;
51 virtual std::string
header ()
const = 0;
60 struct TupleDataSource
62 template<
class DataTuple >
63 struct Value {
typedef typename std::tuple_element< N, DataTuple >::type Type; };
65 template<
class DataTuple >
66 typename Value< DataTuple >::Type
get (
const DataTuple &data )
const
69 return std::get< N >( data );
78 template<
class DataSource >
79 struct ArrayDataSource
81 template<
class DataTuple >
84 typedef typename DataSource::template Value< DataTuple >::Type::value_type Type;
87 ArrayDataSource (
const int index,
const DataSource &source = DataSource() )
88 : index_( index ), source_( source )
91 template<
class DataTuple >
92 typename Value< DataTuple >::Type
get (
const DataTuple &data )
const
95 return source_.get( data )[ index_ ];
108 template<
class W
idthDataSource,
class ErrorDataSource >
111 template<
class DataTuple >
117 explicit EOCDataSource (
const WidthDataSource &widthSource = WidthDataSource(),
118 const ErrorDataSource &errorSource = ErrorDataSource() )
119 : widthSource_( widthSource ),
120 errorSource_( errorSource ),
121 hOld_(
std::numeric_limits< double >::infinity() )
124 explicit EOCDataSource (
const ErrorDataSource &errorSource )
125 : errorSource_( errorSource ),
126 hOld_(
std::numeric_limits< double >::infinity() )
129 template<
class DataTuple >
130 typename Value< DataTuple >::Type
get (
const DataTuple &data )
const
132 double h = widthSource_.get( data );
133 double e = errorSource_.get( data );
134 std::swap( h, hOld_ );
135 std::swap( e, eOld_ );
136 if( h < std::numeric_limits< double >::infinity() )
137 return std::log( eOld_ / e ) / std::log( hOld_ / h );
139 throw NoDataException();
143 WidthDataSource widthSource_;
144 ErrorDataSource errorSource_;
145 mutable double hOld_;
146 mutable double eOld_;
163 template<
class DataTuple,
class DataSource >
176 const DataSource &source = DataSource() )
178 decimals_( decimals ),
196 return BaseType::AlignRight;
204 std::string
entry (
const DataTuple &data )
const
206 return toString( source_.get( data ) );
210 std::string
header ()
const {
return header_; }
214 template<
class Number >
215 std::string
toString (
const Number &number )
const
217 std::ostringstream s;
218 s << std::fixed << std::setprecision( decimals_ );
219 s <<
"$" << number <<
"$";
240 template<
class DataTuple >
259 void writeRow (
const DataTuple &data );
265 typedef typename ColumnWriterVectorType::const_iterator ColumnWriterIteratorType;
271 std::string preamble ()
const;
273 std::string header ()
const;
284 template<
class DataTuple >
287 : columnWriters_( columnWriters ),
288 out_( filename.c_str() )
295 out_ <<
"\\begin{tabular}{" << preamble() <<
"}" << std::endl;
297 out_ << header() <<
" \\\\"<< std::endl;
302 template<
class DataTuple >
306 out_ <<
"\\end{tabular}" << std::endl;
311 template<
class DataTuple >
314 std::string separator =
"";
315 const ColumnWriterIteratorType end = columnWriters_.end();
316 for( ColumnWriterIteratorType it = columnWriters_.begin(); it != end; ++it )
324 out_ << (*it)->entry( data );
326 catch(
const NoDataException & )
328 out_ <<
"\\multicolumn{1}{|c|}{---}";
332 out_ <<
" \\\\" << std::endl;
336 template<
class DataTuple >
341 const ColumnWriterIteratorType end = columnWriters_.end();
342 for( ColumnWriterIteratorType it = columnWriters_.begin(); it != end; ++it, ++e )
347 out_ <<
"\\cline{" << (b+1) <<
"-" << e <<
"}";
352 out_ <<
"\\cline{" << (b+1) <<
"-" << e <<
"}";
357 template<
class DataTuple >
360 const ColumnWriterIteratorType end = columnWriters_.end();
361 for( ColumnWriterIteratorType it = columnWriters_.begin(); it != end; ++it )
369 template<
class DataTuple >
370 inline std::string LatexTableWriter< DataTuple >::header ()
const
372 std::string header, separator;
373 const ColumnWriterIteratorType end = columnWriters_.end();
374 for( ColumnWriterIteratorType it = columnWriters_.begin(); it != end; ++it )
379 header +=
"\\multicolumn{1}{|c|}{" + (*it)->header() +
"}";
385 template<
class DataTuple >
386 inline std::string LatexTableWriter< DataTuple >::preamble ()
const
388 const char alignment[] = {
'l',
'c',
'r' };
390 std::string preamble(
"|" );
391 const ColumnWriterIteratorType end = columnWriters_.end();
392 for( ColumnWriterIteratorType it = columnWriters_.begin(); it != end; ++it )
395 preamble += alignment[ (*it)->alignment() ];
397 preamble +=
"@{}p{0.2em}@{}";
gets the N th element of a provided tuple assuming its a number
Definition: latextablewriter.hh:166
BaseType::Alignment alignment() const
set the aligment of the entries for this column in the latex table
Definition: latextablewriter.hh:194
NumberColumnWriter(const std::string &header, const DataSource &source)
Constructor of NumberColumnWriter where decimal default to 6.
Definition: latextablewriter.hh:187
std::string header() const
return Column titles in latex row format
Definition: latextablewriter.hh:210
std::string toString(const Number &number) const
converts number to std::string
Definition: latextablewriter.hh:215
std::string entry(const DataTuple &data) const
returns N the element from data tuple
Definition: latextablewriter.hh:204
NumberColumnWriter(const std::string &header, const int decimals=6, const DataSource &source=DataSource())
Definition: latextablewriter.hh:175
Default exception class for I/O errors.
Definition: exceptions.hh:231
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13
constexpr auto get(std::integer_sequence< T, II... >, std::integral_constant< std::size_t, pos >={})
Return the entry at position pos of the given sequence.
Definition: integersequence.hh:22
Class representing column writer in general.
Definition: latextablewriter.hh:39
Alignment
The alignment for the data in this column.
Definition: latextablewriter.hh:41
virtual std::string header() const =0
virtual std::string entry(const DataTuple &) const =0
virtual ~AbstractColumnWriter()
Destructor.
Definition: latextablewriter.hh:44
virtual Alignment alignment() const
Definition: latextablewriter.hh:47
writes latex tables based on user-defined row structure
Definition: latextablewriter.hh:242
void writeRow(const DataTuple &data)
Write row to the table.
Definition: latextablewriter.hh:312
void writeSeparator()
Adds extra space between two columns in the latex table.
Definition: latextablewriter.hh:337
std::vector< const ColumnWriterType * > ColumnWriterVectorType
Abstract column vector type.
Definition: latextablewriter.hh:246
LatexTableWriter(const std::string &filename, const ColumnWriterVectorType &columnWriter)
Definition: latextablewriter.hh:286
~LatexTableWriter()
writes "\end{tabular}" to the latex file and removes column vector
Definition: latextablewriter.hh:303
AbstractColumnWriter< DataTuple > ColumnWriterType
Abstract column type.
Definition: latextablewriter.hh:244