方程式的网格排版

方程式的网格排版

关于 TeX 中的网格排版有很多讨论。“几乎不可能”……我想证明这是可能的。我找到了一种让显示数学遵循网格的方法。与grid我的解决方案中的包不同,数学基线也与其他基线对齐。它通过吸收 tokes 来工作,如本线程所示: 普通 LuaTeX:检测 displaymath 是否以 $$ 或 \Ustopdisplaymath 结尾

梅威瑟:

% Plain LuaTeX
\input 2columns.mac
\input luaotfload.sty
\catcode`\@=11

\newdimen\b@selineskip% the main baselineskip
\[email protected]\p@
\def\setstrut{\setbox\strutbox=\hbox{\vrule height.7\baselineskip
        depth .3\baselineskip width\z@}}% as in LaTeX

\font\twentyonerm="texgyrepagella-regular.otf:mode=node;script=latn;%
  language=DFLT;+tlig" at 20.74\p@
\font\twentyonemath="texgyrepagella-math.otf:mode=base;script=math" at 20.74\p@
\font\fourteenmath="texgyrepagella-math.otf:mode=base;script=math" at 14.4\p@
\font\tenmath="texgyrepagella-math.otf:mode=base;script=math" at 10\p@

\twentyonerm
\textfont\@ne\twentyonemath
\scriptfont\@ne\fourteenmath
\scriptscriptfont\@ne\tenmath
\normalbaselineskip\b@selineskip
\normallineskip\z@% very important!
\normalbaselines
\setstrut

\raggedbottom
\topskip.7\b@selineskip% important when \displaylines are split 
                       % over pages/columns and kills glue
\parskip\z@

% For the example equations
\Umathcode`!="5"1`!     \let\mathexclam=!%21
\Umathcode`.="0"1`.     \let\mathperiod=.%2E
\Umathcode`+="2"1`+     \let\mathplus=+
\Umathcode`-="2"1`−
\Umathcode`−="2"1`−
\Umathcode`/="0"1`/     \let\mathslash=/    \let\slash\mathslash%2F
\Umathcode`=="3"1`=     \let\equal==
\Umathcode`0="7"1`0
\Umathcode`1="7"1`1
\Umathcode`2="7"1`2
\Umathcode`3="7"1`3
\Umathcode`4="7"1`4
\Umathcode`6="7"1`6
\Umathcode`8="7"1`8
\Umathcode`π="7"1`π     \let\pi=π
\Umathcode`∑="1"1`∑     \let\sum=∑%2211
\Umathcode`∞="0"1`∞     \let\infty=∞%221E
\Umathcode`≈="3"1`≈     \let\approx=≈
\Umathcode`(="4"1`(     \Udelcode`(="1`(
\Umathcode`)="5"1`)     \Udelcode`)="1`)

\newdimen\@tempdima
\newdimen\@tempdimb
\newdimen\@tempdimc
\newdimen\@tempdimd

\abovedisplayskip\normalbaselineskip
\abovedisplayshortskip\normalbaselineskip
\belowdisplayskip\normalbaselineskip
\belowdisplayshortskip\normalbaselineskip

% make a box fit to the grid
\def\@fittogrid#1{\@tempdima\ht#1%
    \advance\@tempdima.1\b@selineskip%
    \divide\@tempdima\b@selineskip%
    \multiply\@tempdima\b@selineskip%
    \advance\@tempdima.7\b@selineskip%
    \global\ht#1\@tempdima%
    \@tempdima\dp#1%
    \advance\@tempdima.5\b@selineskip%
    \divide\@tempdima\b@selineskip%
    \multiply\@tempdima\b@selineskip%
    \advance\@tempdima.3\b@selineskip%
    \global\dp#1\@tempdima}

\newbox\gridb@x
\def\gridbox#1{\setbox\gridb@x\hbox{\Ustartmath#1\Ustopmath}%
    \@fittogrid\gridb@x%
    \box\gridb@x}

% Absorb tokens after $$
\newtoks\d@absorbed

\newdimen\@displaymathgap
\newdimen\@displaymathindent
\newif\if@eqno
\newdimen\@eqnnumheight
\newdimen\@eqnnumdepth
\newdimen\@eqnnumraise

\@displaymathindent40pt

\newif\ifh@lign
\h@lignfalse

\begingroup
\def~{\global\let\spacetoken= }~ %
\endgroup

