Skip to content

Commit cda64ac

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

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

source/macros.tex

+24-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,9 +462,10 @@
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+
\def\codeblockbeginpenalty{0} % default penalty before codeblock
465466
\newcommand{\CodeBlockSetup}{%
466467
\lstset{escapechar=@, aboveskip=\parskip, belowskip=0pt,
467-
midpenalty=500, endpenalty=-50,
468+
beginpenalty=\codeblockbeginpenalty, midpenalty=500, endpenalty=-50,
468469
emptylinepenalty=-250, semicolonpenalty=0}%
469470
\renewcommand{\tcode}[1]{\textup{\CodeStylex{##1}}}
470471
\renewcommand{\term}[1]{\textit{##1}}%
@@ -473,8 +474,27 @@
473474

474475
\lstnewenvironment{codeblock}{\CodeBlockSetup}{}
475476

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

0 commit comments

Comments
 (0)