jupyter / notebook

Jupyter Interactive Notebook
https://jupyter-notebook.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
11.74k stars 4.98k forks source link

Errors for math equations to print PDF from Jupyter notebook #3353

Open PeterXiaoGuo opened 6 years ago

PeterXiaoGuo commented 6 years ago

Hey everyone,

**I write a table in jupyter notebook and declare some usual statistics symbols like Latex.

$\DeclareMathOperator{\Corr}{Corr}$ $\DeclareMathOperator{\Var}{Var}$ $\DeclareMathOperator{\E}{\mathbb{E}}$ $\DeclareMathOperator{\Cov}{Cov}$**

However, when I print it to PDF, it fails. any ideas to fix it? Thank you very much!

Package hyperref Warning: Rerun to get /PageLabels entry.

(c:/texlive/2017/texmf-dist/tex/latex/psnfss/ot1ppl.fd) (c:/texlive/2017/texmf-dist/tex/latex/psnfss/omlzplm.fd) (c:/texlive/2017/texmf-dist/tex/latex/psnfss/omszplm.fd) (c:/texlive/2017/texmf-dist/tex/latex/psnfss/omxzplm.fd) (c:/texlive/2017/texmf-dist/tex/latex/psnfss/ot1zplm.fd)

LaTeX Warning: No \author given.

(c:/texlive/2017/texmf-dist/tex/generic/oberdiek/se-ascii-print.def)

Package longtable Warning: Column widths have changed (longtable) in table 1 on input line 314.

! LaTeX Error: Can be used only in preamble.

See the LaTeX manual or LaTeX Companion for explanation. Type H for immediate help. ...

l.323 Proof: (\DeclareMathOperator {\Corr}{Corr}) ? ! Emergency stop. ...

l.323 Proof: (\DeclareMathOperator {\Corr}{Corr}) No pages of output. Transcript written on notebook.log.

Faur commented 6 years ago

Just to be clear: It works in the mark-down view? right?

If so I have the same issue, but with different commands (e.g. \begin{align} \end{align})

PeterXiaoGuo commented 6 years ago

@Faur Hi, Yes, it works totally right in mark-down view! hope someone could help us.

the-rccg commented 6 years ago

Still encountering this problem. Did anyone figure out a way to fix this or a work around?

leolca commented 5 years ago

I manged to solve it. Here is what I did.

  1. Add the _removecell tag to the cell where you define a command or declare a math operator. Applying a tag it is possible to hide the cell using the tag editor built into the notebook. Suppose I have a cell used only to define the sign math operator. $\DeclareMathOperator{\sign}{sign}$ Adding this cell makes it possible to use the operator sign in the notebook, but the command will appear in LaTeX outside the preamble, causing error. Therefore we need to remove it when converting the notebook.

  2. Create a template. Edit a file temp.tplx:

((*- extends 'article.tplx' -*))

((* block author *))
\author{My Name}
((* endblock author *))

((* block title *))
\title{My Document Title}
((* endblock title *))

((* block definitions *))
((( super() )))
\DeclareMathOperator{\sign}{sign}
((* endblock definitions *))
  1. Use nbconvert and remove the tags marked with the tag _removecell.
jupyter nbconvert --to pdf --template temp notebook.ipynb --TagRemovePreprocessor.remove_cell_tags='{"remove_cell"}'