# Incorrect actual text in alignat environment with expl3

I want to redefine alignat environment to automatically do tagging of the formulas and insert as actual text of the tag P this formulas. It works ok,but why if i switch on expl3 syntax I get actual text xs_StrFindGrou. The second question, how I can remove all extra commands to set formula as actual text of paragraph without using xstring package but using only expl3 features? Thanks everybody for the help.

\documentclass{article}
\usepackage{tagpdf,amsmath,xstring}
\tagpdfsetup{tabsorder=structure,uncompress,activate-all,interwordspace=true,tagunmarked=false}
\tagpdfifpdftexT
{
\pdfcompresslevel=0
%set language / can also be done with hyperref
\pdfcatalog{/Lang (en-US)}
\usepackage[T1]{fontenc}
\input glyphtounicode
\pdfgentounicode=1
}
\tagpdfifluatexT
{
%set language / can also be done with hyperref
\pdfextension catalog{/Lang (en-US)}
\usepackage{fontspec}
\newfontface\zerowidthfont{freeserif}
}
\pagestyle{empty}
\ExplSyntaxOn
\makeatletter
\long\def\doactualtext#1{
\def\@mltext{\detokenize\expandafter{#1}}
\def\@mltexttmp{}
\StrBehind[5]{\@mltext}{ }[\@mltexttmp]
\StrGobbleRight{\@mltexttmp}{1}[\@mltext]
\tagstructbegin{tag=P,actualtext-o=\detokenize\expandafter{\@mltext}}
\tagmcbegin{tag=P}
#1
\tagmcend
\tagstructend
}
\renewenvironment{alignat}{
\collect@body\doactualtext\space
\start@align
\z@\st@rredfalse
}{
\endalign
}
\makeatother
\ExplSyntaxOn
\begin{document}
\tagstructbegin{tag=Document}
\begin{alignat}
10xy^2+15x^2y-5xy7 & =  5\left(2xy^2+3x^2y-xy7\right) = \\
& = 5x\left(2y^2+3xy-y7\right) = \\
& = 5xy\left(2y+3x-7\right)
\end{alignat}
%\tagstructend Why i get an error,that there is no structure on the stack?
\end{document}

• What's the purpose of loading expl3 when you use none of its facilities? Also, shouldn't the second \ExplSyntaxOn be \ExplSyntaxOff? Dec 4 '19 at 21:11
• What do you expect \@mltext to contain? Dec 4 '19 at 21:19

You get an error from the \tagstructend as the part after the #1 in your \doactualtext is processed twice by the amsmath commands. You can use \ifmeasuring@ to suppress this during the measuring step.

You get the wrong alttext as you didn't replace the space by the tilde.

\documentclass{article}
\usepackage{tagpdf,amsmath,xstring}
\tagpdfsetup{tabsorder=structure,uncompress,activate-all,interwordspace=true,tagunmarked=false}
\tagpdfifpdftexT
{
\pdfcompresslevel=0
%set language / can also be done with hyperref
\pdfcatalog{/Lang (en-US)}
\usepackage[T1]{fontenc}
\input glyphtounicode
\pdfgentounicode=1
}
\tagpdfifluatexT
{
%set language / can also be done with hyperref
\pdfextension catalog{/Lang (en-US)}
\usepackage{fontspec}
\newfontface\zerowidthfont{freeserif}
}
\pagestyle{empty}
\ExplSyntaxOn
\makeatletter
\long\def\doactualtext#1{
\def\@mltext{\detokenize\expandafter{#1}}
\def\@mltexttmp{}
\StrBehind[5]{\@mltext}{~}[\@mltexttmp]
\StrGobbleRight{\@mltexttmp}{1}[\@mltext]
\tagstructbegin{tag=P,actualtext-o=\detokenize\expandafter{\@mltext}}
\tagmcbegin{tag=P}
#1
\ifmeasuring@\else
\tagmcend
\tagstructend
\fi
}
\renewenvironment{alignat}{
\collect@body\doactualtext\space
\start@align
\z@\st@rredfalse
}{
\endalign
}
\makeatother
\ExplSyntaxOn
\begin{document}
\tagstructbegin{tag=Document}
\begin{alignat}
10xy^2+15x^2y-5xy7 & =  5\left(2xy^2+3x^2y-xy7\right) = \\
& = 5x\left(2y^2+3xy-y7\right) = \\
& = 5xy\left(2y+3x-7\right)
\end{alignat}
\tagstructend
\end{document}


I have no idea what your replacements are supposed to do.

• Thank you very much,now my code works ok. As for replacement xstring package by expl3,i will explain now. If we will use \detokenize\expandafter{#1} as actualtext we will get in pdf \start@align \z@ \st@rredfalse 10xy^2+15x^2y-5xy7&=5\left (2xy^2+3x^2y-xy7\right )=\\&=5x\left (2y^2+3xy-y7\right )=\\&=5xy\left (2y+3x-7\right ). To avoid it,we use StrBehind command of xstring package,which split line by space character 5 times. But i want to use expl3 features for this instead of xstring package. I know about seq_set_split function for this,but i can't set number of splittings. Dec 5 '19 at 0:23
• For example if i want to split line one two three four by space two times,i expect get sequence {one}{two}{three four},and use the last item of this sequence,but expl3 function of sequence split allows me only get sequence {one}{two}{three}{four}. It very interesting for me,why amsmath package execute part of command after #1 twice? Also it not understandable for me,why in structure tree i get several mc elements? Thank you very much for your help and for answers on my questions. Dec 5 '19 at 0:31
• I did understand that you want to get rid of startling code but you method is odd and at least on my system it took away to much. Don't ask new questions in a comment. Dec 5 '19 at 6:46
• Ok,and how you propose improve my method,using expl3? If i will create a new question,it will be almost about the same,so it will be mark as douplicate by other users of this website. Dec 5 '19 at 10:45