Write a LaTeX File

Updated: Jun 7, 2026

This example shows how to write records to a LaTeX file. LaTeX is a document preparation system widely used for scientific and technical publishing. DataPipeline enables users to serialize structured data into LaTeX documents containing a formatted table, automatically escaping all special LaTeX characters in both field names and field values so the generated file compiles cleanly with any standard LaTeX engine.

 

Java Code Listing

package com.northconcepts.datapipeline.examples.latex;

import java.io.File;
import java.math.BigDecimal;
import java.sql.Date;

import com.northconcepts.datapipeline.core.DataReader;
import com.northconcepts.datapipeline.core.DataWriter;
import com.northconcepts.datapipeline.core.Record;
import com.northconcepts.datapipeline.core.RecordList;
import com.northconcepts.datapipeline.job.Job;
import com.northconcepts.datapipeline.latex.LatexWriter;
import com.northconcepts.datapipeline.memory.MemoryReader;

public class WriteALatexFile {

    public static void main(String[] args) {
        RecordList records = new RecordList(

            new Record()
                .setField("Product",       "Wireless Keyboard")
                .setField("SKU",           "WK-1001")
                .setField("Quantity",      150)
                .setField("Unit Price",    new BigDecimal("49.99"))
                .setField("Discount %",    10.0)          // '%' is a LaTeX comment char -> escaped to \%
                .setField("In Stock",      true)
                .setField("Last Updated",  Date.valueOf("2026-05-15"))
                .setField("Notes",         "Best seller"),

            new Record()
                .setField("Product",       "Tom & Jerry Special Edition")    // & -> \&
                .setField("SKU",           "TJ-2002")
                .setField("Quantity",      30)
                .setField("Unit Price",    new BigDecimal("99.95"))
                .setField("Discount %",    5.0)
                .setField("In Stock",      true)
                .setField("Last Updated",  Date.valueOf("2026-04-01"))
                .setField("Notes",         "Price in $USD; see section #4_A"), // $ # _ -> \$ \# \_

            new Record()
                .setField("Product",       "Config {Pro} v2")               // { } -> \{ \}
                .setField("SKU",           "CP-3003")
                .setField("Quantity",      5)
                .setField("Unit Price",    new BigDecimal("299.00"))
                .setField("Discount %",    0.0)
                .setField("In Stock",      false)
                .setField("Last Updated",  Date.valueOf("2026-03-20"))
                .setField("Notes",         "Path: C:\\Program Files\\App"),   // \ -> \textbackslash{}

            new Record()
                .setField("Product",       "SuperCharge^2")                  // ^ -> \textasciicircum{}
                .setField("SKU",           "SC-4004")
                .setField("Quantity",      200L)          // long type
                .setField("Unit Price",    new BigDecimal("14.50"))
                .setField("Discount %",    15.5)
                .setField("In Stock",      true)
                .setField("Last Updated",  Date.valueOf("2026-06-01"))
                .setField("Notes",         "~Experimental model"),             // ~ -> \textasciitilde{}

            new Record()
                .setField("Product",       "Legacy Adapter")
                .setField("SKU",           "LA-5005")
                .setField("Quantity",      0)
                .setField("Unit Price",    new BigDecimal("4.99"))
                .setField("Discount %",    0.0)
                .setField("In Stock",      false)
                .setField("Last Updated",  (Object) null)  // null -> empty cell
                .setField("Notes",         (Object) null)  // null -> empty cell
        );

        DataReader reader = new MemoryReader(records);
        DataWriter writer = new LatexWriter(new File("example/data/output/product-catalog.tex"));

        Job.run(reader, writer);
    }

}

 

Code Walkthrough

  • A RecordList is populated with five Record objects representing a product catalog. Each record contains eight fields covering a range of Java types: String, int, long, BigDecimal, double, boolean, java.sql.Date, and null.
  • setField() creates a field using the name supplied in the first parameter and the value in the second parameter. Fields are stored in insertion order and define the column sequence in the generated LaTeX table.
  • Several field values deliberately contain LaTeX special characters to demonstrate automatic escaping:
    • & becomes \&
    • $ becomes \$
    • # becomes \#
    • _ becomes \_
    • % becomes \%
    • { / } become \{ / \}
    • \ becomes \textbackslash{}
    • ^ becomes \textasciicircum{}
    • ~ becomes \textasciitilde{}
    The field name Discount % also contains a %, which is similarly escaped when written to the document.
  • Passing (Object) null to setField() stores a null value; LatexWriter renders null fields as empty cells in the table.
  • A MemoryReader is created to read records from the in-memory RecordList.
  • A LatexWriter is created, targeting the output file example/data/output/product-catalog.tex
  • Data is transferred from MemoryReader to LatexWriter via Job.run(). Each record becomes one \hline-bordered data row.
  • The generated .tex file can be compiled to PDF with: pdflatex product-catalog.tex
Mobile Analytics