Opened 7 months ago

Closed 3 months ago

# Introduce align_latex option that allows latex displays aligned left or centered

Reported by: Owned by: klee major sage-9.6 notebook egourgoulhon Kwankyu Lee Eric Gourgoulhon N/A 3427da0 3427da09ec598d8b4284a250c2d077d3bef941cf #33475

This

%display latex
dm = get_display_manager()
dm.preferences.align_latex = 'left'  # or 'center' or None
(1+x)^10/(1+x+x^2)


displays the expression flush left, which is the new default.

Motivated by ticket:32859#comment:32.

### comment:1 Changed 7 months ago by klee

• Dependencies set to u/klee/32942

### comment:2 Changed 7 months ago by klee

• Branch set to u/klee/32942
• Dependencies u/klee/32942 deleted

New commits:

 ​6ef43a0 Introduce display_latex preference

### comment:3 Changed 7 months ago by klee

• Authors set to Kwankyu Lee
• Description modified (diff)
• Status changed from new to needs_review

### comment:4 Changed 7 months ago by klee

• Description modified (diff)

### comment:5 Changed 7 months ago by git

• Commit changed from 6ef43a0eba5a86a8ad33932bb0cfe89924b3f525 to 5ebf3e552eaa9b095a0a66e34f5915c3baf91136

Branch pushed to git repo; I updated commit sha1. New commits:

 ​5ebf3e5 Fix doctest failures

### comment:6 Changed 7 months ago by klee

• Description modified (diff)

### comment:8 Changed 7 months ago by klee

• Description modified (diff)

### comment:9 Changed 7 months ago by klee

• Description modified (diff)

### comment:10 Changed 7 months ago by git

• Commit changed from 5ebf3e552eaa9b095a0a66e34f5915c3baf91136 to 4fbbeb76b4d2830e02326caa2c31917b2eb89245

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

 ​a968e39 Introduce display_latex preference ​4fbbeb7 Fix doctest failures

### comment:11 Changed 6 months ago by mkoeppe

• Milestone changed from sage-9.5 to sage-9.6

### comment:12 follow-up: ↓ 13 Changed 4 months ago by egourgoulhon

Sorry for the delay in reviewing!

• the option name display_latex sounds a little too vague and also may - introduce some confusion with the magic %display latex. Wouldn't align_latex be a better name?
• I've tested the new feature with Jupyterlab 3.3 (cf. #32069), it works well!
• Regarding Jupyter (classic), LaTeX outputs are always left-aligned for me (on two distinct Ubuntu 20.04 computers), as discussed at ticket:32859#comment:16 and setting dm.preferences.display_latex = 'center' does not change this...
• Shouldn't 'left' be the default, instead of 'center'? Left-align is the default for SymPy MathJax display in a pure Python 3 notebook and I see no reason to deviate from SymPy policy here. It also better (more readable) for short outputs, like 0.

### comment:13 in reply to: ↑ 12 ; follow-up: ↓ 14 Changed 4 months ago by klee

• the option name display_latex sounds a little too vague and also may - introduce some confusion with the magic %display latex. Wouldn't align_latex be a better name?

I agree.

• Regarding Jupyter (classic), LaTeX outputs are always left-aligned for me (on two distinct Ubuntu 20.04 computers), as discussed at ticket:32859#comment:16 and setting dm.preferences.display_latex = 'center' does not change this...

"left" alignment is forced in the classic notebook. It seems developers decided so. Hence align_latex is only for jupyterlab.

• Shouldn't 'left' be the default, instead of 'center'? Left-align is the default for SymPy MathJax display in a pure Python 3 notebook and I see no reason to deviate from SymPy policy here. It also better (more readable) for short outputs, like 0.

I personally agree. But since "center" alignment has been de facto default in jupyterlab, and "left" alignment is expensive in that the additional code \displaystyle" is inserted in every output cell, I am not sure if changing the default is a right thing... If you strongly insist, then I would follow. Do you?

### comment:14 in reply to: ↑ 13 ; follow-up: ↓ 17 Changed 4 months ago by egourgoulhon

• Shouldn't 'left' be the default, instead of 'center'? Left-align is the default for SymPy MathJax display in a pure Python 3 notebook and I see no reason to deviate from SymPy policy here. It also better (more readable) for short outputs, like 0.

I personally agree. But since "center" alignment has been de facto default in jupyterlab,

What do you mean by "de facto default"? I have the impression that, on the contrary, Jupyterlab's default is left-aligned, see e.g. the discussion LaTeX display math in output cells should be left-aligned on Jupyterlab github. For instance, in Jupyterlab with Python 3 kernel, the following cells all result in a left-aligned output:

import sympy
sympy.sqrt(2)

from IPython.display import Latex
Latex(r"$\sqrt{2+x}$")

Latex(r"$$\sqrt{2+x}$$")

Latex(r"$\sqrt{2+x}$")

Latex(r"$$\sqrt{2+x}$$")


and "left" alignment is expensive in that the additional code \displaystyle" is inserted in every output cell

Yes this is (one more!) drawback of our "%display latex" magic; instead of relying on Jupyter's native latex display...

I am not sure if changing the default is a right thing... If you strongly insist, then I would follow. Do you?

Despite the \displaystyle burden, I would vote in favor on the left-aligned default, for compatibility both with the display in Sage classic Jupyter and with the default for SymPy and Latex in Jupyterlab. It seems also that users prefer left-aligned, cf. https://ask.sagemath.org/question/59775/. Shall we organize a vote on sage-devel?

### comment:15 Changed 4 months ago by git

