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

I have already seen some other links related to this question, which are mentioned as below:
How to write LaTeX in IPython Notebook?
https://towardsdatascience.com/write-markdown-latex-in-the-jupyter-notebook-10985edb91fd
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?

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.

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

Related

how to type in latex styled variables for julia in vs code or jupyter notebook

As in Julia REPL, we can type in latex styled variables by using tab and \epsilon to get the greek letter and give it a value. Since I mainly wirte Julia
codes in VS Code now, I was wondering if we could do that in vs code also? or can we do that in Jupyter notebook?
and in advance, can we have complicate latex styled computation formula in vs code when progrmming with the julia extension? or in Jupyter notebook
What I want to describe here is something very similar to what Mathematica can do, which makes the math equations much easier to read.
There is a VS Code Julia extension, which does support LaTeX-like tab completion, just like in the REPL. It should also just work in Jupyter, if you have the correct Julia kernel installed. You can find out more about that here.
In terms of a Mathematica style notebook workflow, Jupyter is probably the closest. Julia code still has to be regular Unicode text, so you can't expect your code to look exactly like free-form LaTeX, but you might want to look at Latexify.jl, which can convert Julia expressions and data structure into nice-looking LaTeX code. If you output LaTeX code generated by Latexify.jl in a Jupyter cell, it should even render correctly using MathJax. Otherwise, you can also just write LaTeX formulas inside Markdown cells with Jupyter.

VS code `\` underlining for insufficiently long

I am using Lean, an open source theorem prover and programming language on VS Code. Lots of commands require '\' to write math symbols, in the same way that LaTeX does.
The essential difference between Lean on VS Code and LaTeX is that in order to have the math symbol compile (e.g the difference between getting \forall and the maths 'forall' sign) is that an underline must appear whilst you're typing the characters after the \ sign.
The issue that I'm experiencing is that this underline
1) Doesn't always appear when I type \
2) Disappears way too quickly.
My question is: Is this a bug, or is there a way to change the settings in VS code such that an underline always appears after the \?
Any answers would be much appreciated.

Formatted text in the command window

I know of the cprintf Undocumented Matlab way of changing the color and other font properties in the command window but I also saw this symbols in plots. This shows that Matlab supports TeX markup in plots at least. I played with it for a while and found it very useful. So much so that I wanted to find a way to include this in the command window.
I first tried sprintf('\color{red} Something\n') and was rewarded with an error that \c is not a recognized escape sequence. Google was no help either.
This is a way to use the some of the other formatting options in the command window?
The command window doesn't support TeX. Sorry. The TeX support is part of the routines that generate the figures, not the code that displays the command window.
In essence, the command window is, by modern standards, a pretty boring terminal emulator. There's not much you can do with it.
If you're looking for something to do math in that generates fancy notebooks on the fly that combine the commands you type and their results in a nice-to-read, modern way, you might just want to avoid Matlab and have a look at Jupyter (formerly IPython) Notebooks, which of course support MathJax (and thus, LaTeX math syntax): https://try.jupyter.org/

Exporting inline code to html in org-mode

What I want in org-mode is like the inline code block syntax in Markdown language X <- 3. I googled and attempted (reference):
src_R{X <- 3}
inline in org-mode, but exporting to html (with C-cC-eh) did not show the inline code. Other code block like #+begin_src R stuff works fine, but anyhow there is a warning message while exporting:
htmlize.el 1.34 or later is needed for source code formatting [14 times]
Could that be the reason ?
Update:
Thanks for #LeVieuxGildas opinion, I installed and loaded the latest htmlize.el version 1.43 now and did not get any error message when exporting. But still exporting html can not show inline code part.
system: OS X 10.8.2 ; emacs mac port https://github.com/railwaycat/emacs-mac-port ; built-in org-mode
I think we are talking about two things here. If you want to just "show code" like the backticks in Markdown, surround your inline code with equal signs, as in:
Write messages in JavaScript using: =console.log(x, y)=
However, using the src_XYZ{abc} attempts to evaluate that as code and return the results.
Fast and dirty solution is to modify the header arguments in your previous inline code.
src_R[:exports code]{X <- 3}
In my opinion, inline code is much better than =code= or ~code~ because it can be syntax highlighted if you output your org file as Latex file.

Getting Emacs to recognise custom math delimiters so it can colour the face accordingly

I have found a code to make typing equations in LaTeX in a simpler and faster way. Instead of typing
\begin{equation}
\end{equation}
I can now type
\be
\ee
It works great and I am happy with it. The code, found in this document (p. 13), is
\makeatletter
\newcommand{\be}{\begingroup
\setlength{\arraycolsep}{2pt}
\eqnarray%
\#ifstar{\nonumber}{}%
}
\newcommand{\ee}{\endeqnarray\endgroup}
\makeatother
My problem is that I use emacs, and it doesn't recognise those math delimiters and hence it doesn't give the face the proper math colour. I have tried to customise this by using the command M-x customize-apropos to try and find something I could use, but the closest I got were the variables
'font-latex-math-environments',
which I don't think is what I am
after since from what I can see I
can only type the name of the
command which goes inside the curly
brackets in \begin{} and \end{}
'font-latex-make-built-in-keywords',
which would require more knowledge
from me than I have, and I don't
even know whether it would work.
What I would like to know, thus, is how to set up Emacs so that whenever I used the pair \be and \ee it would give the face the same colour as I set up for math. I imagine this would require an emacs lisp code, which would be great so I could modify it to include other things if so I wished.
Can anyone help me getting this done?
This is not really an answer to your question, but if it's just about saving keystrokes for writing an equation environment, you might want to try the following.
From the tags under your question, I assume you're using AUCTeX mode. With that, instead of typing
\begin{equation}
\end{equation}
every time, use the following shortcut to have AUCTeX insert an environment interactively: C-c C-e. This will prompt your for the name of the environment ("equation") which you can type using tab-completion.
So you do save a couple of keystrokes, the result is syntactically correct, you don't need the additional \be and \ee commands, and what's best is that this approach is generic, i.e., it works for all LaTeX environments, not just equation.