latex3 / tagging-project

Issues related to the tagging project
https://latex3.github.io/tagging-project/
LaTeX Project Public License v1.3c
23 stars 5 forks source link

math environments cause error if placed in group #79

Open mbertucci47 opened 1 month ago

mbertucci47 commented 1 month ago

If a math environment is placed in a braced group, it produces an error with the math tagging code. Compiling

\DocumentMetadata{testphase={phase-III,math}}
\documentclass{article}

\begin{document}

{\begin{equation}
A
\end{equation}}
Some text
\end{document}

with lualatex-dev produces

! Package tagpdf Error: The number of automatic begin (1) and end (2)
(tagpdf)                text-unit para hooks differ!

If the Some text after is removed, the error goes away.

I noticed this because, out of curiosity, I wanted to see if testmath.tex worked with the tagging code. With a few modifications due to the lack of amsthm support, it does, except for two instances of math environments placed in a group. Modified version below with those environments commented out.

testmath-test.txt

For reference, the commented lines are

{\makeatletter\ctagsplit@true
\begin{equation}
\begin{split}
 \abs{I_2}&=\left\lvert \int_{0}^T \psi(t)\left\{u(a,t)-\int_{\gamma(t)}^a
  \frac{d\theta}{k(\theta,t)}
  \int_{a}^\theta c(\xi)u_t(\xi,t)\,d\xi\right\}dt\right\rvert\\
&\le C_6\left\lvert \left\lvert f\int_\Omega\left\lvert \wt{S}^{-1,0}_{a,-}
  W_2(\Omega,\Gamma_l)\right\rvert\right\rvert
  \left\lvert \abs{u}\overset{\circ}\to W_2^{\wt{A}}
  (\Omega;\Gamma_r,T)\right\rvert\right\rvert.
\end{split}
\end{equation}}%

and

{\delimiterfactor750
\begin{align}
\begin{split}\abs{I_1}
  &=\left\lvert \int_\Omega gRu\,d\Omega\right\rvert\\
&\le C_3\left[\int_\Omega\left(\int_{a}^x
  g(\xi,t)\,d\xi\right)^2d\Omega\right]^{1/2}\\
&\quad\times \left[\int_\Omega\left\{u^2_x+\frac{1}{k}
  \left(\int_{a}^x cu_t\,d\xi\right)^2\right\}
  c\Omega\right]^{1/2}\\
&\le C_4\left\lvert \left\lvert f\left\lvert \wt{S}^{-1,0}_{a,-}
  W_2(\Omega,\Gamma_l)\right\rvert\right\rvert
  \left\lvert \abs{u}\overset{\circ}\to W_2^{\wt{A}}
  (\Omega;\Gamma_r,T)\right\rvert\right\rvert.
\end{split}\label{eq:A}\\
\begin{split}\abs{I_2}&=\left\lvert \int_{0}^T \psi(t)\left\{u(a,t)
  -\int_{\gamma(t)}^a\frac{d\theta}{k(\theta,t)}
  \int_{a}^\theta c(\xi)u_t(\xi,t)\,d\xi\right\}dt\right\rvert\\
&\le C_6\left\lvert \left\lvert f\int_\Omega
 \left\lvert \wt{S}^{-1,0}_{a,-}
  W_2(\Omega,\Gamma_l)\right\rvert\right\rvert
  \left\lvert \abs{u}\overset{\circ}\to W_2^{\wt{A}}
  (\Omega;\Gamma_r,T)\right\rvert\right\rvert.
\end{split}
\end{align}}%
u-fischer commented 1 month ago

Yes, bracing environments is problematic with the tagging code. It hides the \@doendpe which is important in various places. See also https://github.com/latex3/tagging-project/issues/48 and https://github.com/latex3/tagging-project/issues/18.

FrankMittelbach commented 1 month ago

