我正在尝试使用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或其他网站上找到的多种解决方案,但都没有成功。
发布于 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
发布于 2017-12-10 19:53:56
据我所知,你不能。您必须替换标识符中Python不喜欢的字符,然后才能尝试在Jinja模板中使用它们。
https://stackoverflow.com/questions/43495728
复制相似问题