Issue
I'm trying to run some HTML templates in Jupyter Notebook that helps me to organise a mathematical proof.
If I run my HTML sript in Text cell it will display an output as I expect. For instance,
<ul>
<li>EN: this is a $\triangle ABC$</li>
<li>LT: Δia yra $\triangle ABC$ </li>
</ul>
displays
- EN: this is a △ABC
- LT: Δia yra △ABC
I have many HTML templates like this so I want to run them in Code cell instead, like so:
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))
Unfortunately, it removes escape characters in my LaTeX scripts:
- EN: this is a ππππππππ΄π΅πΆ
- LT: Δia yra ππππππππ΄π΅πΆ
How to fix this issue?
Note that one trick of fixing it is to add extra \
characters in template
:
template = \
'''<ul>
<li>EN: this is a $\\triangle ABC$</li>
<li>LT: Δia yra $\\triangle ABC$ </li>
</ul>'''
However, I prefer not to modify my script of template
variable since I want to type my formulas in the same way as in LaTeX with no extra care about escape characters.
Solution
At least for the provided example, you want the docstring to be treated as raw, and so your first code block should be the following:
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))
The difference is the r
in front of the docstring. See here or here
"Both string and bytes literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and treat backslashes as literal characters." [SOURCE]
Then the \t
doesn't get treated as a tab and there's no need to escape.
Answered By - Wayne
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.