首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >转义Jinja2和LaTeX的斜杠

转义Jinja2和LaTeX的斜杠
EN

Stack Overflow用户
提问于 2017-04-19 20:25:49
回答 2查看 872关注 0票数 3

我正在尝试使用LaTeX和Jinja2以及python脚本实现报表自动化。我设法用.txt文件中的一些数据填充了一个简单的表。

我想在我的数据文件中使用包含字符"/“的变量名。但是,python会解释/。我试图使用从几个网站中提取的过滤器,但我无法摆脱它。

我的密钥如下:<RF/Freq>。我要求Jinja找出'<‘和'>’之间的术语,我在LaTeX模板中也有相同的内容。

这就是我现在的过滤器:

LATEX_SUBS = (
    (re.compile(r'\\'), r'\\textbackslash'),
    (re.compile(r'([{}_#%&$])'), r'\\\1'),
    (re.compile(r'~'), r'\~{}'),
    (re.compile(r'\^'), r'\^{}'),
    (re.compile(r'"'), r"''"),
    (re.compile(r'\.\.\.+'), r'\\ldots'),
    (re.compile(r'/'), r'\/')
)

def escape_tex(value):
    newval = value
    for pattern, replacement in LATEX_SUBS:
        newval = pattern.sub(replacement, newval)
    return newval

但金佳回归了:jinja2.exceptions.UndefinedError: 'RF' is undefined

作为参考,我的latex模板包含:

\documentclass[12pt,a4paper]{article} 
\begin{document}
\begin{tabular}{c|c}
Test & Result \\
\hline

Frequency & <RF\Freq | escape_tex>

\end{tabular}
\end{document}

我尝试了在SE或其他网站上找到的多种解决方案,但都没有成功。

EN

回答 2

Stack Overflow用户

发布于 2019-04-17 03:46:28

jinja_env = jinja2.Environment(
    block_start_string = '\BLOCK{',
    block_end_string = '}',
    variable_start_string = '\VAR{',
    variable_end_string = '}',
    comment_start_string = '\#{',
    comment_end_string = '}',
    line_statement_prefix = '%%',
    line_comment_prefix = '%#',
    trim_blocks = True,
    autoescape = False,
    loader = jinja2.FileSystemLoader(TeX_template),
)

# TeX_template is path to template

jinja_env.filters['escape_tex'] = escape_tex
票数 1
EN

Stack Overflow用户

发布于 2017-12-10 19:53:56

据我所知,你不能。您必须替换标识符中Python不喜欢的字符,然后才能尝试在Jinja模板中使用它们。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43495728

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档