Jupyter nbconvert fails to convert this line of LaTex - jupyter-notebook

I'm attempting to convert a Jupyter Notebook to a pdf, but it throws this error at me:
nbconvert.exporters.pdf.LatexFailed: PDF creating failed, captured latex output:
Failed to run "xelatex notebook.tex -quiet" command:
notebook.tex:411: Missing $ inserted
notebook.tex:411: Extra }, or forgotten
notebook.tex:412: Missing $ inserted
It works if I take this LaTex out of my notebook:
$ y=\frac{1}{2}x+3 $
I'm not familiar with LaTex that much, but it displays correctly in the .ipynb file, so I'm assuming there is nothing wrong with this syntax. Is this an issue with nbconvert? Thank you!

After playing around a bit, I have solved the problem. It is an issue with LaTeX syntax. I wish jupyter notebooks make it to where the cell would show an error, but it doesn't. The correct syntax is the same as above except no spaces between the dollar signs. After changing that line of code to:
$y=\frac{1}{2}x+3$
nbconvert successfully made the PDF.

Related

What is the best way to write down in-line math equations in a jupyter notebook to have best LaTeX results?

When I am writing math equations in a markdown cell of a jupyter notebook, I casually put all of that in $ ... $. Today, I converted an .ipynb file to .tex, and realized most of these equations are converted into messy stuff in the output pdf. For instance, I realized it is converting all '$'s into '$'s.
This is an example of what has happened:
Jupyter Notebook markdown:
$ \nabla ^{2} f(x) = \frac{-1}{(x+1)^{2}} $
Tex Output:
\$ \nabla \^{}\{2\} f(x) = \frac{-1}{(x+1)^{2}} \$
Does anyone have any ideas why this is happening? Is there a better way to write down in-line math equations so that it is more compatible with LaTeX?
If you are eventually going to LaTeX version, the traditional advice is spelled out in minrk's comment from November 8 of 2012:
"The best solution for that right now would be to use 'raw' cells instead of markdown, and just type LaTeX as you would. Then use nbconvert to turn the ipynb to TeX (code, figures and all), and run latex to render that to PDF, etc. You don't get live-rendered TeX in the browser like you do with MathJax / Markdown, but you do still have TeX / code in one document."
The step where you are converting now is probably using nbconvert under the hood even if you aren't directly. (You may be using it directly since you tagged with 'nbconvert`.)
Alternatively, if you are going to LaTeX ultimately, you may want to use a code cell and use the LaTeX magic cell line at the start of that cell. See here and here about %%latex cell magic. (You'll note it is also mentioned among the StackOverflow thread I referenced earlier.) In regards to the %%latex cell magic, I suggest actually consulting the link that leads use in an example notebook in this post because it seems it has to be full-blown LaTeX code and not just simple equations that work easily elsewhere with just dollar signs bracketing them, i.e., MathJax.
I tried that option with your equation and when I output the notebook as LaTeX, I didn't see additional backslashes added. (I didn't however actually try rendering the LaTeX, and so I cannot address if all the cruft/boilerplate that Jupyter is adding causes any issues for downstream useability.)

R markdown not knitting because of parse raw had been removed

I've been having problems on knitting Rmarkdown, and it's been frustrating that I have no idea what to do. Every time I knit them, it always ended up with an error like this
output file: -R--Pengenalan_R.knit.md
--parse-raw/-R has been removed. Use +raw_html or +raw_tex extension.
Unknown option ---Pengenalan_R.knit.md.
Try pandoc.exe --help for more information.
Error: pandoc document conversion failed with error 6
Execution halted
I've also tried to reinstall my Rstudio but it doesn't work either. Does anyone have a clue how to solve this? Thank you in advance!

removing certain cells via tags fails due to wrong type

I want to hide certain parts of a jupyter notebook and came across tags which can achieve this. I've tagged the cells with remove_cell in my notebook an tried to run
$ jupyter nbconvert test.ipynb --TagRemovePreprocessor.remove_input_tags="{'remove_cell'}"
however I always get the following error:
traitlets.traitlets.TraitError: The 'remove_input_tags' trait of a TagRemovePreprocessor instance must be a set, but a value of type 'unicode' (i.e. u'{remove_cell}') was specified.
I've tried to change the "{'remove_cell'}" to various format, e.g. {'remove_cell'} etc with the same result. Any help would be appreciated
According to nbconvert documentation it must be done as you have specified. But there seems to be some bug in command line parsing traitlets API, used internally by jupyter nbconvert. So i tried a slightly different approach of specifying Configuration in jupyter_nbconvert_config.py file.
Steps:
jupyter nbconvert --generate-config
This will generate default ~/.jupyter/jupyter_nbconvert_config.py.
Edit the configuration file and specify your configuration, in this case
c.TagRemovePreprocessor.remove_input_tags = set(['remove_cell'])
Run jupyter nbconvert test.ipynb This will remove the tagged cells and convert it to default HTML page.
I got this to work without editing the config file by using square brackets, which match the format of the tag in the cell metadata, instead of curly braces.
So, in my cell metadata I have this:
{
"tags": ["remove_cell"]
}
and on the command line I used:
jupyter nbconvert test.ipynb --TagRemovePreprocessor.remove_input_tags="['remove_cell']"
which successfully removed any cells with this tag from the HTML output.
nbconvert did not hide the cells with tags while also executing the notebook, and this is the most useful example I found here https://github.com/jupyter/nbconvert/issues/1300:
So the underlying issue is that the preprocessors being added on your behalf are un-ordered, so by unlikely chance the --execute turning on the ExecutePreprocessor is getting applied after the TagRemovePreprocessor. To fix this you can set the preprocessors to use in an explicit order by doing:
jupyter nbconvert notebooks/demo_notebook.ipynb \
--TagRemovePreprocessor.remove_input_tags='{"remove-input"}' \
--TagRemovePreprocessor.remove_all_outputs_tags='{"remove-output"}' \
--Exporter.preprocessors='["nbconvert.preprocessors.ExecutePreprocessor","nbconvert.preprocessors.TagRemovePreprocessor"]' \
--to notebook

Jupyter Notebook save to PDF without Code

This has been an ongoing issue for me, as I would love to use Jupyter Notebook to write my research reports, but have found it very difficult to export my Jupyter Notebooks to PDF without code and without large formatting errors.
I am able to download the notebooks as PDF, but have not found a way to hide the code, or have the PDF resemble the formatting of the notebook.
No solution I've found on SO has been sufficient for my issue, so it may be possible that this is not the intended functionality of Jupyter Notebook.
Have you tried this:
jupyter nbconvert path/to/your/ipynb --to=pdf --TemplateExporter.exclude_input=True
For more on the flags, you can refer to nbconvert config options
I have successfully converted .ipynb to .html exclude the code blocks
I followed the suggestion by 0xffff above but the PDF output did not behave as intended (code blocks were still included).
However, their suggestion inspired me to try converting to html first using the following call:
jupyter nbconvert path/to/your/ipynb --to=html --TemplateExporter.exclude_input=True
This behaved as intended, and from there, it was straightforward to print the output to PDF in a browser.
Regarding the issue with the --to=pdf flag, I've opened up an issue on the Jupyter Notebook git repo: https://github.com/jupyter/notebook/issues/3804. Will report back once I get a response.
nbconvert
nbconvert is an official package, and other answers have mentioned it. However, they did not mention there are two ways to convert to PDF.
The default option uses LaTeX, and it's often a pain to install:
jupyter nbconvert notebook.ipynb --to=pdf
A newer option is to use the webpdf converter, this does not require LaTeX
jupyter nbconvert notebook.ipynb --to=webpdf
Quarto
Quarto is the newest option. The defaults are prettier than nbconvert, and it has advanced features to customize the output PDF. However, it uses pandoc (and pandoc uses LaTeX), so you must install both first.
quarto render notebook.ipynb --to pdf
Quarto is a new project, and I often run into problems (I couldn't convert the sample notebook); however, the project is in active development and I'm sure it will get better.
Online converters
The last option is to use one of the many online services. The two main caveats are 1) You might not want to upload your notebooks to some random website, and 2) It's often unclear what engine they use to convert the notebooks. I've tried many of them with mostly negative results.
I created one online converter that uses nbconvert and deletes your notebook as soon as it's converted.
If you manually create the PDF, there is a simpler solution: add the following code block & execute it
from IPython.core.display import HTML
HTML('<style>.input, .jp-InputArea {display: none !important}</style>')
Code will disappear both in the "normal" UI and in print preview / export to HTML. Open print preview, generate the PDF - it looks great now, clear the output of this one cell to bring the code back, done

How to run R Code in R Markdown file in SublimeREPL?

I would like to test code parts in my R Markdown code without leaving Sublime Text.
For instance:
Multiplying the grades with two solves the unreliability problem:
```{r}
chisq.test(2*grades)
```
In the above example, I would like to select the line that has the code "chisq.test(2*grades)", press my key combination, and have it ran in SublimeREPL as R code.
However, when I try this, I get the following error from SublimeREPL:
Cannot find REPL for 'HTML.markdown.rmarkdown'
When I change the syntax through view menu to "R" (rather than R Markdown), the code runs fine. This is a workaround though, and it is undesirable because it costs me R Markdown syntax highlighting.
I suspect the solution is simply copy-pasting a few lines of SublimeREPL package code and repurposing them for R Markdown, but I was unable to achieve any results yet. I'd appreciate any help.
From this answer :
Open the file SublimeREPL/config/R/Main.sublime-menu. Its default position depends on your system
Linux: ~/.config/sublime-text-3/Packages
Mac: ~/Library/Application Support/Sublime Text 3/Packages (untested)
Windows: %APPDATA%/Sublime Text 3/Packages (untested)
Add your scode to the option "additional_scopes":
"additional_scopes": ["HTML.markdown.rmarkdown","tex.latex.knitr"],
Save the file, close the REPL tab, restart sublime, and open a new REPL instance.

Resources