# Pretty print output in Jupyter notebook

In order to provide meaningful results to my students in a Jupyter notebook, I would like to pretty print some output, including text, LaTeX and things computed by Sage. I have found that I could combine either show (or pretty_print) and html, or show and LatexExpr. For example, either

limit = r"\displaystyle\lim_{x\to0}\frac{\sin x}{x}="
value = lim(sin(x)/x, x=0)
show(html("Solucion: "+"${}={}$".format(limit,value)))


limit = r"\displaystyle\lim_{x\to0}\frac{\sin x}{x}="
value = lim(sin(x)/x, x=0)
show("Solucion: ", LatexExpr(limit), value)


I would ask the following questions:

1. Is there any reason to prefer one approach to the other?
2. If I replace "Solucion" by "Solución" (please note the accented ó), which is the correct way to write this Spanish word, in both approaches I get meaningless letters instead of the expected output. If I use u"Solución", then Sage raises the error 'ascii' codec can't encode character u'\xf3' in position 6: ordinal not in range(128). Is there a way to use non-English characters?

I am using SageMath 8.5 on a MacBook Pro with macOS High Sierra.

edit retag close merge delete

Sort by » oldest newest most voted

"Strange characters" : this is a well-known Python snag...

In Python 2, current base, there is a distinction between strings (which are encoded in whatever your source fine is encoded into) and UTF_8 strings. See the Python documentation for discussion.

Check that you use utf8 strings, e. g. u"Solución".

Sage should use UTF-8 in any case, but maybe you are working on a file inadvertently encoded in, say, Latin1 ?

In the (forthcoming) Python3-based Sage, these distinctions should disappear...

An alternative in the Jupyter notebook : you can use display_typeset to get all your output processed by Mathjax.

more

Many thanks for your answer. I was aware of %display latex . It seems that typeset is a synonym of latex. Unfortunately, since MathJax only typesets maths, it does not help if the output also contains text. Anyway, while digging in HTML for a different question,I have found a simple workaround: to use the HTML code of the conflicting characters. This works:

limit = r"\displaystyle\lim_{x\to0}\frac{\sin x}{x}="
value = lim(sin(x)/x, x=0)
show(html("Soluci&oacute;n: "+"${}={}$".format(limit,value)))

( 2019-02-18 02:35:54 +0100 )edit

A function generating the HTML representation of an UTF-8 string probably exists in the zillions of available Python libraries. You could try to pip install one of them and import it in Sage, no ?

( 2019-02-18 08:21:50 +0100 )edit

No need necessarily for a non-standard library. On Python 2 you can do:

>>> u'Solución'.encode('ascii', 'xmlcharrefreplace')
'Soluci&#243;n'


Of course in this case you just get a hex escape instead of the more readable entity name, which is not ideal. I think there are ways to get entity name conversions as well. Regardless this shouldn't usually even be necessary, but as you wrote unicode will be handled better in Python 3 across the board.

( 2019-02-18 12:04:30 +0100 )edit

Many thanks for your suggestion. Yes, I could do so in my own computer. But the notebook should run in university or students' computers, which I cannot configure. However, I could include such a function in the notebook. As a proof of concept, I could define

def html_fy(string):
html_code={"á":"&aacute;", "é":"&eacute;", "í":"&iacute;",
"ó":"&oacute;", "ú": "&uacute;"}
for letter in html_code.keys():
if letter in string:
string=string.replace(letter,html_code[letter])
return string

( 2019-02-18 12:42:45 +0100 )edit

And then write

text = html_fy("Solución válida: ")
limit = r"\displaystyle\lim_{x\to0}\frac{\sin x}{x}"
value = lim(sin(x)/x, x=0)
show(html(text+"${}={}$".format(limit,value)))


which provides exactly the kind of output I was seeking for.

( 2019-02-18 12:43:15 +0100 )edit