title: How Can I Extract Variables from a LaTeX Doc into a Python Dictionary So That I Can Pull it into Django?
body: <p>I'm pretty new to Django and LaTeX so I'm hoping that someone out there has done something like this before:</p>
  
  <p>I'm trying to create a Django app that can read a LaTeX file, extract all of the variables (things of this form: "<code>\newcommand{\StartDate}{January 1, 2018}</code>") and place them as key/value pairs into a dictionary that I can work with inside Django.</p>
  
  <p>The idea is that each variable in the LaTeX file starts with a place holder value. I'll be building a dynamic form that uses the dictionary to create field/values and let's a user replace the place holder value with a real one. After a user has set all of the values, I'd like to be able to write those new values back into the LaTeX file and generate a pdf from it.</p>
  
  <p>I've tried regular expressions but have run into trouble because some of the 'variables' will contain blocks of LaTeX like lists, for example. I've also looked at TexSoup which seems to be very promising but I haven't been able to totally figure out yet. Here is a section from the preamble of an example LaTeX file like the ones I'll be dealing with:</p>
  
  <pre><code>%% Project Name
  \newcommand{\projectName}{Project Name}
  
  %% Start and End dates
  \newcommand{\startDate}{January 1, 2018}
  \newcommand{\finDate}{December 31, 2018}
  
  %% Name of User
  \newcommand{\userName}{aUser}
  
  % What tasks will be a part of this process?
  \newcommand{\tasks}{
  
  \begin{itemize}[noitemsep,topsep=0pt]
      \item Planning of \projectName{} on \startDate{}
      \item Construction of \projectName{}
      \item Configuration of \projectName{} by \userName{} on \finDate{}
  \end{itemize}
  }
  </code></pre>
  
  <p>Using TexSoup, I'm able to pull the LaTex file into an object, find all instances of a '\newcommand' into a generator object that I can iterate:</p>
  
  <pre><code>from TexSoup import TexSoup
  
  soup = TexSoup(open('slatex.tex'))
  
  newcommands = list(soup.find_all('newcommand'))
  </code></pre>
  
  <p>I know that this is pulling each '\newcommand' into its own element and maintaining the formats properly because I can easily print them out one at a time.</p>
  
  <p>I'm stuck trying to figure out how to pull the '\newcommand' from each item, get the <em>name</em> of the item into a dictionary key and the <em>value</em> into a dictionary value. I'd like to think that TexSoup exposes those with some kind of attribute or method but I can't find anything about it. If it doesn't, am I back to looking at regular expressions again?</p>