After reading this answer
Automatically prevent extra line spacing because of math
by David Carlisle, I experimented with the code to get grid typesetting. My approach: The height of a line is always (n - 0.3)\baselineskip
and the depth (n - 0.7)\baselineskip
with n ∈ ℕ
. Then I delete the glue. This works fine for big inline math or other big obejects in horizontal mode, but it does not work with display math as you can see in the MWE.
So I have some questions:
- What ist wrong here?
- How can I influence the height and depth of a display math line in
post_linebreak_filter
or somewhere else? - How can I get the spacing right in this case? That means that the baseline of the display math should be on the grid and the lines after the display math should also obey the grid.
MWE:
\documentclass[12pt]{article}
\usepackage{showframe,color,unicode-math}
% Check grid typesetting
\def\grid{%
\vtop to0pt{%
\hsize=0pt \noindent \color{red}%
X\\X\\X\\X\\X\\X\\X\\X\\X\\X\\X\\X\\X\\X\\X\\X\\X\\X\vss}}
\raggedbottom
\directlua{
function fixlines(h,c)
local B=tex.baselineskip.width
for n in node.traverse(h) do
%
% correct height and depth
if (n.id==0 or n.id==11) then
n.height = (math.ceil((n.height+.3*B)/B)-.3)*B
n.depth = (math.ceil((n.depth+.7*B)/B)-.7)*B
end
%
% lineskip or baselineskip: kill glue
if n.id==12 and (n.subtype==1 or n.subtype==2) then
n.width=0 % kill glue
end
end
return h
end
luatexbase.add_to_callback('post_linebreak_filter', fixlines, 'fix line spacing')
}
% test big objects
\def\𝟙{\begin{pmatrix}1&0&0\\0&1&0\\0&0&1\end{pmatrix}}
\usepackage{lipsum}
\begin{document}
\parskip0pt
\lineskip0pt
\lineskiplimit0pt
% displaymath spacing
\abovedisplayskip\baselineskip
\abovedisplayshortskip\baselineskip
\belowdisplayskip\baselineskip
\belowdisplayshortskip\baselineskip
\noindent \grid I like grid typesetting. Let us test some inline math. Let us
test some very big inline math with : $𝟙₃ = \𝟙$. The next line obeys the grid,
very good! 2 baselines were omitted above, one below.
What about \detokenize{$$}-math?
% Sorry for using $$, but I usually use Plain LuaTeX.
$$ a+b+c+d~\hbox{Why don't I have the same baseline as the red X?} $$
The grid is not obeyed any more. What is wrong here? How can I influence the
height and depth of a display math line and its spacing in \detokenize{post_linebreak_filter}?
\end{document}
Edit: I have added the following code:
\directlua{
local B=tex.baselineskip.width
function killdisplayskips(h)
for n in node.traverse(h) do
if n.id==12 and (n.subtype==4 or n.subtype==5 or n.subtype==6 or n.subtype==7) then
n.width=0 % kill glue
end
end
return h
end
luatexbase.add_to_callback("append_to_vlist_filter", killdisplayskips, "killdisplayskips")
}
The spacing is still wrong (but it changed).
append_to_vlist_filter
perhaps that has alocation
argument that says when it is handling mathappend_to_vlist_filter
the spacing inside the matrix is also changed. But I am still not able to change the height and depth of a line that contains display math.