I have a large data set which I need to export as a table to a PDF file. At the moment, I do this by exporting as CSV, applying some styling in Excel (display as table, autofit column width, etc.), and then exporting as PDF. I would like to be able to do it in one step directly from Mathematica.

NotebookPrint combined with the new (v10.3) function TextGrid seems to be able to present the data quite nicely, but the presentation is poor is when the table spans multiple pages. Is there any way to convince Mathematica to repeat column headings on each page, or to determine where page breaks will occur, so that the repeated headings can be inserted?

For example, here's some data:

data = Select[ExampleData[{"Dataset", "Titanic"}] // Normal, 
    NumericQ[#age] &][[;; 100]];

Extract the keys and put the data into a list of lists:

headings = Keys[First[data]];
dataAsTable = Outer[Lookup[#1, #2, ""] &, data, headings, 1];

I have tried the following two, which (unlike Export) produce a PDF document with sensible page sizes and with the table split between pages:

 TableForm[dataAsTable, TableHeadings -> {None, headings}] // 
  OutputForm, "temp.pdf"];

 TextGrid[Prepend[dataAsTable, headings], Frame -> All, 
  Background -> {None, {LightGray, {None}}}], "temp.pdf"];

But in both cases, the way the grid is split at the page break is quite ugly, and there seems to be no way to repeat the headers on each page.

As I noted in a comment, this can be done by exporting the table as HTML and printing to PDF. Here's an example:

htmlFile = File["Table.html"];
pdfFile = File["Table.pdf"];

rowTemplate = 

headerRow = 

  StringJoin @@ 
   Flatten[{"<table><thead>", headerRow, "</thead><tbody>", 
     rowTemplate /@ data[[;; 100]], "</tbody></table>"}], "Text"];

RunProcess[{"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.