Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display formulas and the math module: How to avoid \parskip below the formula? #762

Open
giovanni111 opened this issue Nov 18, 2024 · 13 comments
Labels
area: math bug Something isn't working in code we maintain (directly) fixed in release issue is fixed and will be deployed in the next release of package or kernel

Comments

@giovanni111
Copy link

\DocumentMetadata{
  testphase   = {
    math % adds \parskip below display formulas
  }}
\documentclass{article}
\parskip\baselineskip
\usepackage{amsmath}
\begin{document}
Some Text
$$A+B=C$$
More Text
\end{document}

When \parskip is not 0, I get a different output for display formulas if using the math module. Perhaps \__math_tag_dollardollar_display_end: should remove \parskip somehow? What would you suggest?

@josephwright
Copy link
Member

Looks to me like

    \penalty \postdisplaypenalty
    \skip_vertical:n { -\l_@@_tmpa_skip  } % insert the correct skip

has the wrong sign and should be

    \penalty \postdisplaypenalty
    \skip_vertical:n { \l_@@_tmpa_skip  } % insert the correct skip

otherwise we are adding twice the skip we've moved around.

@josephwright
Copy link
Member

PR coming up ...

@u-fischer
Copy link
Member

u-fischer commented Nov 18, 2024

@josephwright if you change the sign then this one breaks imho again: #721

@FrankMittelbach
Copy link
Member

yes, I think this is more subtle, definitive a bug ...

@FrankMittelbach FrankMittelbach added bug Something isn't working in code we maintain (directly) analyse something that needs further checking and perhaps code changes area: math labels Nov 18, 2024
@FrankMittelbach
Copy link
Member

As an aside @giovanni111 : $$...$$ is not supported LaTeX syntax. Not that it matters here, but it has other bad side effects. Please use \[... \] or with amsmath \begin{equation*} instead.

@josephwright
Copy link
Member

yes, I think this is more subtle, definitive a bug ...

Indeed - the test suite found that :)

@josephwright
Copy link
Member

Thinking again, we do need two lots of the negative skip as one is just undoing the one that TeX's added - but clearly something is wrong as @FrankMittelbach says.

@FrankMittelbach
Copy link
Member

