htlatex:带有 alt 标签和自定义类的 tikzpicture

htlatex:带有 alt 标签和自定义类的 tikzpicture

请考虑以下示例:

\documentclass{scrbook}

\usepackage{blindtext}
\usepackage{pgfornament}

\title{Some Book}
\subtitle{A novel}
\author{Some Author}

\begin{document}

\chapter{Chapter One}
\blindtext{}
\begin{center}
 \begin{tikzpicture}[alt=Separator,hclass=separator]
  \node {\pgfornament[height=1cm]{79}};
 \end{tikzpicture}
\end{center}
\blindtext{}
\begin{center}
 \begin{tikzpicture}[alt=Endmarker,hclass=endmarker]
  \node {\pgfornament[height=1cm]{75}};
 \end{tikzpicture}
\end{center}

\end{document}

如上文的额外参数所示tikzpicture(但不起作用),我想在 的结果 HTML 中指定alt可访问性的文本和用于标记的元素的类。 应该可以通过和 进行解析。imgtikzpictures.texpdflatexhtlatexmake4ht

我目前得到:

<img alt=' ' src='book0x.svg' />

当然,将图像包装在附加标签(例如div)中并在class其中设置的解决方案也是可以的,而标签alt需要在图像上设置。如果该解决方案还影响\includegraphics命令,那也是可以的。

请注意,这个问题与htlatex:带有 alt 标签和自定义类的 img但关于tikzpictures,它提供了另一个间接级别,我希望能够通过图片指定类别,因为某些图片/图像将是分隔符,而其他可能是内容。

答案1

假设您将althclass键保存到自定义宏:

\documentclass{scrbook}

\usepackage{blindtext}
\usepackage{pgfornament}
\tikzset{
  alt/.store in=\myalt,
  hclass/.store in=\myclass
}
\title{Some Book}
\subtitle{A novel}
\author{Some Author}

\begin{document}

\chapter{Chapter One}
\blindtext{}
\begin{center}
 \begin{tikzpicture}[alt=Separator,hclass=separator]
  \node {\pgfornament[height=1cm]{79}};
 \end{tikzpicture}
\end{center}

\blindtext{}
\begin{center}
 \begin{tikzpicture}[alt=Endmarker,hclass=endmarker]
  \node {\pgfornament[height=1cm]{75}};
 \end{tikzpicture}
\end{center}

\end{document}

重要的代码是这样的:

\tikzset{
  alt/.store in=\myalt,
  hclass/.store in=\myclass
}

现在,键\myalt\myclass宏中都可用了,但问题是,键是在我们在 TeX4ht 驱动程序中启动图片后设置的,因此图像的开始标记已经在文档中了。我们可以使用以下配置文件来修复它:

\Preamble{xhtml}
\begin{document}
\makeatletter
\ConfigureEnv{tikzpicture}{}{}{}{}{}
\def\texfourht@tikz@begin{}
\def\texfourht@tikz@end{}
\AddToHook{env/tikzpicture/begin}{\ifdefined\myalt\ifvmode\Picture*[\myalt]{ class="\myclass"}\else\Picture+[\myalt]{ class="\myclass"}\fi\def\inside@pict@cmd{}\fi}
\AddToHook{env/tikzpicture/end}{\ifdefined\EndPicture\EndPicture\fi}

\makeatother
\EndPreamble

首先我们重置配置tikzpicture,然后我们还需要重置启动图像的其他宏。然后,我们在设置键后使用 LaTeX 环境钩子插入图片启动代码:

\AddToHook{env/tikzpicture/begin}{\ifdefined\myalt\ifvmode\Picture*[\myalt]{ class="\myclass"}\else\Picture+[\myalt]{ class="\myclass"}\fi\def\inside@pict@cmd{}\fi}

由于某种原因,此钩子使用 执行了两次tikzpicture。第一次执行时,\myalt尚未定义。因此我们需要使用 \ifdefined命令,并且仅在定义后才启动图片。

\Picture*\Picture+开始转换为图片的命令的不同版本,一个在段落内,另一个在段落外。

结果如下:

在此处输入图片描述

