简体   繁体   English

如何将LaTeX文档中的变量提取到Python词典中,以便可以将其提取到Django中?

[英]How Can I Extract Variables from a LaTeX Doc into a Python Dictionary So That I Can Pull it into Django?

I'm pretty new to Django and LaTeX so I'm hoping that someone out there has done something like this before: 我是Django和LaTeX的新手,所以我希望那里的人以前做过这样的事情:

I'm trying to create a Django app that can read a LaTeX file, extract all of the variables (things of this form: " \\newcommand{\\StartDate}{January 1, 2018} ") and place them as key/value pairs into a dictionary that I can work with inside Django. 我正在尝试创建一个可以读取LaTeX文件的Django应用,提取所有变量(此形式的内容:“ \\newcommand{\\StartDate}{January 1, 2018} ”)并将其作为键/值对放置成为我可以在Django内部使用的字典。

The idea is that each variable in the LaTeX file starts with a place holder value. 这个想法是LaTeX文件中的每个变量都以占位符值开头。 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. 用户设置完所有值之后,我希望能够将这些新值写回到LaTeX文件并从中生成pdf。

I've tried regular expressions but have run into trouble because some of the 'variables' will contain blocks of LaTeX like lists, for example. 我尝试过正则表达式,但遇到麻烦,因为某些“变量”将包含LaTeX块(例如列表)。 I've also looked at TexSoup which seems to be very promising but I haven't been able to totally figure out yet. 我也看过TexSoup,它看起来非常有前途,但我还不能完全弄清楚。 Here is a section from the preamble of an example LaTeX file like the ones I'll be dealing with: 这是示例LaTeX文件的序言部分,就像我将要处理的文件一样:

%% 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}
}

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: 使用TexSoup,我可以将LaTex文件拉入一个对象,在一个可以迭代的生成器对象中找到“ \\ newcommand”的所有实例:

from TexSoup import TexSoup

soup = TexSoup(open('slatex.tex'))

newcommands = list(soup.find_all('newcommand'))

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. 我知道这会将每个'\\ newcommand'放入其自己的元素中并正确维护格式,因为我可以轻松地一次将它们打印出来。

I'm stuck trying to figure out how to pull the '\\newcommand' from each item, get the name of the item into a dictionary key and the value into a dictionary value. 我一直在努力弄清楚如何从每个项目中提取“ \\ newcommand”,将项目的名称放入字典键,并将变成字典值。 I'd like to think that TexSoup exposes those with some kind of attribute or method but I can't find anything about it. 我想认为TexSoup可以为那些具有某种属性或方法的对象提供暴露,但我对此一无所获。 If it doesn't, am I back to looking at regular expressions again? 如果不是,我又回到正则表达式了吗?

Each of the \\newcommand s has two required arguments, denoted using {} . 每个\\newcommand都有两个必需的参数,用{}表示。 As a result, we can 结果,我们可以

  1. access each newcommand 's arguments, and 访问每个newcommand的参数,以及
  2. access the value of each argument 访问每个参数的值

With your definition of slatex.tex above, we can obtain 通过上slatex.tex的定义,我们可以获得

{'\\finDate': 'December 31, 2018', '\\startDate': 'January 1, 2018'}

using the following script 使用以下脚本

from pprint import pprint
from TexSoup import TexSoup

soup = TexSoup(open('slatex.tex'))
newcommands = list(soup.find_all('newcommand'))

result = {}
for newcommand in newcommands:
    key, value = newcommand.args
    result[key.value] = value.value

pprint(result)

*On a side note, TexSoup doesn't yet understand that these redefined variables will have tangible impact on the rest of the document. *附带说明,TexSoup尚不了解这些重新定义的变量将对文档的其余部分产生明显影响。 It treats them as any other command, passively. 它被动地将它们视为任何其他命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM