[latex3-commits] [latex3/latex2e] latex-lab/uf-review-05: handle \mathchoice in \text (27d7bc8b)
github at latex-project.org
github at latex-project.org
Wed Jan 17 20:01:03 CET 2024
Repository : https://github.com/latex3/latex2e
On branch : latex-lab/uf-review-05
Link : https://github.com/latex3/latex2e/commit/27d7bc8b18e815ce869c14d41b55a7a75dfcfee5
>---------------------------------------------------------------
commit 27d7bc8b18e815ce869c14d41b55a7a75dfcfee5
Author: Ulrike Fischer <fischer at troubleshooting-tex.de>
Date: Wed Jan 17 20:01:03 2024 +0100
handle \mathchoice in \text
>---------------------------------------------------------------
27d7bc8b18e815ce869c14d41b55a7a75dfcfee5
required/latex-lab/changes.txt | 4 ++
required/latex-lab/latex-lab-amsmath.dtx | 74 +++++++++++++++++++++++++++++++-
required/latex-lab/latex-lab-math.dtx | 33 ++++++++++++++
3 files changed, 110 insertions(+), 1 deletion(-)
diff --git a/required/latex-lab/changes.txt b/required/latex-lab/changes.txt
index 11f00b2d..5c19494b 100644
--- a/required/latex-lab/changes.txt
+++ b/required/latex-lab/changes.txt
@@ -1,3 +1,7 @@
+2024-01-17 Ulrike Fischer <Ulrike.Fischer at latex-project.org>
+ * latex-lab-math.dtx, latex-lab-amsmath.dtx, redefine \text to
+ avoid faulty structure and faulty PDF from \mathchoice.
+
2024-01-14 Ulrike Fischer <Ulrike.Fischer at latex-project.org>
* latex-lab-float.dtx, correct destination, see tagging issue #55
diff --git a/required/latex-lab/latex-lab-amsmath.dtx b/required/latex-lab/latex-lab-amsmath.dtx
index d2477382..5dcfa46d 100644
--- a/required/latex-lab/latex-lab-amsmath.dtx
+++ b/required/latex-lab/latex-lab-amsmath.dtx
@@ -2,7 +2,7 @@
%
%% File: latex-lab-amsmath.dtx
%
-% Copyright (C) 2022,2023 The LaTeX Project
+% Copyright (C) 2022-2024 The LaTeX Project
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
@@ -127,6 +127,78 @@
}
% \end{macrocode}
%
+% \subsection{\cs{text}}
+%
+% The \cs{text} command uses \cs{mathchoice} which \enquote{typesets} the argument
+% four times. This makes it quite problematic for tagging. Without precautions
+% structure objects would be created four times and would get MC-chunks as kids
+% that doesn't really exist. amsmath contains a switch that allows to execute code
+% only in the first (displaymath) branch, but that isn't usable here. At first because
+% we don't know if the first branch creates the same structure as the one that is
+% actually used. At second because the engines executes some commands like \cs{label}
+% and \cs{pdfannot} only at shipout from the branch that really was used. So we would
+% get structure data from one \cs{mathchoice}-branch and MC-labels and links from another
+% one and that gets very messy.
+%
+% We therefore have to avoid that tagging is active in unused branches. In pdflatex it
+% is not possible to detect the mathstyle before, so we use a label. With lualatex is
+% is possible to redefine \cs{text} not to use \cs{mathchoice}
+%
+% \begin{macrocode}
+\AddToHook{package/amstext/after}
+ {
+% \end{macrocode}
+% currently amsmath is loaded in a begindocument hook, so this
+% test is fine. If amstext is loaded earlier (in the kernel), this needs perhaps a change.
+% \begin{macrocode}
+ \tag_if_active:T
+ {
+ \sys_if_engine_luatex:TF
+ {
+ \def\text@#1{{%
+ \ifcase\mathstyle
+ \hbox{{#1}}\or
+ \hbox{{#1}}\or
+ \hbox{{#1}}\or
+ \hbox{{#1}}\or
+ \hbox{{\let\f at size\sf at size\selectfont#1}}\or
+ \hbox{{\let\f at size\sf at size\selectfont#1}}\or
+ \hbox{{\let\f at size\ssf at size\selectfont#1}}\or
+ \hbox{{\let\f at size\ssf at size\selectfont#1}}\or
+ \ERROR
+ \fi
+ \check at mathfonts
+ }}
+ }
+ {
+ \def\text@#1
+ {{
+ \int_gincr:N\g__math_mathchoice_int
+ \tag_stop:
+ \mathchoice
+ {
+ \@@_tag_if_mathstyle:en{mathchoice-\int_use:N\g__math_mathchoice_int}{0}
+ \textdef@\displaystyle\f at size{#1}
+ }
+ {
+ \@@_tag_if_mathstyle:en{mathchoice-\int_use:N\g__math_mathchoice_int}{2}
+ \textdef@\textstyle\f at size{\firstchoice at false #1}
+ }
+ {
+ \@@_tag_if_mathstyle:en{mathchoice-\int_use:N\g__math_mathchoice_int}{4}
+ \textdef@\textstyle\sf at size{\firstchoice at false #1}
+ }
+ {
+ \@@_tag_if_mathstyle:en{mathchoice-\int_use:N\g__math_mathchoice_int}{6}
+ \textdef@\textstyle \ssf at size{\firstchoice at false #1}
+ }
+ \check at mathfonts
+ }}
+ }
+ }
+ }
+% \end{macrocode}
+%
% \begin{macrocode}
\ExplSyntaxOff
% \end{macrocode}
diff --git a/required/latex-lab/latex-lab-math.dtx b/required/latex-lab/latex-lab-math.dtx
index a2d4d438..16837a36 100644
--- a/required/latex-lab/latex-lab-math.dtx
+++ b/required/latex-lab/latex-lab-math.dtx
@@ -641,6 +641,39 @@
% \end{macrocode}
% \end{macro}
%
+% \subsection{Mathstyle detection}
+% In some cases we need to detect the mathstyle used in a \cs{mathchoice}
+% command and to disable/enable tagging in the unused branches.
+% This is currently only used in the amstext command \cs{text}
+% but is perhaps also needed in other cases, so we create a general command.
+%
+%\begin{macro}{\l_@@_mathstyle_int,\g_@@_mathchoice_int,mathstyle}
+% \begin{macrocode}
+\int_new:N \l_@@_mathstyle_int
+\int_new:N \g_@@_mathchoice_int
+\property_new:nnnn{mathstyle}{now}{-1}{\int_use:N \l_@@_mathstyle_int }
+% \end{macrocode}
+%\end{macro}
+% For now internal, but perhaps will need a public version.
+% The command should be used in every branch of a \cs{mathchoice}
+% (with the correct mathstyle number) and with an unique label (which should
+% be the same in every branch).
+% \cs{g_@@_mathchoice_int} can be e.g. increased before the mathchoice and
+% then used.
+% \begin{macro}{\@@_tag_if_mathstyle:nn}
+% \begin{macrocode}
+\cs_new_protected:Npn \@@_tag_if_mathstyle:nn #1 #2
+ %#1 refers to label
+ %#2 is a number for the mathstyle (typically 0,2,4,6)
+ {
+ \int_set:Nn \l_@@_mathstyle_int {#2}
+ \property_record:nn {#1} { mathstyle }
+ \int_compare:nNnTF { \property_ref:nn {#1}{ mathstyle} } = { #2 }
+ { \tag_start: }{ \tag_stop: }
+ }
+\cs_generate_variant:Nn \@@_tag_if_mathstyle:nn {en}
+% \end{macrocode}
+% \end{macro}
%
% \subsection{Tagging options}
% \begin{macrocode}
More information about the latex3-commits
mailing list.