Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
mat_latex.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
94#ifndef MAT_LATEX_H
95#define MAT_LATEX_H
96
97#include <fstream>
98#include <ostream>
99#include <sstream>
100#include <string>
101
102namespace Aleph
103{
104
140template <typename Mat,
141 class RowIndexFormatter,
142 class ColIndexFormatter,
143 class EntryFormatter>
144void mat_to_latex(Mat & mat,
145 long n,
146 long m,
147 std::ostream & out,
148 const std::string & prefix = "",
149 const std::string & suffix = "")
150{
151 out << prefix;
152 out << "\\begin{tabular}{|c|";
153
154 for (long j = 0; j < m; ++j)
155 out << "p{1mm}";
156
157 out << "|} \\hline\n& ";
158
159 // Column headers
160 for (long j = 0; j < m; ++j)
161 {
162 out << ColIndexFormatter()(mat, j);
163 if (j < m - 1)
164 out << " & ";
165 else
166 out << "\\\\ \\hline\n";
167 }
168
169 // Matrix rows
170 for (long i = 0; i < n; ++i)
171 {
172 out << RowIndexFormatter()(mat, i) << " & ";
173
174 for (long j = 0; j < m; ++j)
175 {
176 out << EntryFormatter()(mat, i, j);
177 if (j < m - 1)
178 out << " & ";
179 }
180
181 out << "\\\\\n";
182 }
183
184 out << "\\hline\\end{tabular}\n" << suffix;
185}
186
194template <typename Mat,
195 class RowIndexFormatter,
196 class ColIndexFormatter,
197 class EntryFormatter>
198void mat_to_latex(Mat & mat,
199 long n,
200 long m,
201 std::ofstream & out,
202 const std::string & prefix = "",
203 const std::string & suffix = "")
204{
205 mat_to_latex<Mat, RowIndexFormatter, ColIndexFormatter, EntryFormatter>(
206 mat, n, m, static_cast<std::ostream&>(out), prefix, suffix);
207}
208
216template <typename Mat>
218{
219 std::string operator()(Mat &, long i) const
220 {
221 return std::to_string(i);
222 }
223};
224
232template <typename Mat>
234{
235 std::string operator()(Mat &, long j) const
236 {
237 return std::to_string(j);
238 }
239};
240
249template <typename Mat>
251{
252 std::string operator()(Mat & mat, long i, long j) const
253 {
254 std::ostringstream ss;
255 ss << mat(i, j);
256 return ss.str();
257 }
258};
259
275template <typename Mat>
276void mat_to_latex_simple(Mat & mat,
277 long n,
278 long m,
279 std::ostream & out,
280 const std::string & prefix = "",
281 const std::string & suffix = "")
282{
283 mat_to_latex<Mat,
287}
288
289} // namespace Aleph
290
291// Global namespace compatibility
297
298#endif // MAT_LATEX_H
size_t size_t int32_t * out
Definition ca-c-api.h:120
Main namespace for Aleph-w library functions.
void mat_to_latex(Mat &mat, long n, long m, std::ostream &out, const std::string &prefix="", const std::string &suffix="")
Generate LaTeX tabular representation of a matrix.
Definition mat_latex.H:144
void mat_to_latex_simple(Mat &mat, long n, long m, std::ostream &out, const std::string &prefix="", const std::string &suffix="")
Generate LaTeX table with default formatters.
Definition mat_latex.H:276
static void suffix(Node *root, DynList< Node * > &acc)
static void prefix(Node *root, DynList< Node * > &acc)
Default column index formatter.
Definition mat_latex.H:234
std::string operator()(Mat &, long j) const
Definition mat_latex.H:235
Default entry formatter.
Definition mat_latex.H:251
std::string operator()(Mat &mat, long i, long j) const
Definition mat_latex.H:252
Default row index formatter.
Definition mat_latex.H:218
std::string operator()(Mat &, long i) const
Definition mat_latex.H:219
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)