JSON to LaTeX

json2latex is a library for converting a nested Python structure to a format accessible in LaTeX.

Python

json2latex.dumps(name, obj)[source]

Convert a nested Python structure to a string accessible in LaTeX.

Parameters:
  • name (str) – The name of the LaTeX variable to save the data to.
  • obj (dict or list) – The Python object to make accessible in LaTeX.
Returns:

A string of LaTeX code.

Return type:

str

json2latex.dump(name, obj, fp)[source]

Convert a nested Python structure to a file accessible in LaTeX.

Parameters:
  • name (str) – The name of the LaTeX variable to save the data to.
  • obj (dict or list) – The Python object to make accessible in LaTeX.
  • fp (TextIO) – The filelike object to write LaTeX code to.

Example

The following Python code saves a file, out.tex which includes the necessary LaTeX commands to access the data in LaTeX.

import json2latex

data = dict(a="test", b=[1, 2])

with open('out.tex', 'w') as f:
  json2latex.dump('data', data, f)

The same result can be accomplished by running,

json2latex example.json data out.tex

where example.json is a JSON file containing the same data is the data dictionary in the Python example.

The code output by JSON to LaTeX can be used as follows. First the file needs to be imported in LaTeX using \input{out.tex}. Then, the following commands can be used to access the data:

  • \data will expand to the full JSON representation of the input, {"a": "test", "b": [1, 2]}.
  • \data[a] will expand to test.
  • \data[b] will expand to [1, 2].
  • \data[b][0] will expand to 1.
  • \data[b][1] will expand to 2.
  • \data[b][2], and all other undefined values, will expand to ??.