如何在python中格式化LaTeX字符串? [英] How to format a LaTeX string in python?
问题描述
我正在编写一个应用程序,其功能的一部分是生成LaTeX CV,因此我发现自己处在类似这样的字符串的情况下
I'm writing an application, part of whose functionality is to generate LaTeX CVs, so I find myself in a situation where I have strings like
\begin{document}
\title{Papers by AUTHOR}
\author{}
\date{}
\maketitle
\begin{enumerate}
%% LIST OF PAPERS
%% Please comment out anything between here and the
%% first \item
%% Please send any updates or corrections to the list to
%% XXXEMAIL???XXX
%\usepackage[pdftex, ...
我想用动态信息填充的
,例如电子邮件地址.由于LaTeX本身的格式,具有{email}语法的.format将不起作用,也不会使用具有%(email)s语法的字典.特别是,像"\ begin {document}"(LaTeX中的命令)之类的字符串应按原样保留,而不能从.format替换,而像"%%"(LaTeX中的注释)之类的字符串也应保留左,无需从填充词典中进行替换.这样做的合理方法是什么?
which I would like to populate with dynamic information, e.g. an email address. Due to the format of LaTeX itself, .format with the {email} syntax won't work, and neither will using a dictionary with the %(email)s syntax. in particular, strings like "\begin{document}" (a command in LaTeX) should be left literally as they are, without replacement from .format, and strings like "%%" (a comment in LaTeX) should also be left, without replacement from a populating dictionary. What's a reasonable way to do this?
推荐答案
为什么不起作用?
>>> output = r'\author{{email}}'.format(email='user@example.org')
>>> print output
\author{email}
edit :使用双花括号转义"只有LaTeX可以理解的文字花括号:
edit: Use double curly braces to "escape" literal curly braces that only LaTeX understands:
>>> output = r'\begin{{document}} ... \author{{{email}}}'.format(
... email='user@example.org')
>>> print output
\begin{document} ... \author{user@example.org}
这篇关于如何在python中格式化LaTeX字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!