Page Break in Latex Only If a Code Block Is Split Between Pages
I'm Writing a Report in Latex That Contains Several Code Blocks of Varying Sizes. for Example, a Code Block Might Be: \Begin{Lstlisting}[Language=Python] Dic =...
I'm writing a report in LaTeX that contains several code blocks of varying sizes. For example, a code block might be:
\begin{lstlisting}[language=Python]
dic = {
"qwe": 0.2,
"wer": 0.3,
"ert": 0.1,
"rty": 0.1,
"tyu": 0.1,
"yui": 0.2
}
\end{lstlisting}
When I add new text paragraphs, sections, etc., the code block moves around (obviously). Sometimes, the end of a page cuts the block, resulting in some of the block hanging over into a new page.
Instead of manually writing \pagebreak above \begin{lstlisting}, I'm wondering if there's some way to tell LaTeX to page break right above the code block only if not doing so would cause the block to be cut off by the end of a page.
That way, if the code block fits perfectly where it is, there won't be an unnecessary page break.
I hope this makes sense, thanks for the help.