Skip to content

Commit 00da37f

Browse files
committed
[macros] Avoid page break between example and codeblock introducers
if they are immediately following each other.
1 parent 45578ff commit 00da37f

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
@@ -280,7 +280,7 @@
280280
\newcommand{\leftshift}[1]{\ensuremath{\mathbin{\mathsf{lshift}_{#1}}}}
281281

282282
%% Notes and examples
283-
\newcommand{\noteintro}[1]{[\textit{#1}:}
283+
\newcommand{\noteintro}[1]{[\textit{#1}: }
284284
\newcommand{\noteoutro}[1]{\textit{\,---\,#1}\kern.5pt]}
285285

286286
% \newnoteenvironment{ENVIRON}{BEGIN TEXT}{END TEXT}
@@ -292,7 +292,7 @@
292292
\newcommand{\newnoteenvironment}[3]{
293293
\newsubclausecounter{#1}
294294
\newenvironment{tail#1}
295-
{\par\small\stepcounter{#1}\noteintro{#2}}
295+
{\par\small\stepcounter{#1}\noteintro{#2}\hbox to 0pt{}\ignorespaces}
296296
{\noteoutro{#3}}
297297
\newenvironment{#1}
298298
{\begin{tail#1}}
@@ -497,7 +497,12 @@
497497
% Our usual abbreviation for 'listings'. Comments are in
498498
% italics. Arbitrary TeX commands can be used if they're
499499
% surrounded by @ signs.
500-
\newcommand{\CodeBlockSetup}{%
500+
\def\codeblockbeginpenalty{-1} % default: no penalty change before codeblock
501+
\newcommand{\CodeBlockSetup}{
502+
\ifnum\codeblockbeginpenalty=-1
503+
\else
504+
\presetkeys{lst}{beginpenalty=\codeblockbeginpenalty}{}%
505+
\fi
501506
\lstset{escapechar=@, aboveskip=\parskip, belowskip=0pt,
502507
midpenalty=500, endpenalty=-50,
503508
emptylinepenalty=-250, semicolonpenalty=0}%
@@ -508,8 +513,27 @@
508513

509514
\lstnewenvironment{codeblock}{\CodeBlockSetup}{}
510515

511-
% Left-align listings titles
512516
\makeatletter
517+
518+
\newsavebox\@signalbox
519+
520+
% Redefine \begin{codeblock} to look for an immediately-preceding
521+
% \begin{example}.
522+
\global\@namedef{codeblock}{%
523+
\def\lstenv@name {codeblock}\begingroup%
524+
\ifhmode% no last box in vertical mode
525+
\setbox\@signalbox=\lastbox% grab and consume last box
526+
\copy\@signalbox% typeset the last box again
527+
\ifhbox\@signalbox%
528+
\ifdim\wd\@signalbox=0pt% zero-width box is our marker
529+
\def\codeblockbeginpenalty{100}%
530+
\fi%
531+
\fi%
532+
\fi%
533+
\lst@setcatcodes \catcode \active=\active \csname codeblock@\endcsname%
534+
}
535+
536+
% Left-align listings titles
513537
\def\lst@maketitle{\@makeleftcaption\lst@title@dropdelim}
514538
\long\def\@makeleftcaption#1#2{%
515539
\vskip\abovecaptionskip

0 commit comments

Comments
 (0)