| 1 |
\newcount\ptx@penalty |
| 2 |
\ptx@penalty=-10000 |
| 3 |
|
| 4 |
\def\newbreakpenalty#1{% |
| 5 |
\advance\ptx@penalty-1 |
| 6 |
\edef#1{\the\ptx@penalty\relax} |
| 7 |
} |
| 8 |
|
| 9 |
\newbreakpenalty\clearpagepenalty |
| 10 |
\def\clearpage{\vfil\penalty\clearpagepenalty} |
| 11 |
|
| 12 |
\newbreakpenalty\breakpagepenalty |
| 13 |
\def\breakpage{\vfil\penalty\breakpagepenalty} |
| 14 |
|
| 15 |
\def\whitepage{\clearpage\shipout\hbox{}\advancepageno} |
| 16 |
|
| 17 |
% TODO: Rewrite in Lua. |
| 18 |
\def\needspace#1{% |
| 19 |
\par\penalty0 |
| 20 |
\ifdim\dimexpr\pagegoal-\pagetotal<\dimexpr#1\relax |
| 21 |
\breakpage |
| 22 |
\fi |
| 23 |
} |
| 24 |
|
| 25 |
\def\iflines#1{% |
| 26 |
\par\penalty0 |
| 27 |
\ifdim\dimexpr(\pagegoal-\pagetotal) < \numexpr(#1)\baselineskip |
| 28 |
\expandafter\secondoftwo |
| 29 |
\else |
| 30 |
\expandafter\firstoftwo |
| 31 |
\fi} |
| 32 |
|
| 33 |
\long\def\ignorepars#1{% |
| 34 |
\ifxnextnospace\par |
| 35 |
{\gobbleoneand{\ignorepars{#1}}} |
| 36 |
{#1}% |
| 37 |
} |
| 38 |
|
| 39 |
\def\sectioncommand#1#2#3{% |
| 40 |
\ifnext[ |
| 41 |
{\ptx@sectioncommand{#1}{#2}{#3}} |
| 42 |
{\ptx@sectioncommand{#1}{#2}{#3}[]}% |
| 43 |
} |
| 44 |
|
| 45 |
\def\ptx@sectioncommand#1#2#3[#4]{% |
| 46 |
\Section execute {section}{#1}{#2}{#3}{#4}% |
| 47 |
\ignorepars{}% |
| 48 |
} |
| 49 |
\def\makeroman#1{% |
| 50 |
\uppercase\expandafter{\romannumeral#1\relax}% |
| 51 |
} |
| 52 |
|
| 53 |
\newif\ifsectiontitle |
| 54 |
|
| 55 |
\luacode |
| 56 |
pitex.sections = { sections = {} } |
| 57 |
function pitex.sections.increment (type) |
| 58 |
local n = type |
| 59 |
type = pitex.sections.sections[type] or { num = 0 } |
| 60 |
type.num = type.num + 1 |
| 61 |
local level = type.level |
| 62 |
if level then |
| 63 |
for name, tb in pairs(pitex.sections.sections) do |
| 64 |
tb.num = tb.level > level and 0 or tb.num |
| 65 |
end |
| 66 |
end |
| 67 |
end |
| 68 |
function pitex.sections.counter (type) |
| 69 |
type = pitex.sections.sections[type] |
| 70 |
return type and type.num or -1 |
| 71 |
end |
| 72 |
\luacode/ |
| 73 |
|
| 74 |
\def\declaresection#1#2{% |
| 75 |
\ptx@lua{pitex.sections.sections.#1 = {num = 0, level = #2}}% |
| 76 |
} |
| 77 |
|
| 78 |
\def\incrementsection#1{% |
| 79 |
\ptx@lua{pitex.sections.increment("#1")}% |
| 80 |
} |
| 81 |
|
| 82 |
\def\getsectioncounter#1{% |
| 83 |
\ptx@lua{tex.print(pitex.sections.counter ("#1"))}% |
| 84 |
} |
| 85 |
|
| 86 |
%%% Gates in section %%% |
| 87 |
|
| 88 |
\gates new \Section {Section} |
| 89 |
\Section list {section} [4] |
| 90 |
(section_break) [2] |
| 91 |
. [section_vmode] [1] ?{conditional = \ifvalue #1 : vmode = true } {\par\removelastskip\penalty0 } |
| 92 |
. [section_clearpage] [1] ?{conditional = \ifvalue #1 : clear = true } {\clearpage} |
| 93 |
. [section_beforeskip] [2] ?{conditional = -\ifvalue #1 : clear = true } |
| 94 |
{% Or skip some lines. The "beforeskip" attribute is |
| 95 |
% the number of blank lines one wants before a section |
| 96 |
% title, afterskip is the same thing after, |
| 97 |
% and "minimum" is the minimum number of lines |
| 98 |
% one wants after the section title. |
| 99 |
\ifexpression{% |
| 100 |
-\ifdim\dimexpr(\pagegoal-\pagetotal) < 0pt & |
| 101 |
\ifdim\dimexpr(\pagegoal-\pagetotal) < \numexpr(\usevalueor #1 : beforeskip 0+\usevalueor #1 : afterskip 0+\usevalueor #1 : minimum 0) \baselineskip} |
| 102 |
{\breakpage} % Not enough room. |
| 103 |
{\passvaluenobracesand\vskip #1 : beforeskip \baselineskip{}}} |
| 104 |
% |
| 105 |
[section_advance] [1] {\incrementsection{#1}} |
| 106 |
% |
| 107 |
[section_bookmark] [4] ?{conditional = \ifvalue #1 : link = true } {% |
| 108 |
\passvalueand{\outline[meta = #1bookmark]} #1 : bookmarklevel {[#4]{#3}}{}} |
| 109 |
% |
| 110 |
[section_toc] [3] ?{conditional = \ifvalue #1 : toc = true } {% |
| 111 |
\edef\ptx@temp{{#1}{\ifvalue #1 : number = none {}{\getsectioncounter{#1}}}{\unexpanded{\unexpanded{#3}}}{\the\pageno}}% |
| 112 |
\expandafter\writeout\expandafter*\expandafter{\expandafter\noexpand\expandafter\ptx@toc\ptx@temp}} |
| 113 |
% |
| 114 |
[section_pre] {% |
| 115 |
\bgroup |
| 116 |
\maintextfalse |
| 117 |
\sectiontitletrue |
| 118 |
\ptx@section_attribute=0\relax} |
| 119 |
% |
| 120 |
(section_typeset) [2] |
| 121 |
. [section_number] [2] {% |
| 122 |
\ifvalue #1 : number = none |
| 123 |
{\Section return3 {#1}{}{#2}} |
| 124 |
{\Section return3 |
| 125 |
{#1} |
| 126 |
{\usevalueor #1 : numbercommand \unbrace |
| 127 |
{\usevalue #1 : beforenumber |
| 128 |
\ifcasevalue #1 : number |
| 129 |
\val roman \makeroman |
| 130 |
\val arabic \unbrace |
| 131 |
\endval{\getsectioncounter{#1}}% |
| 132 |
\usevalue #1 : afternumber }} |
| 133 |
{#2}}} |
| 134 |
% |
| 135 |
. [section_heading] [3] {% |
| 136 |
\Section return2 {#1}{#2\usevalueor #1 : function \unbrace{#3}\usevalue #1 : aftertitle }} |
| 137 |
% |
| 138 |
. [section_addfont] [2] {\Section return {#1}{\usevalue #1 : font #2}} |
| 139 |
% |
| 140 |
. [section_addcolor] [2] ?{conditional = \ifattribute #1 : color } {% |
| 141 |
\Section return {#1}{\passvalueand\color #1 : color {{#2}}{}}} |
| 142 |
% |
| 143 |
. [section_do] [2] {% |
| 144 |
\noindent |
| 145 |
\settovalue\hskip #1 : indent |
| 146 |
#2% |
| 147 |
\ifvalue #1 : inline = true {}{\settovalue\rightskip #1 :ragged \endgraf}} |
| 148 |
% |
| 149 |
[section_post] {\egroup} |
| 150 |
% |
| 151 |
[section_afterskip] [1] ?{conditional = -\ifvalue #1 : inline = true } {% |
| 152 |
\vskip\usevalueor #1 : afterskip 0\baselineskip |
| 153 |
\ifvalue #1 : removenextindent = true {\removenextindent}{}} |
| 154 |
|
| 155 |
\setparameter metasection : |
| 156 |
clear = false |
| 157 |
vmode = true |
| 158 |
minimum = 3 |
| 159 |
inline = false |
| 160 |
number = arabic |
| 161 |
afternumber = "\kern.3em" |
| 162 |
link = true |
| 163 |
ragged = 0pt |
| 164 |
toc = true |
| 165 |
removenextindent = true |
| 166 |
|
| 167 |
\declaresection {chapter} 1 |
| 168 |
\declaresection {section} 2 |
| 169 |
\declaresection {subsection} 3 |
| 170 |
\declaresection {paragraph} 4 |
| 171 |
|
| 172 |
\setparameter chapter section subsection paragraph: |
| 173 |
meta = metasection |
| 174 |
|
| 175 |
\setparameter chapter : |
| 176 |
clear = true |
| 177 |
number = arabic |
| 178 |
afterskip = 3 |
| 179 |
indent = "0pt plus 1fill" |
| 180 |
beforenumber = "chapitre " |
| 181 |
afternumber = {\par\hfill} |
| 182 |
bookmarklevel = 1 |
| 183 |
|
| 184 |
\setparameter section : |
| 185 |
beforeskip = 2 |
| 186 |
bookmarklevel = 2 |
| 187 |
|
| 188 |
\setparameter subsection paragraph: |
| 189 |
font = \it |
| 190 |
beforeskip = 1 |
| 191 |
|
| 192 |
\setparameter subsection: |
| 193 |
beforenumber = "\getsectioncounter{section}." |
| 194 |
minimum = 3 |
| 195 |
bookmarklevel = 3 |
| 196 |
|
| 197 |
\setparameter paragraph: |
| 198 |
beforenumber = "\getsectioncounter{section}.\getsectioncounter{subsection}." |
| 199 |
minimum = 2 |
| 200 |
inline = true |
| 201 |
aftertitle = ".\hskip.333em" |
| 202 |
bookmarklevel = 4 |
| 203 |
|
| 204 |
\setparameter chapterbookmark sectionbookmark subsectionbookmark paragraphbookmark: |
| 205 |
meta = navigator |
| 206 |
|
| 207 |
\def\chaptertitle{}% |
| 208 |
\freedef\chapter{% |
| 209 |
\sectioncommand{chapter}{#1}{#1}% |
| 210 |
} |
| 211 |
\freedef\section{\sectioncommand{section}{#1}{#1}} |
| 212 |
\freedef\subsection{\sectioncommand{subsection}{#1}{#1}} |
| 213 |
\freedef\paragraph{\sectioncommand{paragraph}{#1}{#1}} |
| 214 |
|
| 215 |
\newif\ifptx@sectioninfile |
| 216 |
\def\sectioninfile{% |
| 217 |
\ifnextnospace* |
| 218 |
{\ptx@sectioninfilefalse |
| 219 |
\gobbleoneand\ptx@sectioninfile} |
| 220 |
{\ptx@sectioninfiletrue |
| 221 |
\ptx@sectioninfile}% |
| 222 |
} |
| 223 |
|
| 224 |
\freedef\ptx@sectioninfile{% |
| 225 |
\ptx@sectioninfile_do{#1}% |
| 226 |
} |
| 227 |
\def\ptx@sectioninfile_do#1 #2 #3 {% |
| 228 |
\sectioncommand{#2}{#1}{#1}% |
| 229 |
\ptx@section_getlabel{#3}% |
| 230 |
\ifptx@sectioninfile |
| 231 |
\input #3\relax |
| 232 |
\fi |
| 233 |
} |
| 234 |
\newstring/ |
| 235 |
\newwhile\ptx@section_getlabel1{#1}{% |
| 236 |
\ifcontains/{#1}% |
| 237 |
{\splitstringat/{#1}{\gobbleoneand\changewhile}} |
| 238 |
{\label{#1}\breakwhile}% |
| 239 |
} |
| 240 |
\freedef\ptx@sectioninfile_gobble{% |
| 241 |
\ptx@sectioninfile_gobblerest |
| 242 |
} |
| 243 |
\def\ptx@sectioninfile_gobblerest#1 #2 #3 {} |
| 244 |
|
| 245 |
\newattribute\ptx@section_attribute |
| 246 |
% |
| 247 |
% Move pending titles if they happen. They shouldn't by themselves, |
| 248 |
% but the next paragraph might want more room than available |
| 249 |
% and clear the page. Hence this. |
| 250 |
% It reads box 255 backwards and move everything with set |
| 251 |
% section attribute to the next page. If the first such material |
| 252 |
% is a line, a (totally arbitrary) one-line skip is added, unless |
| 253 |
% there's already a skip on top of the next page that isn't |
| 254 |
% TeX-inserted (e.g. \baselineskip). |
| 255 |
% This is probably totally insufficient. |
| 256 |
% |
| 257 |
\luacode |
| 258 |
onelineskip = node.new(11) |
| 259 |
onelineskip.kern = tex.baselineskip.width |
| 260 |
remove_pendingtitles = function (head) |
| 261 |
local item, first = node.slide(head), true |
| 262 |
while item do |
| 263 |
if node.has_attribute(item,\attributenumber\ptx@section_attribute) then |
| 264 |
local nextitem = item.prev |
| 265 |
node.remove(head,item) |
| 266 |
if not ((item.id == 10 or item.id == 11) and first) then |
| 267 |
node.insert_before(tex.lists.contrib_head, tex.lists.contrib_head, item) |
| 268 |
tex.lists.contrib_head = item |
| 269 |
end |
| 270 |
if item.id == 0 then |
| 271 |
if first then |
| 272 |
first = false |
| 273 |
if not (tex.lists.contrib_head.next.id == 10 and tex.lists.contrib_head.next.subtype == 0) then |
| 274 |
node.insert_after(tex.lists.contrib_head, item, node.copy(onelineskip)) |
| 275 |
lualog("A title has been moved from page " .. tex.count[0] .. |
| 276 |
" to page " .. tex.count[0]+1 .. ".") |
| 277 |
end |
| 278 |
end |
| 279 |
else |
| 280 |
end |
| 281 |
item = nextitem |
| 282 |
else |
| 283 |
if item.id == 0 or item.id == 1 then |
| 284 |
item = nil |
| 285 |
else |
| 286 |
item = item.prev |
| 287 |
end |
| 288 |
end |
| 289 |
end |
| 290 |
end |
| 291 |
\luacode/ |
| 292 |
|
| 293 |
\def\removependingtitles{% |
| 294 |
\ptx@lua{remove_pendingtitles(tex.box[255].list)}% |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
% ToC: this is an inefficient mess. |
| 299 |
|
| 300 |
\def\ptx@toc_tok{} |
| 301 |
\def\ptx@toc#1#2#3#4{% |
| 302 |
\addright\ptx@toc_tok{% |
| 303 |
\usecs{ptx@toc_item:#1}{#2}{#3}{#4}}% |
| 304 |
} |
| 305 |
|
| 306 |
\def\tableofcontents{% |
| 307 |
\bgroup |
| 308 |
\parindent=0pt |
| 309 |
\ifemptycommand\ptx@toc_tok |
| 310 |
{\ptx@warn{No table of contents.}} |
| 311 |
{\ptx@toc_tok}% |
| 312 |
\egroup |
| 313 |
} |
| 314 |
|
| 315 |
\long\def\ptx@def_tocitem#1#2#3#4{% |
| 316 |
\defcs{ptx@toc_item:#1}##1##2##3{% |
| 317 |
\edefcs{ptx@toc_current#1}{##1}% |
| 318 |
\bgroup\par\quitvmode |
| 319 |
\leftskip#2\relax |
| 320 |
\rightskip=0pt plus 1fil |
| 321 |
#3\reverse\iffemptystring{##1}{\llap{#4##1\kern1em}}% |
| 322 |
##2\leaders\hbox{. }\hfill\hbox to .8cm{\hfil##3}\par |
| 323 |
\egroup}% |
| 324 |
} |
| 325 |
|
| 326 |
\ptx@def_tocitem{chapter}{0pt}{\big\bf\sc}{} |
| 327 |
\ptx@def_tocitem{section}{1cm}{\sc}{} |
| 328 |
\ptx@def_tocitem{subsection}{2cm}{\it} |
| 329 |
{\reverse\iffemptycs{ptx@toc_currentsection}{\usecs{ptx@toc_currentsection}.}} |
| 330 |
\ptx@def_tocitem{paragraph}{3cm}{} |
| 331 |
{\reverse\iffemptycs{ptx@toc_currentsection}{\usecs{ptx@toc_currentsection}.}% |
| 332 |
\reverse\iffemptycs{ptx@toc_currentsubsection}{\usecs{ptx@toc_currentsection}}.} |