Majis
Majis

Reputation: 243

How do I use Latex in a Jupyter notebook inside Visual Studio code?

I have already seen some other links related to this question, which are mentioned as below:

  1. How to write LaTeX in IPython Notebook?
  2. https://towardsdatascience.com/write-markdown-latex-in-the-jupyter-notebook-10985edb91fd
  3. https://colab.research.google.com/drive/18_2yFdH8G-6NXY_7fTcshMoScgJ-SYac#scrollTo=go3imAWqE9au

However, I am still facing some problems while using the Latex code inside Jupyter notebook.

For example,

\begin{align*}
  f(x) &= x^2\\
  g(x) &= \frac{1}{x}\\
  F(x) &= \int^a_b \frac{1}{3}x^3
\end{align*}

This works fine as Markdown but

\begin{equation*}
  f(x) &= x^2\\
  g(x) &= \frac{1}{x}\\
  F(x) &= \int^a_b \frac{1}{3}x^3
\end{equation*}

does not.

While trying to create an ordered list using the following

\begin{enumerate}
  \item The labels consists of sequential numbers.
  \item The numbers starts at 1 with every call to the enumerate environment.
\end{enumerate}

However, $\textbf{This is a bold text.}$ works.

It's really confusing what I can use and whatnot. Is there any way so that I can use only Latex code (and no HTML or other code) inside Visual Studio code to format my texts and equations without worrying about which will work and which not?

Upvotes: 12

Views: 28259

Answers (2)

daniekpo
daniekpo

Reputation: 327

You can now with the latest version of VS Code. The feature is still in preview (as of writing) so you have to enable it in settings.json by adding "notebook.experimental.useMarkdownRenderer": true

Restart VS Code and try $$y=x^2$$ and press ctrl+enter to see it rendered. I think for now the equation needs to be on a separate line by itself.

Here's the announcement: https://code.visualstudio.com/updates/v1_55#_preview-features

Upvotes: 7

Heliton Martins
Heliton Martins

Reputation: 1221

The short and very pragmatic answer is: you can't.

When you type something like $ a^2 $ in a markdown cell, what Jupyter Notebook actually does is send it to a library called MathJax. Mathjax, in its turn, does not compile the TeX code you typed using a standard LaTeX compiler. It, instead, just looks for mathematics environments and try to process the TeX-like syntax into something MathJax can understand.

Diving in LaTeX-stuff

There are two modes in (La)TeX: the text mode and the math mode, which are treated separately by the TeX compiler. For example, open your TeX editor and try to compile the following code.

\documentclass{article}
\begin{document}
a^2

$ ação $
\end{document}

You'll receive that error:

! Missing $ inserted.
<inserted text> 
$
l.4     
   
I've inserted a begin-math/end-math symbol since I think
you left one out. Proceed, with fingers crossed.

Missing character: There is no ç (U+00E7) in font cmmi10!
Missing character: There is no ç (U+00E7) in font cmmi10!
Missing character: There is no ã (U+00E3) in font cmmi10!
Missing character: There is no ã (U+00E3) in font cmmi10!

It's telling you that (1) you can't use ^ in text mode; (2) you can't use unicode characters such as ç and ã in math mode.

At this point, it's important to remember that align environments are courtesy of amsmath or mathtools packages, and doesn't exists in "plain" LaTeX

Back to MathJax

Unlike a real LaTeX compiler, MathJax does not implement the entire TeX macros universe, but only those related to the math mode (as the name suggests). From documentation:

Note that the TeX input processor implements only the math-mode macros of TeX and LaTeX, not the text-mode macros. MathJax expects that you will use standard HTML tags to handle formatting the text of your page; it only handles the mathematics. So, for example, MathJax does not implement \emph or \begin{enumerate}...\end{enumerate} or other text-mode macros or environments. You must use HTML to handle such formatting tasks. If you need a LaTeX-to-HTML converter, you should consider other options.

However, there are some exceptions. The default configuration of TeX input processor allow detecting environments outside math mode, so those environment related to math (equation, align, matrix, etc) can be correctly typeset. Recall that LaTeX does not implements align out-of-the-box, while MathJax does.

And what about the buggy equation environment?

Regarding your buggy equation environment, it's more a LaTeX stuff. equation environments does not support line breaks (\\) neither & (the first one I can't explain exactly why, but certainly someone in TeX SE can), so your input is invalid.

Upvotes: 3

Related Questions