I have two Latex files for a weekly document I create. template.tex contains a large list of variables that get changed each week:


Then the template.tex imports document.tex which is the actual document with each variable in the right spot.

I would like to update this with Python. Currently I have a Python script that finds the appropriate variables in a Google Docs spreadsheet and saves them as a toml file. I would like Latex to read that toml (or json or yaml it doesn't matter the format to me) and then assign the values of those variables based on it.

  • 3
    you could get latex to read any format, but why not get the python to write out the data in tex format directly? note that \newcommand{\var1}{foo} is a syntax error (you can not have a command called \var1 so your example code isn't really making your question clearer. But for example if you just output a two column text file with name value pairs it would be easy to read that in tex. – David Carlisle Feb 2 at 16:36
  • huh didn't know there couldn't be numbers in there (obviously not my actual variable names). I updated that. I could have Python do the work, just felt like it would be nicer if I could use a markup file. I sometimes have to edit them and once in a while others do too. Editing something like toml or yaml is a lot easier and less intimidating for people. Also, it would just be easier, theoretically. – Ian Pringle Feb 2 at 17:37
  • as I say a simple space or comma separated list of two columns with name value would be trivial to read in tex, if you use toml (which I had not heard of but looks like the old windows ini format it seems) or yaml you would have to get tex to parse past the syntax which is not impossibly difficult but hard to do in general, i doubt you need a full yaml or json parser for this so it would be better if you showed a representative input file. But as I say the less syntax the better, really. – David Carlisle Feb 2 at 17:41

Space separated values are simpler to parse than JSON, for example

enter image description here

From a text file vars.txt

var foo
othervar bar
zzz 42

and a tex file


\def\assignvar#1 #2\relax{\expandafter\gdef\csname #1\endcsname{#2}}
\read\varsfile to \tmp


var is \var\ and zzz is \zzz, othevar is \othervar.

  • if you wanted an = instead of a space in the variable file, just use #1=#2 instead of #1 #2 – David Carlisle Feb 2 at 17:56
  • Much thanks! I actually am able to use my rather simple toml files with this no problem. Just changed the #1 #2 to #1 = #2 and it worked without any further work! – Ian Pringle Feb 2 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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