Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LaTeX rendering #1684

Closed
VivaldoMendes opened this issue Jun 27, 2024 · 4 comments · Fixed by #1809
Closed

LaTeX rendering #1684

VivaldoMendes opened this issue Jun 27, 2024 · 4 comments · Fixed by #1809
Labels
bug Something isn't working

Comments

@VivaldoMendes
Copy link

Description

Hi,

I tested some basic LaTeX in marimo, and it works well. For example, $$y=2x^2 \tag{1}$$ correctly displays the equation and its tag.

However, if I try to use more sophisticated LaTeX code --- which runs in a LaTeX editor, in Pluto, and Jupyter --- marimo fails to render the LaTeX markdown code. For example,

$$
\begin{array}{lllll}
\text {IS : } & \hat{y}_{t}=\mathbb{E}_{t} \hat{y}_{t+1}-\frac{1}{\sigma}\left(i_{t}-\mathbb{E}_{t} \pi_{t+1}-r_{t}^{n}\right)+u_t\\
\text {Taylor rule : } & \ i_{t}  = \pi_t + r^n_t+\phi_{\pi} \pi_{t}+\phi_{y} \hat{y}_{t} \\
\text {AD : } & \hat{y}_{t}= \hat{c}_{t} \\
\text {AS : } & \pi_{t}  =\kappa \hat{y}_{t}+\beta \cdot \mathbb{E}_{t} \pi_{t+1} \\
\text {Real interest : } & r_{t}^{n}  =\rho_r \cdot r_{t-1}^{n}+\varepsilon_{t}^{r} \quad , \quad \varepsilon_{t}^{r} \sim {\cal{N}}(0,1) \\
\text {Demand shock : } & u_{t}  =\rho_u \cdot u_{t-1}+\varepsilon_{t}^{u} \quad , \quad \varepsilon_{t}^{u} \sim {\cal{N}}(0,1) \\
\end{array}
$$

gives the following error:

