26

TUGboat, Volume 41 (2020), No. 1

Creating accessible pdfs with LATEX
Ulrike Fischer
Abstract
This article describes the current state and planned
actions to improve accessibility of pdfs created withLATEX, as currently undertaken by the LATEX Team.
1

Accessibility of pdf

The pdf language is at its core a page description
programming language. It describes very accurately
how text and graphical elements look and where they
are placed on a page. But it doesn’t describe thesemantic meaning of the elements and the reading
order; by looking at the code there is no way toknow if a text is a section heading or some watermark or a footnote or if it belongs to a tabular. You
can’t know where a sentence has its continuation on
another page or how many words a text contains,and sometimes it is even impossible to identify thecharacters; you only see some glyph index number
and copy & paste can give gibberish.
All this is not a problem as long as the pdf ismerely meant for printing or viewing but it restricts
its use for digital processing like copy & paste, automatic extraction of billing data, reflowing, or using
the pdf with a screen reader. For such uses you needaccessible, structured, extractable content.
“Accessibility” as a standard is specified in PDF/
UA. It is also included in other standards, notably
PDF/A-1a and PDF/A-2a (the “a” stands for accessible). The standards contain a number of requirements to help in retrieving the content for further
processing; for example, that every character has aUnicode representation (no gibberish when copy &pasting), that word spaces are correctly marked up(so that reflowing works), that the language of thedocument and the text is declared (so a screen reader
can guess the pronunciation), that pictures have sensible alternative descriptions. And most importantly:that the document is tagged. This last requirement
is responsible for adding structure information to thecontent. It marks up content as section or tabularcell or list item. This improves navigation in thedocument with, for example, a screen reader, butalso exporting to other formats like XML.
TUG maintains a web page with links to relevant
standards, articles and packages [3].

First published in ArsTEXnica 28 (Oct 2019), pp. 135–139.
Reprinted with permission.

Ulrike Fischer

2Creating a tagged pdf

Tagging consists of two main tasks. First, in thestream object of a page every bit of content must be
marked and labelled with a number MCID n.
The following listing shows a small example.
The BDC and the EMC lines are the start and end
markers needed for tagging. The /H1 indicates thatthe content is part of a sectioning element.
stream/H1 <<MCID 0>> BDCBT/F17 14.3462 Tf 124.802 706.129
Td [(0.1)-1100(Section)]TJETEMCIn the second step a number of pdf objects must
be created to describe the structure tree. Everyobject contains references to the parent /P and toone or more kid elements /K. The leaf nodes are theMCID n created in the first step. A typical object
looks roughly like this:
5 0 obj<</Type /StructElem/S /H1/P 4 0 R/K <</Type /MCR /MCID 0>>>>
endobjThis is a structure element of the type /H1 (andso a sectioning element) and it has one kid element,
the text of the section marked above.
Beside this a number of additional settings andobjects must be added to the pdf for cross-referencing
and “administration”.
3Changing LATEX

Measured in computer time, LATEX is quite old. LATEX
is not only a format; it was always meant to be extended by packages and classes, and over time, many
people have contributed to LATEX. It has a quite
large user base with very varied demands regarding
stability, features and development. LATEX is stillused with a variety of engines: pdfTEX, XETEX, LuaTEX, (u)pTEX and backends (dvips, dvipdfmx). One
could compare LATEX to an old city; lots of houses
built at different times in different styles by variouspeople, some modern, some older, some are in a goodstate, others are falling apart but nevertheless hometo someone.
This means that changing LATEX is not easy;
we can’t break lots of packages and old documentseven if the reward is accessible pdfs. And we have toconsider that documents must be compilable in TEXTUGboat, Volume 41 (2020), No. 1

27systems of varying age, for example when uploadingthem to a journal.
Thus, a very important aspect of the project isto develop a long term change strategy and manage
integration of core support across the LATEX universe.

latex-dev has not been created solely with tagging in mind, but it will help us to coordinate andtest changes with package and class authors, so it is
an important part of the project.

4When tagging a pdf one has to add a number ofsettings to pdf dictionaries which can be describedas “global resources”. As already mentioned in ananswer [2], LATEX has no interfaces for this:Unhappily, the LATEX format has oversleptPDF development quite entirely. Managing
global resources is the prime task for anOS, format in TEX speak. Because of themissing resource manager, both [tikzand transparent] packages do what mostpackages do, they think they are alone andadd their stuff to the resource, . . .
With tagging entering the scene it was clear thatsomething needed to be done to remedy this problem
and so the pdfresources project in the LATEX github
was created; it contains a (still quite experimental)
expl3-style interface which offers commands to add
contents to pdf resources in a controlled way. It alsooffers backend-independent interfaces to a number ofcore commands needed when writing objects to a pdf.
The package works with the main engines (pdfTEX,LuaTEX and XETEX) and backends (dvipdfmx and —more or less — dvips).
The main task for the next months is to test
the code, to integrate it into the kernel and to adapt
existing packages to use it. The number of packages
which should use the pdf resource manager is notvery large but includes important packages such as
hyperref, tikz, media9, pdfx.

First steps towards tagging

Tagging pdf with LATEX has been on the agenda
for quite some time. Babett Schalitz wrote a thesis
about it in 2007, and Ross Moore has given a numberof talks and articles at TUG conferences since then.
When I considered working on the topic some time
ago I got code from both, and decided rather quickly
that first some work on the basics was needed.
Tagging should, in my opinion, not be done bycreating a package that patches all sorts of commands in other packages; this is much too fragile. Itneeds proper support in the LATEX kernel and proper
support in the main classes and packages. I also
thought that to identify the needed support and totest implementations and interfaces, concrete code
was needed. So I wrote the package tagpdf. The
package offers core commands to tag a document andto activate some of the other requirements needed tomake a pdf accessible. The low-level code to markup a text as a section looks roughly like this:\tagstructbegin{tag=H1}\tagmcbegin{tag=H1}
Section\tagmcend\tagstructend