<p class='noindent'>           <img alt='Separator' class='separator' src='sample0x.svg' />
</p></div>
<!-- l. 23 --><p class='indent'>  Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis sem.
Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante. Donec
ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh lectus
placerat pede. Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit. Ut porttitor.
Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla
tristique neque. Sed interdum libero ut metus. Pellentesque placerat. Nam rutrum augue
a leo. Morbi sed elit sit amet ante lobortis sollicitudin. Praesent blandit blandit
mauris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a, turpis. Mauris
lacinia lorem sit amet ipsum. Nunc quis urna dictum turpis accumsan semper. 
</p>
<div class='center'>
<!-- l. 24 --><p class='noindent'>
</p><!-- l. 27 --><p class='noindent'>              <img alt='Endmarker' class='endmarker' src='sample1x.svg' />

编辑:

这个配置似乎有问题。请尝试使用此版本pgfsys-dvisvgm4ht.def

% Copyright 2021 by Michal Hoftich
% Copyright 2006 by Till Tantau
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.

\ProvidesFileRCS{pgfsys-dvisvgm4ht.def}

% Driver commands for tex4ht

%
% Load common pdf commands:
%

% we load the dvips driver by default. it doesn't support patterns and some other stuff,
% but it handles better nested images and some formatting. if you use patterns or if you
% have other issues with the default method, pass the "tikz-dvisvgm" option to make4ht. 
\ifdefined\ifOption
\ifOption{tikz+}{\input pgfsys-dvisvgm.def}{\input pgfsys-dvips.def}
\else
% load the dvips driver by default
\input pgfsys-dvips.def
\fi


\def\texfourht@tikz@begin{%
  \bgroup%
  \def\run@pict@cmd{}% insert the \Picture hooks only in the top nesting level
  \def\end@pict@cmd{}%
  \ifdefined\EndPicture\else% We are already inside command that uses \Picture
  \ifdefined\inside@pict@cmd\else% handle nested uses
  \ifdefined\tikzexternalize\else% Support externalize library
  \def\run@pict@cmd{\Picture*[\ifdefined\myalt\myalt\else\csname a:Picture-alt\endcsname\fi]{ \ifdefined\myclass class="\myclass"\fi}}%
  \def\end@pict@cmd{\EndPicture}%
  \fi\fi\fi%
  % command used to detect nesting
  \def\inside@pict@cmd{}%
  \csname a:tikzpicture\endcsname%
}

\def\texfourht@tikz@end{%
  \csname b:tikzpicture\endcsname%
  \egroup%
}

\AtBeginDocument{%
  \NewConfigure{tikzpicture}{2}%
  \catcode`\:=11%
  \Configure{tikzpicture}{%
    \protect\csname nested:math\endcsname% support display math
    \run@pict@cmd{}%
  }{\end@pict@cmd}
  % configure the output picture format to svg, as it will require dvisvgm
  % post processing. 
  \Configure{Picture}{.svg}%
  \def\pgfsys@typesetpicturebox#1{%
    \def\@tempa{#1}%
    % I've found that inserting picture instructions here leads to 
    % errors when \pic feature of pgf is used. 
    \if\@tempa\pgfpic%
      \orig@pgfsys@typesetpicturebox{#1}%
    \else%
      \texfourht@tikz@begin%
      \orig@pgfsys@typesetpicturebox{#1}%
      \texfourht@tikz@end%
    \fi%
  }
  %
  % insert tex4ht hooks around TikZ picture box
  \ConfigureEnv{tikzpicture}{\texfourht@tikz@begin}{\texfourht@tikz@end}{}{}%
  \ConfigureEnv{pgfpicture}{\texfourht@tikz@begin}{\texfourht@tikz@end}{}{}%
  \catcode`\:=12%
}


% Make the code inserted by tex4ht configurable
% 


\let\orig@pgfsys@typesetpicturebox\pgfsys@typesetpicturebox
%\def\pgf@sys@postscript@header#1{{\special{! #1}}}


\endinput


%%% Local Variables:
%%% mode: latex
%%% End:

它在它们可用时使用它们\myalt\myclass

相关内容