3

How do I insert a newpage/pagebreak into the pdf output of a Jupyter Notebook using the IPython.display.Latex function?

nbconvert is used:

nbconvert --to=pdf

A latex cell works fine:

%%latex
\newpage

but this doesn't do anything:

Latex(r"\newpage")
Tom Malkin
  • 1,904
  • 2
  • 17
  • 34
  • That works for me. It creates a PDF file with two pages. I don't understand what problem you had. – mforezdev Apr 26 '17 at 12:46
  • 1
    so it seems to work when the `Latex(r"\newpage")` is in it's own cell. But it doesn't seem work within a cell with other python code, for example: `Latex(r'\newpage') print("This should be page 2")` – Tom Malkin May 01 '17 at 23:58
  • 1
    @ManelFornos Which is important I should mention because I'm trying to get the newpage into a for loop – Tom Malkin May 01 '17 at 23:59
  • @Harlekuin have you ever found a workaround to this? – alelom Mar 01 '20 at 16:14

2 Answers2

1

unless it's the last line of the cell (that's why it works when it's in its own cell), it should be:

display(Latex(r"\newpage"))
Julio
  • 63
  • 4
1

Further elaborating on the answer of @Julio, what you need to incorporate some latex code into a Code cell is:

from IPython.display import display, Math, Latex

then you can procedurally add Latex syntax or Math in any code cell like:

display(Latex(r"\newpage"))

for math formulas, swap Latex with Math.

This will work for scenarios where, e.g., you want to add one line break for each iteration of a for loop.

alelom
  • 1,375
  • 2
  • 15
  • 32