Package: pydict2latex
PyDict To LaTeXMake python dictionaries accessible in LaTeX. Useful to keep a LaTeX document in-sync with data but without having to edit the document every time the data changes.This is similar to Cameron Devine'sjson2latex library. The main difference is that the macro to access data isfully expandable. This means the values retrieved from the dictionary can be used everywhere, including arguments to other macros, in conditions (e.g., to have paragraphs only showing depending on a value), and to draw tikz data visualizations.How to use?Python:frompydict2lateximportPyDictToLatexdictionary={"foo":"bar","sub dict":{"a":42,}}PyDictToLatex("mydata",dictionary).save("mydata.tex")LaTeX:\input{mydata}The value of\mydata{foo}is\mydata{sub dict}{a}.Will result in a document with contentThe value of bar is 42Installationgit clone repo
cd repo
pip install .More ExamplesBecause the macro is fully expandable, it can be used pretty much anywhere.For example, to conditonally show a paragraph:\ifnum\pdfstrcmp{\mydata{sub dict}{a}}{42}=0
   The variable is equal to 42\elseThe variable is different from 42\fito loop through the data:\foreach\xin{\mydata{a list}}{\x}or to draw tikz graphics:\begin{tikzpicture}\draw[gray, thick](\mydata{x1},\mydata{y1}) -- (\mydata{x2},\mydata{y2});\end{tikzpicture}Variables To DictTwo functions can help generate dictionaries from array of values.continuous_variables_to_dictgenerates a dictionary with common descriptivie statistics from an array of numerical values:frompydict2lateximportcontinuous_variables_to_dictcontinuous_variables_to_dict([5,1,7,3,2,8,9,5])# {'values': [5, 1, 7, 3, 2, 8, 9, 5], 'min': 1, 'max': 9, 'mean': '5', 'median': '5', 'std': '2.7', 'count': 8, 'sum': 40}categorical_variables_to_dictgenerates a dictionary from an array of categorical values:categorical_variables_to_dict(["cherry","cherry","pineapple","apple","cherry","apple"])# {'unique': 3, 'count': 6, 'descending': {'1': 'cherry', '2': 'apple', '3': 'pineapple'}, 'ascending': {'3': 'cherry', '2': 'apple', '1': 'pineapple'}, 'cherry': {'count': 3, 'percent': '50'}, 'apple': {'count': 2, 'percent': '33.3'}, 'pineapple': {'count': 1, 'percent': '16.7'}}Known IssuesTeX capacity exceededThis might happen with dictionaries having large 'nodes'. Often, this can be solved by re-organizing the dictionary: 'nodes' in the dictionary tree should be kept relatively small (<15 sub-trees). This can be done by spreading the sub-trees into multiple nodes, or by having multiple dictionaries (you can \include multiple dictionaries as long as their names do not collide). No limitations for nodes containing only values (i.e., having no sub-trees), those can contain a virtually infinite number of values.
For a more technical explanation of the issue: to make the macro fully expandable, I rely on a lot of \expandafter. The issue is that, the more sub-trees in a node, the more \expandafter are needed which might exceed TeX's capacity. There are probably some optimizations that could minimize the issue, or some clever tricks to get rid of the issue. PRs are welcome.CreditsCameron Devine forjson2latexwhich inspired this projectJoseph Wright for the\replicatemacro