The \tagstructXX commands create the structure, while the \tagmcXX commands add the MCID
marks to the page stream.
At present, the tagpdf package works withpdfLATEX and LuaLATEX, and LuaLATEX gives thebest results, as one doesn’t have to worry about thebehaviour at page breaks. However, with the helpof the work on the pdfresources project described
below, it should be possible to extend it to otherengines and backends.
5LATEX-dev

Another important step towards accessible pdfs was
the implementation of the latex-dev format by theLATEX team and the maintainers of TEX Live andMiKTEX; latex-dev is a pre-release of LATEX fromthe development branch and made available throughCTAN. It allows users of a current TEX distribution to test their documents and code against anupcoming LATEX release by simply using their preferred latex with the suffix -dev; pdflatex-dev,lualatex-dev, xelatex-dev, etc. (For more information, see [1].)67

PDF resource management

Adapting the engines

Another open issue that emerged during the last year
was missing functionality in engines and backends.
For example pdfTEX was not ready for pdf 2.0; it
has no command to set a major pdf version. (Thiswill be remedied in TEX Live 2020.) As pdf 2.0 adds
important features needed for accessibility (the concept of associated files) this is clearly something thatshould be changed. It would be also useful if pdfTEX
could execute code at shipout time as can be donewith luatex with \latelua. The dvipdfmx backend
and dvips are missing additional color stacks.
8Adding hooks

As already shown in sections 2 and 4, tagging a pdf
requires adding quite a number of commands. Obviously, all the standard structures should if possible
add the needed code automatically. For this, hooks
Creating accessible pdfs with LATEX28

TUGboat, Volume 41 (2020), No. 1

are needed at the right places. The “right place”
has firstly a technical meaning; with the exceptionof LuaTEX, the tagging code inserts whatsits; thismeans it can change the output if used in the wrong
place (as sometimes anchors set by hyperref do).
But more importantly the “right place” meansthat we need to identify the owner of the code whichshould insert the tagging code. For example, sections
are generally created with \@startsection. So thiskernel command looks like a natural place to insert
hooks for tagging commands. On the other hand,chapters and parts have special commands createdby the classes. Does it make sense if the kernel handles the one part and the classes the other? Other
examples are bibliographies and glossaries; packages
like biblatex and glossaries look like the natural
owner here — and both packages already have lotsof hooks which make it easy to implement tagging —but both also use standard structures like lists ortabulars and additions to these generic environments
could clash with their needs.
This means that besides a pdf resource manager
we also need a hook management. And we needlots of real use cases and examples to be able toinvestigate the various dependencies.
9Mathematics

How to tag maths is still an open problem. There arequite a number of possibilities to make it accessible.
• One is to attach the LATEX source code, either as file or verbatim, with /ActualText tothe math structure. For a number of environments this can be automated quite well as theaxessibility package demonstrates (but it isdifficult for inline math input with $...$). Theusability with a screen reader is not bad — even
if not every word was correctly read aloud in mytests — but it requires that the user understands
LATEX input syntax, and with large equations
and complicated grouping it can be quite difficult to follow and to navigate through subequations. Usability can be improved if one invests
the time to manually split the math and add
explaining words.
• Another possibility is to mark all the maths bitswith MathML structure names. At least withLuaTEX this can probably be done more or lessautomatically — proofs of concept are the ConTEXt format and TEX4ht. But it is unknown
whether screen readers or other applications canactually use the information.
• A third possibility is to convert the equation
to MathML, for example with MathJax, andattach it as an associated file to the structure.
Ulrike Fischer

But here too it is unclear how such MathML
can be processed by the pdf consumer. It is also
unknown whether the presentation or content
flavour of MathML should be used in this case.
The pdf standard requires that glyphs and symbols are mapped to Unicode. Here too variants
are possible; for example, a could be mapped toU+1D44E (MathematicalR Italic Small A) or U+0061(Latin Small Letter A); could mapped to U+222B(Integral) or to \int (as is done by the package mmap).
The first alternative sounds more Unicode-like but
actually the screen readers don’t seem to know what
to do with the symbols.
The main task here is to get more information
to be able to decide about which route to follow.
10Contacts

Quite a number of questions and projects circle
around the pdf specification, the needs of users andof pdf consumer applications. To get tagging working it is not enough to know how TEX works. So one
important part of the tagging project is to get incontact with people having inside knowledge about
pdf and pdf consumer applications in various pdf
related organizations and to promote the project in
the TEX world to get user feedback.
11

Summary

Adding tagging facilities to LATEX is a large projectwith many aspects. Happily it doesn’t have to bedone in one large jump; with the tagpdf package it
is already possible for adventurous users with a bitof knowledge in TEX programming to tag quite large
documents. Despite the clear warning in the documentation that it isn’t meant for production, I havealready received feedback about several successful
uses. This gives hope that it can evolve to a stableand usable system.
References[1] LATEX. LATEX development formats are now
available, 2019. https://latex-project.org/
news/2019/09/01/LaTeX-dev-format/[2] H. Oberdiek. TikZ and transparent
incompatibility, 2015. https://tex.
stackexchange.com/a/253417/2388[3] TUG. PDF accessibility and PDF standards,
2019. https://tug.org/twg/accessibility/ Ulrike Fischer
LATEX ProjectMönchengladbach, Germany
ulrike.fischer (at) latex-project.org