如何在Jupyter Notebook中运行包含带有转义符的LaTeX公式的HTML脚本



我正试图在Jupyter Notebook中运行一些HTML模板,帮助我组织数学证明。

如果我在Text单元格中运行我的HTMLsript,它将显示我所期望的输出。例如,

<ul>
<li>EN: this is a $triangle ABC$</li>
<li>LT: čia yra $triangle ABC$ </li>
</ul>

显示

  • EN:这是一个△ABC
  • LT:čia yra△ABC

我有很多这样的HTML模板,所以我想在代码单元中运行它们,就像这样:

from IPython.display import display, HTML
template = 
'''<ul>
<li>EN: this is a $triangle ABC$</li>
<li>LT: čia yra $triangle ABC$ </li>
</ul>'''
display(HTML(template))

不幸的是,它删除了我的LaTeX脚本中的转义字符:

  • EN:这是一个
  • LT:čia yra

如何解决此问题?


注意,修复它的一个技巧是在template:中添加额外的字符

template = 
'''<ul>
<li>EN: this is a $\triangle ABC$</li>
<li>LT: čia yra $\triangle ABC$ </li>
</ul>'''

但是,我不喜欢修改template变量的脚本,因为我希望以与LaTeX中相同的方式键入公式,而不需要特别注意转义字符。

至少对于所提供的示例,您希望文档字符串被视为原始字符串,因此您的第一个代码块应该如下:

from IPython.display import display, HTML
template = 
r'''<ul>
<li>EN: this is a $triangle ABC$</li>
<li>LT: čia yra $triangle ABC$ </li>
</ul>'''
display(HTML(template))

不同之处在于文档字符串前面的r。查看此处或此处

"字符串和字节文字都可以选择以字母"r"或"r"作为前缀;这样的字符串称为原始字符串,并将反斜杠视为文字字符"[来源]

t不会被视为选项卡,也不需要转义。

可以实现一些逻辑,以简单的方式识别数学模式并在其中转义unicode:

def fix_mathmode(text):
text_asbytearray = bytearray(text, 'utf-8')
dollar_positions = [i for i, b in enumerate(text_asbytearray) if b==ord('$')]
math_groups = tuple(zip(dollar_positions[0::2], dollar_positions[1::2]))
for start, end in reversed(tuple(math_groups)):
formula = text_asbytearray[start:end+1]
formula_asbytearray = formula.decode().encode('unicode_escape')
text_asbytearray[start:end+1] = formula_asbytearray
return str(text_asbytearray, 'utf-8').expandtabs(4)
template = '''<ul>
t<li> EN: this is a $triangle ABC$ </li>
t<li> LT: čia yra $triangle ABC$ </li>
</ul>'''
>>> print(fix_mathmode(template))  
<ul>
<li> EN: this is a $triangle ABC$ </li>
<li> LT: čia yra $triangle ABC$ </li>
</ul>
>>> print(template.expandtabs(4))
<ul>
<li> EN: this is a $    riangle ABC$ </li>
<li> LT: čia yra $  riangle ABC$ </li>
</ul>

然而,它只适用于简单的情况($...$而不是$$...$$)。我不确定是否有一种简单的方法可以做到这一点,所以我同意@Wayne的解决方案。

最新更新