| 1 |
% This file is part of the CTAN package named plain-grid. |
| 2 |
% |
| 3 |
% plaingridI.tex: text, math, |
| 4 |
% and support for insertions and a ragged bottom |
| 5 |
% Version 1.0, 05.05.2026 |
| 6 |
% |
| 7 |
% Copyright (C) 2026 Udo Wermuth (author) |
| 8 |
% |
| 9 |
% This program is free software: you can redistribute it and/or modify |
| 10 |
% it under the terms of the GNU General Public License as published by |
| 11 |
% the Free Software Foundation, either version 3 of the License, or |
| 12 |
% (at your option) any later version. |
| 13 |
% |
| 14 |
% This program is distributed in the hope that it will be useful, |
| 15 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
% GNU General Public License for more details. |
| 18 |
% |
| 19 |
% You should have received a copy of the GNU General Public License |
| 20 |
% along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 |
% |
| 22 |
|
| 23 |
\input plaingridM.tex |
| 24 |
{\toks255=\expandafter{\gridrestoreparams}% |
| 25 |
\let\\=\noexpand \xdef\gridrestoreparams{% |
| 26 |
\the\toks255 \skip\topins=\the\skip\topins |
| 27 |
\skip\footins=\the\skip\footins |
| 28 |
\\\GRIDrestorefootnotes |
| 29 |
\let\\\pagecontents=\\\GRIDpagecontents}} |
| 30 |
\skip\topins=0pt \skip\footins=\GRIDgrid |
| 31 |
\let\gridtopinsert=\topinsert % these three |
| 32 |
\let\gridmidinsert=\midinsert % macros call |
| 33 |
\let\gridpageinsert=\pageinsert% macro \@ins |
| 34 |
\catcode`@=11 % we access plain.tex's macros |
| 35 |
\def\gridendinsert{% end top/mid/pageinsert |
| 36 |
\egroup % \bgroup is in \@ins; ends \box0 |
| 37 |
\if@mid \p@gefalse \GRIDboxheight0(:\vfil)% |
| 38 |
\GRIDdim=\ht0 \advance\GRIDdim by\dp0 |
| 39 |
\advance\GRIDdim by \pagetotal |
| 40 |
\ifdim\pagegoal>\GRIDdim \gridskiponeline |
| 41 |
\box0 \gridsnaptonextbaseline |
| 42 |
\else \GRIDtopinsert1\fi % mid becomes top |
| 43 |
\else \GRIDtopinsert0\fi % page/topinsert |
| 44 |
\endgroup} % finish \begingroup of \@ins |
| 45 |
\def\GRIDtopinsert#1{% #1: 0/1=n/y box known |
| 46 |
\insert\topins{\penalty0 \floatingpenalty=0 |
| 47 |
\splittopskip=0pt \splitmaxdepth=\maxdimen |
| 48 |
\ifp@ge \GRIDdim=\dp0 \vbox to \vsize{% |
| 49 |
\unvbox0 \kern-\GRIDdim}% |
| 50 |
\else \GRIDboxheight0(:\ifnum#1=0 \vfil\fi |
| 51 |
\kern\grideotspace\GRIDgrid)\box0 \fi}} |
| 52 |
\def\grideotspace{0.5}% times leading at end |
| 53 |
\let\GRID@vfn=\vfootnote \let\GRID@f=\@foot |
| 54 |
\def\GRIDrestorefootnotes{\let\@foot=\GRID@f |
| 55 |
\let\vfootnote=\GRID@vfn} |
| 56 |
\def\vfootnote#1{\insert\footins\bgroup |
| 57 |
\interlinepenalty=0 \floatingpenalty\@MM |
| 58 |
\splitmaxdepth=\dp\GRIDsbox % use our strut |
| 59 |
\splittopskip=\ht\GRIDsbox \leftskip\z@skip |
| 60 |
\rightskip\z@skip \xspaceskip\z@skip |
| 61 |
\spaceskip\z@skip \textindent{#1}\footstrut |
| 62 |
\futurelet\next\fo@t} |
| 63 |
\def\@foot{\unhcopy\GRIDsbox\egroup}% ditto |
| 64 |
\let\gridfootnote=\footnote \catcode`@=12 |
| 65 |
\newif\ifgridragged % true: accept shifts |
| 66 |
\let\GRIDpagecontents=\pagecontents %to undo |
| 67 |
\def\pagecontents{% make grid-aware \footins |
| 68 |
\ifdim\pagefillstretch=1pt %`\dosupereject' |
| 69 |
\GRIDdim=0pt \else \GRIDgetpagestatus \fi |
| 70 |
\ifvoid\topins\else\unvbox\topins\fi |
| 71 |
\unvbox255 \GRIDpgreport % analyze page |
| 72 |
\ifvoid\footins \else \leftline{\unhcopy |
| 73 |
\GRIDsbox \gridrule(width 2truein)}% |
| 74 |
\vbox to \ht\footins{\unvbox\footins}\fi} |
| 75 |
\def\GRIDpgreport{\GRIDpgtype % set \GRIDcnt |
| 76 |
\ifnum\GRIDcnt=0 \GRIDpgerr \else |
| 77 |
\vss% use own msg not TeX's underfull vbox |
| 78 |
\ifnum\GRIDcnt>2 \ifgridragged\else |
| 79 |
\GRIDlog0{GRID: page ends ragged; fix}\fi |
| 80 |
\fi \ifodd\GRIDcnt\else \GRIDfnchk \fi |
| 81 |
\fi \edef\GRIDtmp{has a \ifgridragged |
| 82 |
ragged bottom \else shift \fi}% |
| 83 |
\edef\GRIDnext{\ifcase\GRIDcnt is bad\or |
| 84 |
is okay\or with footnote\or \GRIDtmp\or |
| 85 |
with footnote \GRIDtmp\fi}\GRIDlog2{% |
| 86 |
GRID: page (\the\GRIDdim) \GRIDnext}} |
| 87 |
\def\GRIDchkii{% test two dimens; \GRIDcnt>0 |
| 88 |
\ifdim\GRIDdim=\dimen0 \else\ifdim\GRIDdim= |
| 89 |
\dimen2 \else \GRIDcnt=0 \fi\fi} |
| 90 |
\def\GRIDchkiii{% same with three dimensions |
| 91 |
\ifdim\GRIDdim=\dimen0 \else\ifdim\GRIDdim= |
| 92 |
\dimen2 \else\ifdim\GRIDdim=\dimen4 \else |
| 93 |
\GRIDcnt=0 \fi\fi\fi} |
| 94 |
\def\GRIDadd(#1){% add #1 to \dimen 0, 2, 4 |
| 95 |
\advance\dimen0 by #1\advance \dimen2 by |
| 96 |
#1\advance\dimen4 by #1\ignorespaces} |
| 97 |
\def\GRIDpgtype{% set dimen for \GRIDchkii/i |
| 98 |
% Test 1: good page breaks |
| 99 |
\dimen0=-\GRIDgrid \dimen2=0pt |
| 100 |
\GRIDcnt=1 \GRIDchkii |
| 101 |
% 2: footnote +(\dp\strutbox+\baselineskip) |
| 102 |
\ifnum\GRIDcnt=0 \dimen0=\dp\GRIDsbox |
| 103 |
\dimen2=\dimen0\advance\dimen2 by\GRIDgrid |
| 104 |
\GRIDcnt=2 \GRIDchkii |
| 105 |
% Tests 3 & 4: shifts; three values |
| 106 |
\ifnum\GRIDcnt=0 \dimen0=-0.25\GRIDgrid |
| 107 |
\dimen2=-0.33333\GRIDgrid |
| 108 |
\dimen4=-0.5\GRIDgrid |
| 109 |
\GRIDcnt=3 \GRIDchkiii |
| 110 |
\ifnum\GRIDcnt=0 \GRIDadd(\GRIDgrid) |
| 111 |
\GRIDcnt=3 \GRIDchkiii |
| 112 |
% Tests 5 & 6: footnote and shift |
| 113 |
\ifnum\GRIDcnt=0 \GRIDadd(\dp\GRIDsbox) |
| 114 |
\GRIDcnt=4 \GRIDchkiii |
| 115 |
\ifnum\GRIDcnt=0 \GRIDadd(\GRIDgrid) |
| 116 |
\GRIDcnt=4 \GRIDchkiii \fi\fi\fi\fi\fi} |
| 117 |
\def\GRIDgetpagestatus{\GRIDdim=\vsize |
| 118 |
\advance\GRIDdim by -\pagetotal % - text |
| 119 |
\advance\GRIDdim by -\ht\topins % - tops |
| 120 |
\advance\GRIDdim by -\ht\footins}% - foots |
| 121 |
\def\GRIDfnchk{{% check if footnote is moved |
| 122 |
\advance\GRIDdim by -\dp\GRIDsbox |
| 123 |
\ifdim\GRIDdim<\GRIDgrid \else % > one line |
| 124 |
\ifdim\pagetotal>\pagegoal \GRIDlog0{% |
| 125 |
GRID: page too short; fix}\GRIDlog1{The |
| 126 |
page is a line too short because a line |
| 127 |
with a footnote must be moved.}\fi\fi}} |
| 128 |
\def\GRIDpgerr{\GRIDlog0{GRID: ugly page; |
| 129 |
fix}\GRIDlog1{The page is not filled as |
| 130 |
expected. Maybe a text line with footnotes |
| 131 |
is moved to the next page, a skip violates |
| 132 |
the grid, or a forbidden command was used. |
| 133 |
Check the \vbox shown in TeX's warning.}} |