如何在 python 中格式化 LaTeX 字符串?

发布于 2024-11-20 00:21:28 字数 599 浏览 3 评论 0原文

我正在编写一个应用程序,其部分功能是生成 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

且行且努力 2024-11-27 00:21:28

为什么这不起作用?

>>> output = r'\author{{email}}'.format(email='[email protected]')
>>> print output
\author{email}

编辑:使用双大括号来“转义”只有 LaTeX 能理解的文字大括号:

>>> output = r'\begin{{document}} ... \author{{{email}}}'.format(
... email='[email protected]')
>>> print output
\begin{document} ... \author{[email protected]}

Why won't this work?

>>> output = r'\author{{email}}'.format(email='[email protected]')
>>> print output
\author{email}

edit: Use double curly braces to "escape" literal curly braces that only LaTeX understands:

>>> output = r'\begin{{document}} ... \author{{{email}}}'.format(
... email='[email protected]')
>>> print output
\begin{document} ... \author{[email protected]}
柠檬色的秋千 2024-11-27 00:21:28

您不得使用新的 format 语法来避免转义 {}

这应该有效:

>>> a = r'''
\title{%(title)s}
\author{%(author)s}
\begin{document}'''

>>> b = a % {'title': 'My Title', 'author': 'Me, Of course'}
>>> print(b)

\title{My Title}
\author{Me, Of course}
\begin{document}

您应该使用原始字符串 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:

>>> a = r'''
\title{%(title)s}
\author{%(author)s}
\begin{document}'''

>>> b = a % {'title': 'My Title', 'author': 'Me, Of course'}
>>> print(b)

\title{My Title}
\author{Me, Of course}
\begin{document}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文