\def\d@save@tokens#1{\global\d@absorbed\expandafter{\the\d@absorbed#1}}

\def\d@absorb{\global\d@absorbed={}\d@absorb@}
\def\d@absorb@{\futurelet\@next\d@absorb@check}
\def\d@absorb@check{%
    \ifx\@next\spacetoken
    \let\@next=\d@absorb@space
    \else\ifx\@next\bgroup
    \let\@next=\d@absorb@group
    \else\ifx\@next\displaylines% detect alignment displays!!!!! Not a nice solution! (A)
    \global\h@ligntrue
    \let\@next=\d@absorb@gobble
    \else\ifx\@next\eqalignno
    \global\h@ligntrue
    \let\@next=\d@absorb@gobble
    \else\ifx\@next\halign
    \global\h@ligntrue
    \let\@next=\d@absorb@gobble
    \else\ifx\@next\leqalignno
    \global\h@ligntrue
    \let\@next=\d@absorb@gobble
    \else\ifx\@next$%
    \let\@next=\d@absorb@finishI
    \else\ifx\@next\Ustopdisplaymath
    \let\@next=\d@absorb@finishII
    \else
    \let\@next=\d@absorb@gobble
    \fi\fi\fi\fi\fi\fi\fi\fi
    \@next}

\begingroup
\def~ {\d@save@tokens{ }\d@absorb@}
\global\let\d@absorb@space=~
\endgroup
\def\d@absorb@group#1{\d@save@tokens{{#1}}\d@absorb@}
\def\d@absorb@gobble#1{\d@save@tokens{#1}\d@absorb@}
\def\d@absorb@finishI$${\d@absorb@result}
\def\d@absorb@finishII\Ustopdisplaymath{\d@absorb@result}

\newbox\@eqbox
\newbox\@eqnobox

% Make an equation from the tokens
\def\d@absorb@result{\ifh@lign\the\d@absorbed\else
    \def\eqno{&\global\@eqnotrue}
    \def\leqno{&\global\@eqnofalse}
    \tabskip\z@skip
    \halign to \displaywidth{\setbox\@eqbox\hbox{\Ustartmath\displaystyle{##}\Ustopmath}
        \@fittogrid\@eqbox
        \global\@tempdimb\ht\@eqbox
        \global\@tempdimc\dp\@eqbox
        \global\@tempdimd\wd\@eqbox
        \hskip\@displaymathindent\box\@eqbox
        \tabskip0pt plus 1000pt minus 1000pt
        &\setbox\@eqnobox\hbox{\Ustartmath##\Ustopmath}
        \@fittogrid\@eqnobox
        \@displaymathgap\displaywidth% No collision between equation and tag
        \advance\@displaymathgap-\@displaymathindent
        \advance\@displaymathgap-\@tempdimd
        \advance\@displaymathgap-\wd\@eqnobox
        \@eqnnumraise\z@
        \llap{\hbox to \displaywidth{\if@eqno\hss
                \ifdim\@displaymathgap<0pt
                \@eqnnumraise-\@tempdimc
                \advance\@eqnnumraise-.7\normalbaselineskip\fi
                \else\ifdim\wd\@eqnobox>\@displaymathindent
                \@eqnnumraise\@tempdimb
                \advance\@eqnnumraise.3\normalbaselineskip\fi\fi
                \raise\@eqnnumraise\box\@eqnobox%
                \if@eqno\else\hss\fi}}%
        \tabskip\z@skip\cr
        \strut\cr% align to baselines at the top
        \noalign{\vskip-\baselineskip}
        \the\d@absorbed\cr}
    \fi$$}

\def\afterdisplay{\global\h@lignfalse\strut% align to baseline at the bottom (B)
    \vskip-\b@selineskip\vskip-\parskip\noindent\ignorespaces}
\jot=\baselineskip
\everydisplay{\aftergroup\afterdisplay\d@absorb}

%Redefine \displaylines to make it compatible to grid typesetting
\def\displaylines#1{\displ@y \tabskip\z@skip
    \halign{\hbox to\displaywidth{\hskip\@displaymathindent\@lign
            \gridbox{\displaystyle{##}}\hfil}\crcr
            \strut\cr
            \noalign{\vskip-\baselineskip}%
            #1\crcr}}

\noindent Let's try:
$$ 

相关内容