yes, the code doc could be a little clearer, saying that the first cancels the belowskip added by TeX and the second then adds the real belowskip which is the negation to what TeX has seen. The problem is in how the endpe handling works (or doesn't work) and I think unrelated to the belowskip business.

@giovanni111
Copy link
Author

Side note: To get the same output for my document without and with the math module, my hot fix is:

\cs_set_protected:Npn \__math_tag_dollardollar_display_end:
  {
    %  \typeout{== tag dollarldollar display end}
    %  \ShowTagging{struct-stack}
    \para_raw_end:
    \tagpdfparaOn
    \l__math_tmpa_skip \lastskip
    \tag_socket_use:n{math/display/formula/end}
    \nobreak
    \skip_vertical:n { -\l__math_tmpa_skip -\parskip } % remove the negative belowdisplayskip
    \penalty \postdisplaypenalty
    \skip_vertical:n { -\l__math_tmpa_skip  } % insert the correct skip
  \@doendpe             % this has no \end{...} to take care of it
}

@u-fischer
Copy link
Member

@giovanni111 simply removing \parskip does not work, the spacing is then wrong if there is an empty line after the math:

\DocumentMetadata{
  testphase   = {
    math % adds \parskip below display formulas
  }}
\documentclass{article}
\parskip\baselineskip

\ExplSyntaxOn\makeatletter
\cs_set_protected:Npn \__math_tag_dollardollar_display_end: 
  {
    \para_raw_end:
    \tagpdfparaOn
    \l__math_tmpa_skip \lastskip
    \tag_socket_use:n{math/display/formula/end}    
    \penalty 10000
    \skip_vertical:n { -\l__math_tmpa_skip-\parskip} % negate the negative belowdisplayskip
    \penalty \postdisplaypenalty
    \skip_vertical:n { -\l__math_tmpa_skip  } % reinsert it 
  \@doendpe             % this has no \end{...} to take care of it
}
\ExplSyntaxOff\makeatother
\usepackage{amsmath}
\begin{document}
Some Text
$$A+B=C$$

More Text

Some Text
$$A+B=C$$
More Text
\end{document}

with math module:

image

without math module

image

@FrankMittelbach
Copy link
Member

This is tricky, the correct solution might be something along the following lines:

\ExplSyntaxOn
\makeatletter
\cs_set_protected:Npn \__math_tag_dollardollar_display_end:
  {
    %  \typeout{== tag dollarldollar display end}
    %  \ShowTagging{struct-stack}
    \para_raw_end:
    \tagpdfparaOn
    \l__math_tmpa_skip \lastskip
    \tag_socket_use:n{math/display/formula/end}
    \nobreak
    \skip_vertical:n { -\l__math_tmpa_skip } % remove the negative belowdisplayskip
    \penalty \postdisplaypenalty
    \skip_vertical:n { -\l__math_tmpa_skip } % insert the correct skip
  \@doendpe             % this has no \end{...} to take care of it
  {\parskip\z@\noindent}\ignorespaces
}
\makeatother
\ExplSyntaxOff

Basically, after $$ TeX returns to hmode, but we have to insert the formula sockets so need to go into vmode. So we have to get back to hmode but in a way that a) no no-zero parskip is added and b) that the hlist is totally empty (to not generate another line if there is a \par immediately following.

and I guess the @doendpe is basically nonsense (unless the code is used in other places as well).

@FrankMittelbach
Copy link
Member

ok, looks like \@doendpeis needed but not because it does anything useful (it is basically ending when \noindentis seen) but it seems to be required to get the paragraph tagging structures right ... which has open questions anyway, see note in the code "why is \tagpdfOn needed here?"

So I guess this is something for you @u-fischer to take a look at.

@u-fischer
Copy link
Member

@FrankMittelbach well I think the \tagpdfparaOn is not needed but a \tagpdfparaOff is missing for the $$..$$ which leads to the same issue as in #711 this should be fixed but it unrelated to the issue here.

The problem here is the literal inserted when pdftex is used. Literals inserts spacing, and the code tries to avoid that by using \para_raw_end:\@doendpe but sadly this fails with a non-zero parskip (also if as with luatex there is no literal).

Can't you come up with a \math@doenpe which removes the parskip if there is no empty line? The only other alternative that I see would be to try to move the literal before the $$ so that it is inserted inside the math but I don't know if that affects spacing in math ...

\documentclass{article}
\ExplSyntaxOn\makeatletter
\exp_args:No\tex_everydisplay:D
  {
   \tex_the:D \tex_everydisplay:D
   \group_insert_after:N \__math_tag_dollardollar_display_end:
   \__math_grab_dollardollar:w
  }
  
\cs_new_protected:Npn \__math_grab_dollardollar:w % $$
  #1 $$ {#1 $$}

\cs_new_protected:Npn \__math_tag_dollardollar_display_end:{}
\ExplSyntaxOff
\usepackage{amsmath}

%\parskip=1cm
\begin{document}
\section*{normal}

Some Text
$$ x=1 $$

More Text

Some Text
$$ x=1 $$
more Text

\section*{normal with parskip}

{\parskip=1cm

Some Text
$$ x=1 $$

More Text

Some Text
$$ x=1 $$
more Text

}
\ExplSyntaxOn
\cs_set_protected:Npn \__math_tag_dollardollar_display_end:
  {\pdfliteral{}}
\ExplSyntaxOff

\section*{with literal}

Some Text
$$ x=1 $$

More Text (too much space)

Some Text
$$ x=1 $$
more Text

\ExplSyntaxOn\makeatletter
\cs_set_protected:Npn \__math_tag_dollardollar_display_end:
  {\para_raw_end:\pdfliteral{}}
\ExplSyntaxOff
\newpage 
\section*{literal + para}

Some Text
$$ x=1 $$

More Text

Some Text
$$ x=1 $$
more Text (wrong indentation)

\ExplSyntaxOn\makeatletter
\cs_set_protected:Npn \__math_tag_dollardollar_display_end:
  {\para_raw_end:\pdfliteral{}\@doendpe}
\ExplSyntaxOff

\section*{literal + para + doendpe}

Some Text
$$ x=1 $$

More Text

Some Text
$$ x=1 $$
more Text 

\section*{literal + para + doendpe + parskip}

\parskip=1cm

Some Text
$$ x=1 $$

More Text

Some Text
$$ x=1 $$
more Text (too much space, issue)

\ExplSyntaxOn\makeatletter
\cs_set_protected:Npn \__math_tag_dollardollar_display_end:
  {\para_raw_end:\@doendpe}
\ExplSyntaxOff

\section*{para + doendpe + parskip}

\parskip=1cm

Some Text
$$ x=1 $$

More Text

Some Text
$$ x=1 $$
more Text (too much space, issue)

\end{document}

@FrankMittelbach FrankMittelbach added fixed in branch fixed in a branch different to develop and removed analyse something that needs further checking and perhaps code changes labels Dec 1, 2024
@u-fischer u-fischer added fixed in release issue is fixed and will be deployed in the next release of package or kernel and removed fixed in branch fixed in a branch different to develop labels Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: math bug Something isn't working in code we maintain (directly) fixed in release issue is fixed and will be deployed in the next release of package or kernel
Projects
None yet
Development

No branches or pull requests

4 participants