6

In a previous question, I learned how to add alt text to a TikZ image. This validates as UA-1 according to VeraPDF. On the other hand, PDF Accessibility Checker says this is not UA-1 because the TikZ figure doesn't have a bounding box. How do I provide the bounding box to this figure? (Among other issues, we can get the size of the bounding box, but can't know the location until the page is shipped out.) I've come tantalizingly close with the following:

% !TEX program = LuaLaTeX
\DocumentMetadata{tagging=on,lang=en,pdfversion=1.7,pdfstandard=ua-1}
\title{My Title}
\documentclass{article}
\usepackage{graphicx, tikz}
\ExplSyntaxOn
\dim_new:N \g_tpmm_dx_dim
\dim_new:N \g_tpmm_dy_dim
\cs_new:Nn \tpmm_setupBBox: {
    \node(current~bounding~box.south~west){\savepos};
    \pgfextractx{\g_tpmm_dx_dim}{\pgfpointdiff{\pgfpointanchor{current~bounding~box}{west }}{\pgfpointanchor{current~bounding~box}{east }}}
    \pgfextractx{\g_tpmm_dy_dim}{\pgfpointdiff{\pgfpointanchor{current~bounding~box}{south}}{\pgfpointanchor{current~bounding~box}{north}}}
    \dim_gset_eq:NN \g_tpmm_dx_dim \g_tpmm_dx_dim
    \dim_gset_eq:NN \g_tpmm_dy_dim \g_tpmm_dy_dim
    \hook_gput_next_code:nn { shipout/after } { \tpmm_addBBox:ennee {\tag_get:n{struct_num}} {\lastxpos} {\lastypos} {\g_tpmm_dx_dim} {\g_tpmm_dy_dim} }
}
\cs_new:Npn \tpmm_addBBox:ennee #1#2#3#4#5 {
    \dim_set:Nn \g_tpmm_dx_dim { #4 }
    \dim_set:Nn \g_tpmm_dy_dim { #5 }
    \dim_add:Nn \g_tpmm_dx_dim { #2 sp }
    \dim_add:Nn \g_tpmm_dy_dim { #3 sp }
    % see latex-lab-testphase-graphic.sty
    \tag_struct_gput:ene{#1}{attribute}
       {
         /O /Layout /BBox~
           [
             \dim_to_decimal:n { #2 sp } \c_space_tl
             \dim_to_decimal:n { #3 sp } \c_space_tl
             \dim_to_decimal:n { \g_tpmm_dx_dim } \c_space_tl
             \dim_to_decimal:n { \g_tpmm_dy_dim }
           ]
       }
}
\hook_gput_code:nnn{env/tikzpicture/end}{tikz}{\tpmm_setupBBox:}
\ExplSyntaxOff
\begin{document}

\tagstructbegin{tag=Figure,alt=a circle}\tagmcbegin{}%
\SuspendTagging{tikz}%
\begin{tikzpicture}
    \draw (0,0) circle (1cm);
\end{tikzpicture}%
\par
\ResumeTagging{tikz}%
\tagmcend\tagstructend

\tagstructbegin{tag=Figure,alt=a circle}\tagmcbegin{}%
\SuspendTagging{tikz}%
\begin{tikzpicture}
    \draw (0,0) circle (1cm);
\end{tikzpicture}%
\par
\ResumeTagging{tikz}%
\tagmcend\tagstructend

\tagstructbegin{tag=Figure,alt=a circle}\tagmcbegin{}%
\SuspendTagging{tikz}%
\begin{tikzpicture}
    \draw (0,0) circle (1cm);
\end{tikzpicture}%
\par
\ResumeTagging{tikz}%
\tagmcend\tagstructend
\end{document}

Unfortunately, it appears that multiple figures interfere with each other: their bounding boxes use entries 0 and 1 of the last figure, and entries 2 and 3 accumulate. How can I determine the bounding box of a TikZ picture?

2
  • @Qrrbrbirlbel That helps (at least, the numbers change). But the bounding boxes are still interfering with each other. Commented Jun 14, 2025 at 15:46
  • The node also has a width and height which changes the bounding box directly you evaluate right after. Commented Jun 14, 2025 at 15:49

1 Answer 1

9

You don't need any manual tagging. With the newest LaTeX tikz pictures are tagged. If you want to know how it is done, check the code and comments in latex-lab-graphic.dtx and latex-lab-tikz.dtx:

\DocumentMetadata{tagging=on,lang=en,
pdfversion=1.7,pdfstandard=ua-1,
}
\title{My Title}
\documentclass{article}
\usepackage{graphicx, tikz}
\begin{document}

\begin{tikzpicture}[alt=a circle]
    \draw (0,0) circle (1cm);
\end{tikzpicture}%

\begin{tikzpicture}[alt=a circle]
    \draw (0,0) circle (1cm);
\end{tikzpicture}%

\end{document}

and PAC is happy:

enter image description here

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.