This cell wasn't run because it has errors
line 5 rf""" $$ \begin{array}{lllll} \text {IS : } & \hat{y}_{t}=\mathbb{E}_{t} \hat{y}_{t+1}-\frac{1}{\sigma}\left(i_{t}-\mathbb{E}_{t} \pi_{t+1}-r_{t}^{n}\right)+u_t\\ ^ SyntaxError: unexpected character after line continuation character

And if I try a little variation on the same LaTeX code:

$$
\begin{align}
      \hat{y}_t & =\mathbb{E}_t \hat{y}_{t+1}-\frac{1}{\sigma}\left(i_t-\mathbb{E}_t \pi_{t+1}-r_t^n\right)+u_t&& \tag{IS}\\
      \ i_t  & = \pi_t + r^n_t + \phi_\pi \pi_t+\phi_y \hat{y}_t  &&\tag{Taylor Rule}\\
      \hat{y}_t &=\hat{c}_t&&\tag{AD}\\
      \pi_t &=\kappa \hat{y}_t+\beta \cdot \mathbb{E}_t \pi_{t+1}&&\tag{AS}\\
      u_{t} &=\rho^u \cdot u_{t-1}+\varepsilon_{t}^{u} \quad , \quad \varepsilon_{t}^{u} \sim {\cal{N}}(0,1)&&\tag{Demand shock}\\
      r_{t}^{n}  &=\rho^r \cdot r_{t-1}^{n}+\varepsilon_{t}^{r} \quad , \quad \varepsilon_{t}^{r} \sim {\cal{N}}(0,1)&&\tag{Real Interest Rate}
\end{align}
$$

marimo spits out the following error:

�egin{align}       \hat{y}_t & =\mathbb{E}_t \hat{y}_{t+1}-rac{1}{\sigma}\left(i_t-\mathbb{E}_t \pi_{t+1}-r_t^n ight)+u_t&& ag{IS}\       \ i_t & = \pi_t + r^n_t + \phi_\pi \pi_t+\phi_y \hat{y}_t && ag{Taylor Rule}\       \hat{y}_t &=\hat{c}_t&& ag{AD}\       \pi_t &=\kappa \hat{y}_t+�eta \cdot \mathbb{E}_t \pi_{t+1}&& ag{AS}\       u_{t} &= ho^u \cdot u_{t-1}+�arepsilon_{t}^{u} \quad , \quad �arepsilon_{t}^{u} \sim {\cal{N}}(0,1)&& ag{Demand shock}\       r_{t}^{n} &= ho^r \cdot r_{t-1}^{n}+�arepsilon_{t}^{r} \quad , \quad �arepsilon_{t}^{r} \sim {\cal{N}}(0,1)&& ag{Real Interest Rate} \end{align}

It looks like marimo does not accept the backslash command in LaTeX. Pluto uses KaTeX as well, but no problems with LaTeX.

The second code is supposed to produce the following output:

aa

Thanks

Suggested solution

There seems to be a little bug with the LaTeX backslash command in marimo.

Alternative

No response

Additional context

No response

@akshayka
Copy link
Contributor

If you use a Python raw string r-string with mo.md as in the example at this link, it will work https://marimo.app/l/is03zm.

Unfortunately, our "view as markdown" option is automatically making the string an f-string, which breaks LaTeX. We need to fix this, thanks for bringing it to our attention. (cc @mscolnick)

marimo code:

import marimo

__generated_with = "0.6.24-dev2"
app = marimo.App()


@app.cell
def __():
    import marimo as mo

    mo.md(
        r"""
        $$
        \begin{array}{lllll}
        \text {IS : } & \hat{y}_{t}=\mathbb{E}_{t} \hat{y}_{t+1}-\frac{1}{\sigma}\left(i_{t}-\mathbb{E}_{t} \pi_{t+1}-r_{t}^{n}\right)+u_t\\
        \text {Taylor rule : } & \ i_{t}  = \pi_t + r^n_t+\phi_{\pi} \pi_{t}+\phi_{y} \hat{y}_{t} \\
        \text {AD : } & \hat{y}_{t}= \hat{c}_{t} \\
        \text {AS : } & \pi_{t}  =\kappa \hat{y}_{t}+\beta \cdot \mathbb{E}_{t} \pi_{t+1} \\
        \text {Real interest : } & r_{t}^{n}  =\rho_r \cdot r_{t-1}^{n}+\varepsilon_{t}^{r} \quad , \quad \varepsilon_{t}^{r} \sim {\cal{N}}(0,1) \\
        \text {Demand shock : } & u_{t}  =\rho_u \cdot u_{t-1}+\varepsilon_{t}^{u} \quad , \quad \varepsilon_{t}^{u} \sim {\cal{N}}(0,1) \\
        \end{array}
        $$
        """
    )
    return mo,


@app.cell
def __(mo):
    mo.md(
        r"""
        $$
        \begin{align}
          \hat{y}_t & =\mathbb{E}_t \hat{y}_{t+1}-\frac{1}{\sigma}\left(i_t-\mathbb{E}_t \pi_{t+1}-r_t^n\right)+u_t&& \tag{IS}\\
          \ i_t  & = \pi_t + r^n_t + \phi_\pi \pi_t+\phi_y \hat{y}_t  &&\tag{Taylor Rule}\\
          \hat{y}_t &=\hat{c}_t&&\tag{AD}\\
          \pi_t &=\kappa \hat{y}_t+\beta \cdot \mathbb{E}_t \pi_{t+1}&&\tag{AS}\\
          u_{t} &=\rho^u \cdot u_{t-1}+\varepsilon_{t}^{u} \quad , \quad \varepsilon_{t}^{u} \sim {\cal{N}}(0,1)&&\tag{Demand shock}\\
          r_{t}^{n}  &=\rho^r \cdot r_{t-1}^{n}+\varepsilon_{t}^{r} \quad , \quad \varepsilon_{t}^{r} \sim {\cal{N}}(0,1)&&\tag{Real Interest Rate}
        \end{align}
        $$
        """
    )
    return


@app.cell
def __(mo):
    mo.md(rf"")
    return


if __name__ == "__main__":
    app.run()

@akshayka akshayka added the bug Something isn't working label Jun 28, 2024
@akshayka
Copy link
Contributor

akshayka commented Jul 2, 2024

@VivaldoMendes this will be fixed in the next release (0.7.0), which should be available in a day or two.

@VivaldoMendes
Copy link
Author

@akshayka, excellent news.

@VivaldoMendes
Copy link
Author

I have tested it in Marimo v0.7.0, and the LaTeX is already correctly rendered. There is still a minor issue that I am unsure whether it deserves further attention. In notebooks for teaching or exams, we regularly use equations and tags. Tags are usually justified on the right-hand side of the cell. As it stands now, the tags are centered on the right-hand side, see image below (in marimo):

a

The more commonly used way to justify tags is as follows (in Pluto or in a normal LaTeX editor):

aa

The LaTeX code to produce the image above:

$$\begin{align}
	\hat{y}_t & =\mathbb{E}_t \hat{y}_{t+1}-\frac{1}{\sigma}\left(i_t-\mathbb{E}_t \pi_{t+1}-r_t^n\right)&& \tag{IS}\\
	\ i_t  & =  \pi_t + r^n_t+\phi_\pi \pi_t+\phi_y \hat{y}_t + x_t &&\tag{Taylor Rule}\\
	\hat{y}_t &=\hat{c}_t&&\tag{AD}\\
	\pi_t &=\kappa \hat{y}_t+\beta \cdot \mathbb{E}_t \pi_{t+1}&&\tag{AS}\\
	r^n_t  &=\rho_r \cdot r^n_{t-1}+\varepsilon_{t}^{r} \quad , \quad \varepsilon_{t}^{r} \sim {\cal{N}}(0,1)&&\tag{Natural real interest rate} \\
	x_{t}  &=\rho_x \cdot x_{t-1}+\varepsilon_{t}^{x} \quad , \quad \varepsilon_{t}^{z} \sim {\cal{N}}(0,1)&&\tag{Decision making shocks}
\end{align}$$

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants