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.\hrule height0pt
works, but will mess up the baseline spacing.\begin{code}
insert\goodbreak
. (2) Between the two code blocks, insert\par\nobreak
.memoir
class (a superset ofbook
,report
andarticle
) provides various methods of putting text into a margin: the standard\marginpar
;\sidepar
likemarginpar
but marginal texts may overlap;\sidebar
, like\marginpar
but starts at the top of the page and where the text is long will continue on the next page;\sidefootnotes
where the notes are typeset in the margin instead at the bottom of the page. Perhaps one of these will help you.