Skip to content

Commit e32c22f

Browse files
committed
[macros] Avoid page break between example and codeblock introducers
if they are immediately following each other.
1 parent 6608a61 commit e32c22f

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

source/macros.tex

+28-4
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@
267267
\newcommand{\leftshift}[1]{\ensuremath{\mathbin{\mathsf{lshift}_{#1}}}}
268268

269269
%% Notes and examples
270-
\newcommand{\noteintro}[1]{[\textit{#1}:}
270+
\newcommand{\noteintro}[1]{[\textit{#1}: }
271271
\newcommand{\noteoutro}[1]{\textit{\,---\,#1}\kern.5pt]}
272272

273273
% \newnoteenvironment{ENVIRON}{BEGIN TEXT}{END TEXT}
@@ -278,7 +278,7 @@
278278
\newcommand{\newnoteenvironment}[3]{
279279
\newsubclausecounter{#1}
280280
\newenvironment{#1}
281-
{\def\noteend{#3}\par\small\stepcounter{#1}\noteintro{#2}}
281+
{\def\noteend{#3}\par\small\stepcounter{#1}\noteintro{#2}\hbox to 0pt{}\ignorespaces}
282282
{\noteoutro{\noteend}\par}
283283
}
284284

@@ -462,7 +462,12 @@
462462
% Our usual abbreviation for 'listings'. Comments are in
463463
% italics. Arbitrary TeX commands can be used if they're
464464
% surrounded by @ signs.
465-
\newcommand{\CodeBlockSetup}{%
465+
\def\codeblockbeginpenalty{-1} % default: no penalty change before codeblock
466+
\newcommand{\CodeBlockSetup}{
467+
\ifnum\codeblockbeginpenalty=-1
468+
\else
469+
\presetkeys{lst}{beginpenalty=\codeblockbeginpenalty}{}%
470+
\fi
466471
\lstset{escapechar=@, aboveskip=\parskip, belowskip=0pt,
467472
midpenalty=500, endpenalty=-50,
468473
emptylinepenalty=-250, semicolonpenalty=0}%
@@ -473,8 +478,27 @@
473478

474479
\lstnewenvironment{codeblock}{\CodeBlockSetup}{}
475480

476-
% Left-align listings titles
477481
\makeatletter
482+
483+
\newsavebox\@signalbox
484+
485+
% Redefine \begin{codeblock} to look for an immediately-preceding
486+
% \begin{example}.
487+
\global\@namedef{codeblock}{%
488+
\def\lstenv@name {codeblock}\begingroup%
489+
\ifhmode% no last box in vertical mode
490+
\setbox\@signalbox=\lastbox% grab and consume last box
491+
\copy\@signalbox% typeset the last box again
492+
\ifhbox\@signalbox%
493+
\ifdim\wd\@signalbox=0pt% zero-width box is our marker
494+
\def\codeblockbeginpenalty{100}%
495+
\fi%
496+
\fi%
497+
\fi%
498+
\lst@setcatcodes \catcode \active=\active \csname codeblock@\endcsname%
499+
}
500+
501+
% Left-align listings titles
478502
\def\lst@maketitle{\@makeleftcaption\lst@title@dropdelim}
479503
\long\def\@makeleftcaption#1#2{%
480504
\vskip\abovecaptionskip

0 commit comments

Comments
 (0)