• Commit changed from 4fbbeb76b4d2830e02326caa2c31917b2eb89245 to 45e530a346e6b16d7dbf81c22b569ff46686a1d1

Branch pushed to git repo; I updated commit sha1. New commits:

 ​de883c1 Merge branch 'develop' ​45e530a Rename to align_latex

### comment:16 Changed 4 months ago by klee

• Description modified (diff)

### comment:17 in reply to: ↑ 14 ; follow-up: ↓ 19 Changed 4 months ago by klee

• Shouldn't 'left' be the default, instead of 'center'? Left-align is the default for SymPy MathJax display in a pure Python 3 notebook and I see no reason to deviate from SymPy policy here. It also better (more readable) for short outputs, like 0.

I personally agree. But since "center" alignment has been de facto default in jupyterlab,

What do you mean by "de facto default"?

I meant center alignment is status quo, and is what users got used to in jupyterlab (including myself).

I have the impression that, on the contrary, Jupyterlab's default is left-aligned, see e.g. the discussion LaTeX display math in output cells should be left-aligned on Jupyterlab github. For instance, in Jupyterlab with Python 3 kernel, the following cells all result in a left-aligned output:

import sympy
sympy.sqrt(2)

from IPython.display import Latex
Latex(r"$\sqrt{2+x}$")

Latex(r"$$\sqrt{2+x}$$")

Latex(r"$\sqrt{2+x}$")

Latex(r"$$\sqrt{2+x}$$")


and "left" alignment is expensive in that the additional code \displaystyle" is inserted in every output cell

Yes this is (one more!) drawback of our "%display latex" magic; instead of relying on Jupyter's native latex display...

I am not sure if changing the default is a right thing... If you strongly insist, then I would follow. Do you?

Despite the \displaystyle burden, I would vote in favor on the left-aligned default, for compatibility both with the display in Sage classic Jupyter and with the default for SymPy and Latex in Jupyterlab. It seems also that users prefer left-aligned, cf. https://ask.sagemath.org/question/59775/. Shall we organize a vote on sage-devel?

I agree. The current default center alignment may be regarded as a defect. No vote in fixing a defect. But perhaps we need to advertise (in release tour?) how to change the behavior for those users that do not like the new default.

### comment:18 Changed 4 months ago by egourgoulhon

• Summary changed from Introduce display_latex option that allows latex displays aligned left to Introduce align_latex option that allows latex displays aligned left or centered

### comment:19 in reply to: ↑ 17 ; follow-up: ↓ 21 Changed 4 months ago by egourgoulhon

I agree. The current default center alignment may be regarded as a defect. No vote in fixing a defect. But perhaps we need to advertise (in release tour?) how to change the behavior for those users that do not like the new default.

OK. Thanks for the changes.

There are 2 doctests failures in src/sage/misc/html.py.

### comment:20 Changed 4 months ago by klee

• Dependencies set to #33475

### comment:21 in reply to: ↑ 19 Changed 4 months ago by klee

There are 2 doctests failures in src/sage/misc/html.py.

Fixed. Thanks

I preemptively solved merge conflicts with already positively reviewed #33475, by adding it as dependency.

### comment:22 Changed 4 months ago by git

• Commit changed from 45e530a346e6b16d7dbf81c22b569ff46686a1d1 to 74e26967a878d845b97ece4183f731decfd81e60

Branch pushed to git repo; I updated commit sha1. New commits:

 ​ecac525 Fix two doctest failures ​f3d3fda deTeX documentation title ​528ef65 Use new commands \SL and \PSL ​9187bf1 Fix a glitch ​eff36ec Fix another glitch ​fe4811c Fix doctest failures ​7285ad3 Fix more doctest failures ​11ed990 Speed up macro parsing ​7da2b5f Fix an error in removing \$ ​74e2696 Merge branch 'trac33475'

### comment:23 Changed 4 months ago by klee

• Description modified (diff)

### comment:24 Changed 4 months ago by klee

• Description modified (diff)

### comment:25 follow-up: ↓ 26 Changed 4 months ago by egourgoulhon

• Reviewers set to Eric Gourgoulhon
• Status changed from needs_review to needs_work

The following notebook cell

%display latex
x


yields

\displaystylex


The argument of \displaystyle should be wrapped into curly brackets.

Version 1, edited 4 months ago by egourgoulhon (previous) (next) (diff)

### comment:26 in reply to: ↑ 25 Changed 4 months ago by klee

The following notebook cell

%display latex
x


yields

\displaystylex


In src/sage/misc/html.py, the argument of \displaystyle should be wrapped into curly brackets.

\displaystyle does not take an argument. I put a space after \displaystyle to prevent it from being combined with the following string. I am running doctesting.

### comment:27 Changed 4 months ago by git

• Commit changed from 74e26967a878d845b97ece4183f731decfd81e60 to 3427da09ec598d8b4284a250c2d077d3bef941cf

Branch pushed to git repo; I updated commit sha1. New commits:

 ​3427da0 Put a space after \displaystyle`

### comment:28 Changed 4 months ago by klee

• Status changed from needs_work to needs_review

### comment:29 Changed 4 months ago by egourgoulhon

• Status changed from needs_review to positive_review

Looks good. I've tested the latest version on various notebooks, both with Jupyter and Jupyterlab. Thanks!

### comment:30 Changed 4 months ago by klee

Thank you!

I will announce this change on sage-devel.

### comment:31 Changed 3 months ago by vbraun

• Branch changed from u/klee/32942 to 3427da09ec598d8b4284a250c2d077d3bef941cf
• Resolution set to fixed
• Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.