latex3 / tagging-project

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

Reading backslash display-math/inline-math with pdfmanagement and lualatex #14

Open balakrishnan1978 opened 1 year ago

balakrishnan1978 commented 1 year ago

I have using TeXLive 2023 with LuaLaTeX Version 1.17.0. Created Accessibility PDF file successfully, but when opening the PDF file through Acrobat Reader PDF file is reading backslash in all the Math Mode. How to deactivate this? I have need to read only Math Content.

My MWE is:

\DocumentMetadata{testphase={phase-III,math},pdfstandard=UA-1,pdfversion=2.0,debug=pdfmanagement,debug={xmp-export=test,uncompress}} \documentclass{article}

\usepackage{fontspec} \usepackage{unicode-math} \setmainfont{STIX Two Text} \setmathfont{STIX Two Math} \begin{document}

In order to learn a bilingual mapping that connects two embeddings into one space, the mapping matrix $W{X}$ makes $Y{t} = W{X} X{s}$, we employ an adversarial training to learn the mapping. In particular, the generator $G$ tries to make a mapping matrix to confuse the discriminator. In order to get orthogonal parameterization, we note that transforming the source word embedding into the target, its transpose should also transform the target to the source. The $G$ tries to learn a mapping $W{X}$ to maximize a source representation $W{X} X{s}$ to mapping into a target embedding $Y{t}$. The discriminator D is a binary classifier which aims to enhance its ability to distinguish $Y{t}$ and $W{X} X{s}$. To achieve this goal, we maximize the ability of the discriminator and minimize the probability of generator.\vspace*{-3pt} \begin{equation} score{L{s},L{t}} = ∑{i=1}^{max length(L{s},L{t})} ± CSCL(x{s}^{i},y_{t}^{j}) \end{equation} \end{document}

In this equation is not reading proberly. I have checked with Adobe Acrobat Reader Version 23.006.20320.0.

FrankMittelbach commented 1 year ago

As documented tagging of math is nowhere near its final state. At the moment all it does is to capture the math source data and presents it as "LaTeX source" to the reader in Alt text. What a reader does with that very much depends on the reader abilities. Acrobat, for example, seems to ignore many characters and only reads what it considers "English text" which is not very useful. Other readers (e.g. Voice Over) do differently but neither do work really well with that approach. Eventually, we expect to be able to produce mathml instead (though probably only with LuaTeX, which is why we also explore other options) but we aren't there yet.

You mention that "Acrobat is reading backslash". Now sure what you mean by that. It is, as far as I can see, not reading the backslash which is in parts why the output is in fact not really comprehensible even if you are used to LaTeX source data.

davidcarlisle commented 1 year ago

@FrankMittelbach the default acrobat reader reading does seem to read the backslash (at least in English), this is surprising to the OP here especially in inline math where $G$ reads as backslash begin math G backslash end math as $ gets normalised to \begin{math} during the internal processing. That is: it reads backslashes that are not explicitly there.

I agree though that this is all experimental code so not really a bug, more a test example for future work.

In the original version at stackexchange I suggested a regex (expensive but OK for testing) to simplify the AltText before tagging (actually the version there also simplifies the associated file version which is probably less good as it makes it invalid latex, but acrobat doesn't use it at all so that isn't handled in my tex.sx answer)

https://tex.stackexchange.com/a/697308/1090

balakrishnan1978 commented 1 year ago

@davidcarlisle: Thanks for your answer. But it's worked only for inline math and not working in display math.

\DocumentMetadata{testphase={phase-III,math},pdfstandard=UA-1,pdfversion=1.7,uncompress}
\documentclass{article}
\usepackage{pdfcomment}% http://ctan.org/pkg/pdfcomment
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{mathtools}% http://ctan.org/pkg/mathtools
\usepackage{fontspec}
\usepackage{unicode-math}
\setmainfont{STIX Two Text}
\setmathfont{STIX Two Math}
\hypersetup{
pdflang={en-US},
pdftitle={Making Accessible Documents Using LuaLaTeX},
pdfauthor={David Carlisle},
pdfsubject={Tagged PDF, Accessibilityo in LuaLaTeX},
pdfkeywords={Accessibility, LuaLaTeX}
}
\begin{document}
\ExplSyntaxOn
\makeatletter

\def\@kernel@math@begin {
  \regex_replace_all:nnN{\_}{~sub~}\g__math_grabbed_math_tl
  \regex_replace_all:nnN{\^}{~sup~}\g__math_grabbed_math_tl
  \regex_replace_all:nnN{\\}{~}\g__math_grabbed_math_tl

  \tl_gset:Nx\tmpmathcontent
     { 
       \exp_not:V\g__math_grabbed_math_tl
       \space 
    }
  \tagstructbegin{tag=Formula,
    AFinline-o=\tmpmathcontent,
    title-o=\g__math_grabbed_env_tl,
    actualtext=\tmpmathcontent
  }
  \grabaformulapartandstart
}

