latex3 / tagging-project

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

listings not supported #70

Open domix75 opened 7 months ago

domix75 commented 7 months ago

Hello thanks a lot for this work

I have a small issue, I try to use tagged pdf with a file including listings. But there is a compile error

% !Mode:: "TeX:UTF-8:Main"
\DocumentMetadata{
 testphase={
     phase-III,
%     math,table
     },
 pdfversion=2.0,
 pdfstandard=ua-2,
}
%%\tagpdfsetup{math/mathml/write-dummy}
\tagpdfsetup{listings}
\documentclass{article}

\usepackage{amsmath}
\usepackage{listings}

\title{Listing Examples}
\author{Dom}
\date{\today}

\begin{document}

\section{Python code}

%\begin{lstlisting}[language=python,frame=single,numbers=left,basicstyle=\footnotesize]
\begin{lstlisting}[language=python]
import os

os.listdir()
\end{lstlisting}

\end{document}

compiled with lualatex-dev, (This is LuaHBTeX, Version 1.18.0 (TeX Live 2024)/Development id: 7611), this produces :

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

br

u-fischer commented 7 months ago

listings is a complicated package and not yet supported ;-(. You can try the code I use in the tagpdf documentation. There I defined a new environment. But it is not much tested, e.g. I don't know what happens e.g. with line numbers

\DocumentMetadata{
 testphase={
     phase-III,
%     math,table
     },
 pdfversion=2.0,
 pdfstandard=ua-2,
}
%%\tagpdfsetup{math/mathml/write-dummy}
\documentclass{article}

\usepackage{amsmath}
\usepackage{listings}

\title{Listing Examples}
\author{Dom}
\date{\today}
\makeatletter
\ExplSyntaxOn
\DeclareInstance{blockenv}{lstlisting}{display}
{
  env-name       = lstlisting,
  tag-name       = verbatim,
  tag-class      = ,
  tagging-recipe = standard,
  inner-level-counter  = ,
  level-increase = false,
  setup-code     = ,
  block-instance = displayblock ,
  inner-instance = ,
  final-code     =  \tl_set:Nn \l__tag_para_main_tag_tl {codeline}\tagtool{paratag=Code},
}
\ExplSyntaxOff
\lstnewenvironment{taglstlisting}[2][]{%
     \UseInstance{blockenv}{lstlisting} {}
     \lst@TestEOLChar{#2}%
     \lstset{#1}%
     \csname\@lst @SetFirstNumber\endcsname%
   }{%
     \@nobreakfalse
     \csname\@lst @SaveFirstNumber\endcsname%
     \endblockenv
   }
\makeatother

\begin{document}

\section{Python code}

%\begin{lstlisting}[language=python,frame=single,numbers=left,basicstyle=\footnotesize]
\begin{taglstlisting}[language=python]
import os

os.listdir()
\end{taglstlisting}

\end{document}

It is not fully clear yet how verbatim should be tagged at best and this can change in future. Also be aware that to our knowledge no pdf viewer preserve all spaces in verbatim, so copy&paste of python code is not reliable.

u-fischer commented 7 months ago

This is not an issue with tagpdf itself, so I will transfer the issue to the tagging project.

domix75 commented 7 months ago

You point an interesting issue here, thank you. how should it be tagged ? Did not really think about it initially since I was first trying to have it compiling (could be with a warning saying somth about the tagging not there/not defined) I'd say at least the language and then the content as text, numbering probably tagged (as page numbers are, would be interesting to do it). but this is quick first thought, need more work - not saying it can/should be fixed soon. but at least on the roadmap somewhere

FrankMittelbach commented 7 months ago

well it is on the roadmap :-). Concerning how such things should be tagged ... in many cases that is wide open. In https://github.com/latex3/tagging-project/discussions/7 I made a first stab at a possible tagging structure for verbatim blocks (like listings are) but that is for from being the last word on the topic.