Needs some verification (and perhaps some further thoughts, but I think the following would resolve the problem generally:

\DocumentMetadata{testphase={phase-III,math}}
\documentclass{article}

\makeatletter

\def\@endpetrue {%
   \global\let\if@endpe\iftrue
% if called inside a group propagate to the outside   
   \ifnum\currentgrouplevel>\z@
      \aftergroup\propagate@doendpe
   \fi
}

% if \if@endpe is still true run \@doendpe
% that in turn runs another \@endpetrue (besides other things), thus propagating further, if necessary
% However, if the endpe situation got resolved nothing further happens
\def\propagate@doendpe{%
  \if@endpe
    \typeout{=> run (another) @doendpe at group level \the\currentgrouplevel}%
    \@doendpe
  \else
    \typeout{=> do not run (another) @doendpe}%
  \fi
}

% with the above approach we do not  need the propagation in \end... any longer 
% (since this is now handled by \aftergroup\propagate@doendpe) so here a simple \endgroup is enough
\@namedef{end }#1{%
  \romannumeral
    \IfHookEmptyTF{env/#1/end}%
        {\expandafter\z@}%
        {\z@\UseHook{env/#1/end}}%
    \csname end#1\endcsname\@checkend{#1}%
%    \expandafter\endgroup\if@endpe\@doendpe\fi
    \endgroup
    \UseHook{env/#1/after}%
    \if@ignore\@ignorefalse\ignorespaces\fi
}

\makeatother

\begin{document}

{\begin{equation}
A
\end{equation}}
Some text

{Braces set in different place
\begin{equation}
B
\end{equation}
Some text
}

\end{document}
mbertucci47 commented 1 month ago

I haven't had a chance to debug, but naively adding the changed code to the testmath-test file attached above produces errors, even without uncommenting the braced environments

mbertucci47 commented 1 month ago

It seems the changed code doesn't work with thebibliography.

\DocumentMetadata{testphase={phase-III,math}}
\documentclass{article}

\makeatletter

\def\@endpetrue {%
   \global\let\if@endpe\iftrue
% if called inside a group propagate to the outside   
   \ifnum\currentgrouplevel>\z@
      \aftergroup\propagate@doendpe
   \fi
}

% if \if@endpe is still true run \@doendpe
% that in turn runs another \@endpetrue (besides other things), thus propagating further, if necessary
% However, if the endpe situation got resolved nothing further happens
\def\propagate@doendpe{%
  \if@endpe
    \typeout{=> run (another) @doendpe at group level \the\currentgrouplevel}%
    \@doendpe
  \else
    \typeout{=> do not run (another) @doendpe}%
  \fi
}

% with the above approach we do not  need the propagation in \end... any longer 
% (since this is now handled by \aftergroup\propagate@doendpe) so here a simple \endgroup is enough
\@namedef{end }#1{%
  \romannumeral
    \IfHookEmptyTF{env/#1/end}%
        {\expandafter\z@}%
        {\z@\UseHook{env/#1/end}}%
    \csname end#1\endcsname\@checkend{#1}%
%    \expandafter\endgroup\if@endpe\@doendpe\fi
    \endgroup
    \UseHook{env/#1/after}%
    \if@ignore\@ignorefalse\ignorespaces\fi
}

\makeatother

\begin{document}

\begin{equation}
\sum
\end{equation}

\begin{thebibliography}{9}
\bibitem{dihe:newdir}
W.~Diffie and E.~Hellman, \emph{New directions in cryptography}, IEEE
Transactions on Information Theory \textbf{22} (1976), no.~5, 644--654.
\end{thebibliography}

\end{document}

produces the error

! Package tagpdf Error: The number of automatic begin (3) and end (2)
(tagpdf)                text-unit para hooks differ!

Getting rid of either the equation or thebibliography environment removes the error.

FrankMittelbach commented 1 month ago

I missed out one necessary adjustments involving lists:

\ExplSyntaxOn
\socket_set_plug:nnn{tagsupport/block-endpe}{on} 
                   { \@endpetrue }
\socket_set_plug:nnn{tagsupport/block-endpe}{off}
                    { \@endpefalse }
\ExplSyntaxOff

After that it should work.