2

I have latex document in which there are various fields and there values are to be generated dynamically. So, what i am planning is to have python script which will generate values related to field and then insert it inside the latex document. The document looks as follows:

Project = ABC
Version = 1.0.0
Date = xyz

Now the values of project , version and date are to be filled by using python script. So, please help me how can i have the values inside latex document. I searched and have got generating the whole latex document from python but i want the two processes to be seperate. So, please help. I have with me the latex document code so, i realy don't want to play around with the code as its completly new to me, i just want to feed values inside the various fields using python.

Learner
  • 453
  • 13
  • 29
  • Could you provide some more detail on exactly what you expect the python code to do and what variables you are defining? Would I be correct in assuming in Python you have lists such as the following: `project = ["A", "B", "C"] version = ["1.0.0", "1.1.1"] date = ["x", "y", "z"]` and you want all possible iterations of these outputted as a .tex file? – Doctor David Anderson Dec 03 '15 at 09:41

1 Answers1

4

If I understand your intention, I would just replace the values within the LaTeX source by named variables, such as $project instead of ABC, $version instead of 1.0.0 etc. Then you can run the following Python script to substitute these named variables by their actual values. This assumes that the LaTeX source doesn't contain other occurrences of text conflicting with the variable syntax $xy. If it is not the case, other syntax can be chosen.

You didn't specify how you get the values into the python program. Here I assume you can define them statically within the python code.

The program will fail when an undefined variable name (not present in the dictionary) is found within the source file. It could also be changed to leave such text unchanged or replace it by empty string depending on your needs.

#!/usr/bin/env python

import sys
import re

variables = {
    'project': 'ABC',
    'version': '1.0.0',
    'date': 'xyz',
}

def run(args):
    if len(args) == 1:
        filename = args[0]
    else:
        sys.stderr.write("Filename must be passed as argument.\n")
        sys.exit(1)

    regex = re.compile(r"\$([a-zA-Z][a-zA-Z_]*)")
    with open(filename) as f:
        for line in f:
            sys.stdout.write(regex.sub(lambda m: variables[m.group(1)], line))

if __name__ == '__main__':
    run(sys.argv[1:])
Tomáš Cerha
  • 309
  • 1
  • 7
  • Although I didn't answer or ask the question. My solution was quite different. Would you be able to provide comment on several aspects of this code. In particular: `sys.stdout.write(regex.sub(lambda m: variables[m.group(1)], line))` and `if __name__ == '__main__': run(sys.argv[1:])` – Doctor David Anderson Dec 03 '15 at 12:00
  • @TomasCerha I did updated variables inside .tex as you suggested and output i am getting on console showing updated values but i want .tex file to get updated with these values. So, for that what can be done? and also date value is not getting updated. – Learner Dec 03 '15 at 12:26
  • This program demonstrates the possible solution. You will need to customize and deploy this solution according to your conditions. – Tomáš Cerha Dec 04 '15 at 09:13
  • The program just substitutes the variables within your input file and writes the result to standard output. For example on unix command line, you can type `substitute.py source_file.tex > target_file.tex`. Considering you saved the given code to a file `substitute.py` and made this file executable, it will create `target_file.tex` with the substituted values (standard output is redirected into this file). – Tomáš Cerha Dec 04 '15 at 09:21
  • @Learner Can you post a snipplet of your source file where `$date` substitution does not work? – Tomáš Cerha Dec 04 '15 at 09:26
  • @Michael `regex.sub` takes a function as the first argument. This function will be called for each match and it will be passed the Match object as argument. The value returned by this function is used for the substitution. The `lambda` construct allows defining such function inline. Here it returns the value obtained from the dictionary by the key which is taken from the match object as the value of the first unnamed group within the regular expression. See the [Python re module documentation](https://docs.python.org/2/library/re.html) for more details and examples. – Tomáš Cerha Dec 04 '15 at 09:43
  • @Michael For explanation of `if __name__ == "__main__":` see [this question](http://stackoverflow.com/questions/419163/what-does-if-name-main-do). – Tomáš Cerha Dec 04 '15 at 09:46
  • @Michael For explanation of `sys.argv` see [this question](http://stackoverflow.com/questions/20323688/python-sys-argv1-vs-sys-argv1). – Tomáš Cerha Dec 04 '15 at 09:49
  • @Learner Would you be so kind to mark the answer as correct if that's the case or let us know why it's not, please? – Tomáš Cerha Dec 18 '15 at 08:14