Skip to content
Anselm Stordeur edited this page Jul 9, 2016 · 6 revisions

formattedTabular(matrix, width, options)


Takes an array as matrix and converts it to an latex Tabular.

Parameters

  • matrix: object, Data Array -> gets sliced in rows of the tabular depending on the width parameter
  • width: int, width or breadth of the matrix. Array gets cut depending on this parameter
  • options: object, optional options for formatting the tabular
    • options.caption: string, a nice caption for the tabular
    • options.hLines: Array.<boolean>, an array which elements say if a horizontal line should be printed in top of a row. To get a fully closed tabular the array has to be filled with (rows + 1) true statements.
    • options.vLines: Array.<boolean>, an array which elements say if a vertical line should be printed left of a column. To get a fully closed tabular the array has to be filled with (columns + 1) true statements.

Returns: string, Latex Tabular


Example Usage:

const dataToLatex = require('data-to-latex');
// some data
let exampleData = [
  "Name", "Registered", "Latitude", "Longitude", "Measurements",
  "Kim", "2015-04-17T12:52:08 -02:00", 53.988205, -166.008217, [20.8018, 25.4325],
  "Gordon", "2014-03-14T11:18:30 -01:00", 58.7322, -142.624466, [29.8197, 19.5144],
  "Mcclure", "2015-08-07T09:56:09 -02:00", 51.255166, 143.618947, [16.0908, 28.4438],
  "James", "2015-08-20T01:23:03 -02:00", 51.935684, 166.887844, [28.2362, 24.8768],
  "Branch", "2014-05-24T04:39:57 -02:00", 51.612881, -165.472281, [28.3879, 21.2844]
];
// create a tabular with 5 columns
let dataMatrix = dataToLatex.formattedTabular(exampleData, 5);

console.log(dataMatrix);

Output would be:

\begin{tabular}{lllll}
Name & Registered & Latitude & Longitude & Measurements \\
Kim & 2015-04-17T12:52:08 -02:00 & 53.988205 & -166.008217 & [20.8018,25.4325] \\
Gordon & 2014-03-14T11:18:30 -01:00 & 58.7322 & -142.624466 & [29.8197,19.5144] \\
Mcclure & 2015-08-07T09:56:09 -02:00 & 51.255166 & 143.618947 & [16.0908,28.4438] \\
James & 2015-08-20T01:23:03 -02:00 & 51.935684 & 166.887844 & [28.2362,24.8768] \\
Branch & 2014-05-24T04:39:57 -02:00 & 51.612881 & -165.472281 & [28.3879,21.2844]
\end{tabular}

Note that the arrays are converted into strings. In fact if there were Objects they would be stringified to. Null, undefined or NaN values would be converted into a -. Pdf result: Tabular

You can pass in many options to format the tabular:

const dataToLatex = require('data-to-latex');
// some data
let exampleData = [
  "Name", "Registered", "Latitude", "Longitude", "Measurements",
  "Kim", "2015-04-17T12:52:08 -02:00", 53.988205, -166.008217, [20.8018, 25.4325],
  "Gordon", "2014-03-14T11:18:30 -01:00", 58.7322, -142.624466, [29.8197, 19.5144],
  "Mcclure", "2015-08-07T09:56:09 -02:00", 51.255166, 143.618947, [16.0908, 28.4438],
  "James", "2015-08-20T01:23:03 -02:00", 51.935684, 166.887844, [28.2362, 24.8768],
  "Branch", "2014-05-24T04:39:57 -02:00", 51.612881, -165.472281, [28.3879, 21.2844]
];

let tabularOptions = {
  caption: 'Weatherstations',
  vLines: [true, false, false, false, false, true], // close the tabular on the sides vertically
  hLines: [true, true, false, false, false, false, true]  // close the header and the tabular bottom horizontally
};

// creates a tabular with 5 columns and options
let dataMatrix = dataToLatex.formattedTabular(exampleData, 5, tabularOptions);

// output the document as string
console.log(dataMatrix);

Output:

\begin{tabular}{|lllll|}
\multicolumn{5}{c}{Weatherstations} \\ \hline
Name & Registered & Latitude & Longitude & Measurements \\ \hline
Kim & 2015-04-17T12:52:08 -02:00 & 53.988205 & -166.008217 & [20.8018,25.4325] \\
Gordon & 2014-03-14T11:18:30 -01:00 & 58.7322 & -142.624466 & [29.8197,19.5144] \\
Mcclure & 2015-08-07T09:56:09 -02:00 & 51.255166 & 143.618947 & [16.0908,28.4438] \\
James & 2015-08-20T01:23:03 -02:00 & 51.935684 & 166.887844 & [28.2362,24.8768] \\
Branch & 2014-05-24T04:39:57 -02:00 & 51.612881 & -165.472281 & [28.3879,21.2844] \\ \hline
\end{tabular}

The Lines were set correctly and the code is still clear and readable. Pdf result: With Options set

Documentation:

Project

Clone this wiki locally