\makeatother
\ExplSyntaxOff

This is the sample Inline Equations for $XYZ_{s}$ testing purpose only.

\begin{equation*}
\alpha + \beta = \gamma
\end{equation*}
\end{document}

How to do it will work both inline or any display math?

davidcarlisle commented 12 months ago

For the record I inproved the regex a bit in that tex.sx answer

\DocumentMetadata{testphase={phase-III,math},pdfstandard=UA-1,pdfversion=2.0,debug=pdfmanagement,debug={xmp-export=test},uncompress}

\documentclass{article}

\usepackage{fontspec}
\usepackage{unicode-math}
\setmainfont{STIX Two Text}
\setmathfont{STIX Two Math}
\begin{document}

\ExplSyntaxOn
\makeatletter

\def\@kernel@math@begin {
  % space after every token so $abc$" voiced as "a b c" not "ab-ca"
  % \mathrm{thing} voiced as thing
  \regex_replace_all:nnN{((\c{mathrm}|\c{mathit}|\c{text})\{(.*?)\}|(.))\ *}{\3\4\ }\g__math_grabbed_math_tl
  % detokenize so \alpha is 6 characters not one cs token
  \tl_gset:Nx\g__math_grabbed_math_tl{\tl_to_str:V\g__math_grabbed_math_tl}
  % voice _ as sub (space already addded above)
  \regex_replace_all:nnN{\_}{sub}\g__math_grabbed_math_tl
  % voice ^ as sup (space already addded above)
  \regex_replace_all:nnN{\^}{sup}\g__math_grabbed_math_tl
  % lose & (you might want to voice this alignment cell separator but...)
  \regex_replace_all:nnN{\&}{\ }\g__math_grabbed_math_tl
  % voice \cr (which should not have been there) or \\ as newline
  \regex_replace_all:nnN{\x{5c}cr|\x{5c}\x{5c}}{\ newline\ }\g__math_grabbed_math_tl
  % lose \
  \regex_replace_all:nnN{\x{5c}}{\ }\g__math_grabbed_math_tl
  % debug
  \typeout{\meaning\g__math_grabbed_math_tl}
  \tl_gset:Nx\tmpmathcontent
     { 
       \exp_not:V\g__math_grabbed_math_tl
       \space 
    }
  \tagstructbegin{tag=Formula,
    AFinline-o=\tmpmathcontent,
    title-o=\g__math_grabbed_env_tl,
    actualtext=\tmpmathcontent
  }
  \grabaformulapartandstart
}

\makeatother
\ExplSyntaxOff
$\mathrm{thing}+thing + \mathit{thing} + \text{otherthing}$ % voiced as: thing plus t h i n g plus plus thing plus other thing

In order to learn a bilingual mapping that connects two embeddings
into one space, the mapping matrix $W_{X}$ makes $Y_{t} = W_{X}
X_{s}$, we employ an adversarial training to learn the mapping. In
particular, the generator $G$ tries to make a mapping matrix to
confuse the discriminator. In order to get orthogonal
parameterization, we note that transforming the source word embedding
into the target, its transpose should also transform the target to the
source. The $G$ tries to learn a mapping $W_{X}$ to maximize a source
representation $W_{X} X_{s}$ to mapping into a target embedding
$Y_{t}$. The discriminator D is a binary classifier which aims to
enhance its ability to distinguish $Y_{t}$ and $W_{X} X_{s}$. To
achieve this goal, we maximize the ability of the discriminator and
minimize the probability of generator.\vspace*{-3pt}
\begin{equation}
\mathrm{score}_{L_{s},L_{t}} = ∑_{i=1}^{\mathrm{max\ length}(L_{s},L_{t})} ± CSCL(x_{s}^{i},y_{t}^{j})
\end{equation}

align

\begin{align*}
\alpha + \beta &= \gamma\\
  2. α + β &1= γ\\
 & {κ}_{j=1}\\ 
& K^{a+1}
\end{align*}

\end{document}

This logs the AltText to the terminal. eg the final align gets read as

alpha + beta = gamma newline 2 . α + β 1 = γ newline { κ } sub { j = 1 } newline K sup { a + 1 }

note acrobat read out loud can read the Greek , so there's no need to replace γ by gamma here (and conversely the csnames \alpha \beta \gamma could have been expanded to the characters rather than read as source, but that's not done here. Acrobat does not read most punctuation so {..} and (..) are not read, which can make it hard to distinguish subterms in larger expressions. (These could of course be replaced so (x,y) was read as open paren x comma y close paren or some such.