mohanakrishnavh
mohanakrishnavh

Reputation: 139

Unable to use LaTex in Jupyter Notebook

The below code fails on Jupyter Notebook.

$$ \alpha + \frac{\beta}{\gamma} = \delta $$

File "", line 1 $$ \alpha + \frac{\beta}{\gamma} = \delta $$ ^ SyntaxError: invalid syntax

Upvotes: 5

Views: 4884

Answers (1)

Rick
Rick

Reputation: 45231

You are trying to directly enter and display markdown in a regular code cell. You can't do that. You have to turn it into a markdown cell first.

Jupyter has at least two kinds of cells: code cells and markdown cells. The default for a cell is a code cell.

To change it to a markdown cell, select the cell while in meta mode (not with the active cursor in the cell) and press m. You can also turn it into a markdown cell using the menu. Enter the markdown and it will display the way you expect.

You actually can put markdown in a code cell, but the cell will only be allowed to contain markdown and no other code. Use the jupyter magic in the first line for this like so:

%%markdown

Similarly if you want just latex in a code cell:

%%latex

Technically you can mix markdown with code in a code cell, you just can't enter it directly. Put it into a IPython.display.Markdown object. Like this:

from IPython.display import Markdown, display

m  = Markdown('some $markdown$')
display(m)

Here and here and here and also here for more info.

Upvotes: 5

Related Questions