I'm using \llap in a custom environment to write some text in the document left margin, aligned with a code listing (code listing is done using lstlisting). Whenever the code listing is at the bottom of the page, the llap label stays on the previous page, whereas the code listing moves to the next page. An example is shown below (dark rectangles represent the document's margins):
The problem in this example being that the Out [9]: label is below the page's bottom margin, and the associated code listing is on the next page.
I need long code listings to break on multiple pages, so I do not want to wrap both the label and the listing inside a minipage (which would work, but not allow page breaks inside the code listing).
Is there any way to make llap respect the bottom margin? Below is an MWE.
\documentclass{article}
\usepackage{listings}
\usepackage[breakable]{tcolorbox}
\usepackage{ifthen}
\usepackage{xcolor}
\usepackage{lipsum}
\usepackage{showframe}
% custom colors
\definecolor{incolor}{HTML}{868686}
\definecolor{outcolor}{HTML}{868686}
\definecolor{cellbackground}{HTML}{F7F7F7}
% prompt
\makeatletter
\newcommand{\boxspacing}{\kern\kvtcb@left@rule\kern\kvtcb@boxsep}
\makeatother
\newcounter{cellcounter}
\lstdefinestyle{style}{
language=Python,
aboveskip=-1.4\medskipamount,
belowskip=0.5em,
backgroundcolor=\color{cellbackground},
numbers=none,
}
\makeatletter
\lst@Key{showprompt}{true}{\def\showprompt{#1}}
\lst@Key{promptnumber}{\thecellcounter}{\def\promptnumber{#1}}
\makeatother
\lstnewenvironment{code}[1][]
{
\lstset{
style=style,
#1
}
\addtocounter{cellcounter}{1}
\ifthenelse{\boolean{\showprompt}}
{\footnotesize\ttfamily\llap{{\color{outcolor}Out\,[\thecellcounter]:\hspace{3pt}\boxspacing}}}
{\phantom{\footnotesize\ttfamily\llap{{\color{outcolor}Out\,[\thecellcounter]:\hspace{3pt}\boxspacing}}}}
}{
}
%----------------------------------------------------------------------------------------
% DOCUMENT
%----------------------------------------------------------------------------------------
\setlength\parindent{0pt}
\setlength\parskip{83pt}
\begin{document}
\lipsum[70]
\lipsum[70]
\lipsum[70]
\lipsum[66]
\begin{code}
df.head()
\end{code}
\begin{code}
0 False False ...
1 False False ...
3 False False ...
4 False False ...
... ... ... ...
9064 False False ...
9065 False False ...
9066 False False ...
9067 False False ...
\end{code}
\end{document}



Out [9]text but llap does not do that, Please include a small but complete example that shows the problem. – David Carlisle Apr 27 '20 at 12:18\hrule height0ptworks, but will mess up the baseline spacing. – John Kormylo Apr 27 '20 at 14:59\begin{code}insert\goodbreak. (2) Between the two code blocks, insert\par\nobreak. – barbara beeton Apr 27 '20 at 17:32\llapwith\makebox[0pt][r]and it's almost working. I can see the labels are a bit misaligned though, but the page breaking appears to be correct. – Ivan May 26 '20 at 11:05parindentandparskipto test. Re-adding the\setlength\parindent{0pt}solved the alignment issue. So, on my side at least, replacing\llapwith\makebox[0pt][r]is your solution. – Ivan May 26 '20 at 11:29memoirclass (a superset ofbook,reportandarticle) provides various methods of putting text into a margin: the standard\marginpar;\sideparlikemarginparbut marginal texts may overlap;\sidebar, like\marginparbut starts at the top of the page and where the text is long will continue on the next page;\sidefootnoteswhere the notes are typeset in the margin instead at the bottom of the page. Perhaps one of these will help you. – Peter Wilson May 26 '20 at 18:10