使用python脚本生成pdf-latex

时间:2011-11-10 19:58:23

标签: python latex pdflatex

我是一名大学生,在我的大学里,要提供任何类型的家庭作业,它必须有一个标准的封面(有大学标志,课程名称,教授的名字,我的名字和bla bla bla)。 / p>

所以,我有一个.tex文档,它生成我的标准封面pdfs。它类似于:

...
\begin{document}
%% College logo
\vspace{5cm}
\begin{center}
\textbf{\huge "School and Program Name" \\}
\vspace{1cm}
\textbf{\Large "Homework Title" \\}
\vspace{1cm}
\textbf{\Large "Course Name" \\}
\end{center}
\vspace{2.5cm}
\begin{flushright}
{\large "My name" }
\end{flushright}
...

所以,我想知道是否有办法制作一个Python脚本,询问我的作业标题,课程名称和其余字符串,并用它们来生成封面。之后,它应该编译.tex并生成带有给定信息的pdf。

接受任何意见,建议,摘要,图书馆。

4 个答案:

答案 0 :(得分:59)

您可以将模板tex文件定义为字符串:

content = r'''\documentclass{article}
\begin{document}
...
\textbf{\huge %(school)s \\}
\vspace{1cm}
\textbf{\Large %(title)s \\}
...
\end{document}
'''

接下来,使用argparse接受课程,标题,姓名和学校的值:

parser = argparse.ArgumentParser()
parser.add_argument('-c', '--course')
parser.add_argument('-t', '--title')
parser.add_argument('-n', '--name',) 
parser.add_argument('-s', '--school', default='My U')

将args插入content

只需要一些字符串格式化
args = parser.parse_args()
content%args.__dict__

将内容写入文件后,封面.tex,

with open('cover.tex','w') as f:
    f.write(content%args.__dict__)

您可以使用subprocess来致电pdflatex cover.tex

proc = subprocess.Popen(['pdflatex', 'cover.tex'])
proc.communicate()

您也可以在此处添加lpr命令,以便在工作流程中添加打印。

删除不需要的文件:

os.unlink('cover.tex')
os.unlink('cover.log')

然后可以像这样调用脚本:

make_cover.py -c "Hardest Class Ever" -t "Theoretical Theory" -n Me

全部放在一起,

import argparse
import os
import subprocess

content = r'''\documentclass{article}
\begin{document}
... P \& B 
\textbf{\huge %(school)s \\}
\vspace{1cm}
\textbf{\Large %(title)s \\}
...
\end{document}
'''

parser = argparse.ArgumentParser()
parser.add_argument('-c', '--course')
parser.add_argument('-t', '--title')
parser.add_argument('-n', '--name',) 
parser.add_argument('-s', '--school', default='My U')

args = parser.parse_args()

with open('cover.tex','w') as f:
    f.write(content%args.__dict__)

cmd = ['pdflatex', '-interaction', 'nonstopmode', 'cover.tex']
proc = subprocess.Popen(cmd)
proc.communicate()

retcode = proc.returncode
if not retcode == 0:
    os.unlink('cover.pdf')
    raise ValueError('Error {} executing command: {}'.format(retcode, ' '.join(cmd))) 

os.unlink('cover.tex')
os.unlink('cover.log')

答案 1 :(得分:5)

当然有像Jinja这样的模板系统,但它们可能对你所要求的东西有些过分。您也可以使用RST格式化页面并使用它来生成LaTeX,但这又可能是矫枉过正。哎呀,自动生成页面对于你必须定义的字段数量来说可能有些过分,但是从什么时候开始就过度杀戮了! :)

我用Python的字符串格式做了类似的事情。上面的LaTeX文档,通过将%(placeholder_name1)s标记放入文档中来“标记”文件。例如,您希望自己的班级名称去哪里,请使用%(course_name)s

\textbf{\Large "%(homework_title)s" \\}
\vspace{1cm}
\textbf{\Large "%(course_name)s" \\}

然后,从Python中,您可以加载该模板并将其格式化为:

template = file('template.tex', 'r').read()
page = template % {'course_name' : 'Computer Science 500', 
                   'homework_title' : 'NP-Complete'}
file('result.tex', 'w').write(page)

如果您想自动找到这些令牌,以下内容应该做得非常好:

import sys
import re
import subprocess

template = file('template.tex', 'r').read()
pattern = re.compile('%\(([^}]+)\)[bcdeEfFgGnosxX%]')
tokens = pattern.findall(template)

token_values = dict()
for token in tokens:
    sys.stdout.write('Enter value for ' + token + ': ')
    token_values[token] = sys.stdin.readline().strip()

page = template % token_values
file('result.tex', 'w').write(page)

subprocess.call('pdflatex result.tex')

代码将遍历令牌并向控制台打印提示,要求您输入每个令牌。在上面的示例中,您将收到两个提示(示例答案):

Enter value for homework_title: NP-Complete
Enter value for course_name: Computer Science 500

最后一行在结果文件上调用pdflatex并从中生成PDF。如果您想要更进一步,您还可以要求用户输出文件名或将其作为命令行选项。

答案 2 :(得分:3)

还有一个Template类(自2.4起)允许使用$that令牌而不是%(thi)s令牌。

答案 3 :(得分:3)

有一个确切的Python库:PyLaTeX。以下代码为taken directly from the documentation

from pylatex import Document, Section, Subsection, Command
from pylatex.utils import italic, NoEscape


def fill_document(doc):
    """Add a section, a subsection and some text to the document.

    :param doc: the document
    :type doc: :class:`pylatex.document.Document` instance
    """
    with doc.create(Section('A section')):
        doc.append('Some regular text and some ')
        doc.append(italic('italic text. '))

        with doc.create(Subsection('A subsection')):
            doc.append('Also some crazy characters: $&#{}')


if __name__ == '__main__':
    # Basic document
    doc = Document('basic')
    fill_document(doc)

    doc.generate_pdf(clean_tex=False)
    doc.generate_tex()

    # Document with `\maketitle` command activated
    doc = Document()

    doc.preamble.append(Command('title', 'Awesome Title'))
    doc.preamble.append(Command('author', 'Anonymous author'))
    doc.preamble.append(Command('date', NoEscape(r'\today')))
    doc.append(NoEscape(r'\maketitle'))

    fill_document(doc)

    doc.generate_pdf('basic_maketitle', clean_tex=False)

    # Add stuff to the document
    with doc.create(Section('A second section')):
        doc.append('Some text.')

    doc.generate_pdf('basic_maketitle2', clean_tex=False)
    tex = doc.dumps()  # The document as string in LaTeX syntax

它对于生成自动报告或幻灯片特别有用。

相关问题