如何在 python 中格式化 LaTeX 字符串?
我正在编写一个应用程序,其部分功能是生成 LaTeX 简历,因此我发现自己处于这样的情况:我有一些字符串,
\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 中的注释)也应该是左边,没有从填充字典中替换。这样做的合理方法是什么?
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, ...
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. Edit: 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么这不起作用?
编辑:使用双大括号来“转义”只有 LaTeX 能理解的文字大括号:
Why won't this work?
edit: Use double curly braces to "escape" literal curly braces that only LaTeX understands:
您不得使用新的
format
语法来避免转义{
和}
。这应该有效:
您应该使用原始字符串
r'something'
以避免将\
转义为\\
。PS:你应该看看 txt2tags,这是一个将 t2t 格式文本转换为 html、latex、markdown 等的 Python 脚本。检查源代码以了解这些转换是如何完成的。
You may not use the new
format
syntax to avoid escaping the{
and}
.That should work:
You should use raw strings
r'something'
to avoid escaping\
as\\
.PS: You should take a look on txt2tags, a Python script to convert t2t formatted text into html, latex, markdown etc. Check the source code to see how these conversions are done.