# Minted code on new page instead of splitting in half

I was wondering if it's possible to have a minted environment to automatically display itself on a new page if it doesn't fit in the remaining space of the current page.

Currently the code displayed is splitted between two pages.

You could surround the minted block with a minipage. Compare the following two minted blocks:

\documentclass[]{article}

\usepackage{minted}

\begin{document}
\rule{\textwidth}{0.9\textheight}
\begin{minted}{python}
for i in range(5):
print("foo")
print("foo")
print("foo")
print("foo")
print("foo")
print("foo")
\end{minted}
\clearpage
\rule{\textwidth}{0.9\textheight}
\begin{minipage}[]{\linewidth}
\begin{minted}{python}
for i in range(5):
print("foo")
print("foo")
print("foo")
print("foo")
print("foo")
print("foo")
\end{minted}
\end{minipage}
\end{document}

• I have voted your answer. Very good. +1 – Sebastiano Oct 13 '17 at 8:01

samepage -> Forces the whole listing to appear on the same page, even if it doesn’t fit.

\documentclass{article}
\textheight5cm% Just to make the textblock small for the example.
\usepackage{minted}
\begin{document}
\begin{minted}[samepage]{Java}
{Wombat}
{Wombat}
{Wombat}
{Wombat}
{Wombat}
{Wombat}
{Wombat}
{Wombat}
{Wombat}
{Wombat}
{Wombat}
{Wombat}
{Wombat}
{Wombat}
{Wombat}
{Wombat}
{Wombat}
\end{minted}
\end{document}