本文整理汇总了Python中pylatex.Document.packages方法的典型用法代码示例。如果您正苦于以下问题:Python Document.packages方法的具体用法?Python Document.packages怎么用?Python Document.packages使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pylatex.Document
的用法示例。
在下文中一共展示了Document.packages方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _upload_problem
# 需要导入模块: from pylatex import Document [as 别名]
# 或者: from pylatex.Document import packages [as 别名]
def _upload_problem(self, problem):
"""Uploads a specified problem to imgur.
Returns:
A tuple (str, (int, int)), where str is the url, on imgur and
the tuples are the dimensions of the image (width, height).
Raises:
LatexParsingException : there was an issue parsing the document
"""
default_doc = []
# populate doc with the appropriate problem
for line in problem[0]:
# these first two statements are for wrapping the title around in a minipage which allows
# the problem to be generated on one page and doesn't invoke \newpage
if line == '\\begin{document}':
default_doc.append(NoEscape(line))
default_doc.append(NoEscape('\\begin{minipage}{\\textwidth}'))
elif line == '\\maketitle':
default_doc.append(NoEscape(line))
default_doc.append(NoEscape('\\end{minipage}'))
elif line == '\\end{itemize}':
for line2 in problem[1]:
default_doc.append(NoEscape(line2))
default_doc.append(NoEscape(line))
else:
default_doc.append(NoEscape(line))
doc_class_line = NoEscape(default_doc[0])
use_pkg_line = NoEscape(default_doc[1])
# skip twocolumn since it makes the problem look spread awfully
opts = filter(lambda pkg: pkg != 'twocolumn', doc_class_line[doc_class_line.find('[') + 1: doc_class_line.find(']')].split(','))
args = NoEscape(doc_class_line[doc_class_line.find('{') + 1: doc_class_line.find('}')])
doc = Document(documentclass=Command('documentclass', options=opts, arguments=args))
# load packages
doc.packages = [Package(i) for i in use_pkg_line[use_pkg_line.find('{') + 1: use_pkg_line.find('}')].split(',')]
# position right after \begin{document}
it = 4
while default_doc[it].strip() != '\end{document}':
doc.append(NoEscape(default_doc[it]))
it += 1
# fail safe for future problems which may not parse correctly
try:
doc.generate_pdf('default', compiler="pdflatex")
except:
raise LatexParsingException
# These are normal Linux commands that are used to convert the pdf
# file created by pylatex into a snippet
os.system("pdfcrop default.pdf")
os.system("pdftoppm default-crop.pdf|pnmtopng > default.png")
path = os.path.abspath('default.png')
uploaded_image = self._client.upload_image(path, title="LaTeX")
return uploaded_image.link, OnlineImage.get_local_image_info(path)
示例2: handle_request
# 需要导入模块: from pylatex import Document [as 别名]
# 或者: from pylatex.Document import packages [as 别名]
def handle_request(self, latex_expr):
"""Uploads LaTeX formated equations to imgur and returns a URL.
Args:
latex_expr: string that is to converte to LaTeX, requires that string is enclosed by $.
>>> handle_request("$\int \sqrt{1+\cos x + \sin x} dx$")
Returns:
A tuple (str, (int, int)), where str is the url, on imgur and
the tuples are the dimensions of the image (width, height).
"""
# create a bare bones latex document with only the one line specified from the user in the document.
doc = Document(documentclass='minimal')
doc.packages = [Package(NoEscape(i)) for i in self.packages.split(',')]
doc.append(NoEscape(latex_expr))
doc.generate_pdf('default', compiler_args=['-no-shell-escape', ], compiler="pdflatex", clean=True, clean_tex=True)
# These are normal Linux commands that are used to convert the pdf
# file created by pylatex into a snippet
os.system("pdfcrop default.pdf")
os.system("pdftoppm default-crop.pdf|pnmtopng > default.png")
path = os.path.abspath('default.png')
uploaded_image = self._client.upload_image(path, title="LaTeX")
return uploaded_image.link, OnlineImage.get_local_image_info(path)