michal-h21 / make4ht

Build system for tex4ht
132 stars 15 forks source link

Alt tag for images #80

Closed danielezambelli closed 2 years ago

danielezambelli commented 2 years ago

Hi michal-h21,

there are a simple way to set alt tag for images?

%--8<-------------------------------------
\documentclass[10pt,a4paper]{memoir}
\usepackage[T1]{fontenc} 
\usepackage{textcomp}   
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel} 
\usepackage{graphicx}
\usepackage{tikz}
\RequirePackage[normalem]{ulem}

\NewDocumentCommand \immagine{s O{1} m m}{
  % Macro for placing image with:
  % - Alternative text,
  % - scale (default value: 1),
  % - centered without an asterisk, with asterisk does NOT center the image.
  % Examples:
  %% \immagine{}{}
  %% \immagine{Alternative text}{\miaimmagine}
  %% \immagine[.5]{Alternative text}{\miaimmagine}
  %% \immagine*[.5]{Alternative text}{\miaimmagine}
  %
  % \begin{alternativetext}[#3]
  \IfBooleanF{#1}
    {\begin{center}}
  \scalebox{#2}{#4}
  \IfBooleanF{#1}
    {\end{center}}
  % \end{alternativetext}
}

\newcommand{\pitagora}[2]{
  \begin{tikzpicture}[x=5 mm, y=5 mm, smooth, line cap=round]
    \filldraw [gray, fill=yellow] 
      (0, 0) -- (0, -#1) -- (+#1, -#1) -- (+#1, 0) -- cycle
      (0, 0) -- (-#2, 0) -- (-#2, +#2) -- (0, +#2) -- cycle
      (0, +#2) -- (+#2, +#1+#2) -- (+#1+#2, +#1) -- 
      (+#1, 0) -- cycle;
    \draw [very thick, pink!70!black] 
      (0, 0) -- (#1, 0) -- (0, #2) -- cycle; 
  \end{tikzpicture}
}

\begin{document}

There are a simple way to set alt tag for images?

\immagine{Pythagorean theorem}{\pitagora{4}{3}}

\end{document}

%--8<-------------------------------------

Thanks.

michal-h21 commented 2 years ago

For ordinary \includegraphics command, you can add alt text using the alt option, like \includegraphics[alt=hello]{hello.png}. With TikZ pictures, it is a bit more complicated.

First of all, you need the updated driver, where I just added the support for the alt text. Then, you need to pass the alt text from your command to the \Picture command used by TeX4ht. You already have the alternativetext environment, so you can use that:

%--8<-------------------------------------
\documentclass[10pt,a4paper]{memoir}
\usepackage[T1]{fontenc} 
\usepackage{textcomp}   
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel} 
\usepackage{graphicx}
\usepackage{tikz}
\RequirePackage[normalem]{ulem}

\NewDocumentCommand \immagine{s O{1} m m}{
  % Macro for placing image with:
  % - Alternative text,
  % - scale (default value: 1),
  % - centered without an asterisk, with asterisk does NOT center the image.
  % Examples:
  %% \immagine{}{}
  %% \immagine{Alternative text}{\miaimmagine}
  %% \immagine[.5]{Alternative text}{\miaimmagine}
  %% \immagine*[.5]{Alternative text}{\miaimmagine}
  %
  \begin{alternativetext}{#3}
  \IfBooleanF{#1}
    {\begin{center}}
  \scalebox{#2}{#4}
  \IfBooleanF{#1}
    {\end{center}}
  \end{alternativetext}
}

\newenvironment{alternativetext}[1]{}{}

\newcommand{\pitagora}[2]{
  \begin{tikzpicture}[x=5 mm, y=5 mm, smooth, line cap=round]
    \filldraw [gray, fill=yellow] 
      (0, 0) -- (0, -#1) -- (+#1, -#1) -- (+#1, 0) -- cycle
      (0, 0) -- (-#2, 0) -- (-#2, +#2) -- (0, +#2) -- cycle
      (0, +#2) -- (+#2, +#1+#2) -- (+#1+#2, +#1) -- 
      (+#1, 0) -- cycle;
    \draw [very thick, pink!70!black] 
      (0, 0) -- (#1, 0) -- (0, #2) -- cycle; 
  \end{tikzpicture}
}

\begin{document}

There are a simple way to set alt tag for images?

\immagine{Pythagorean theorem}{\pitagora{4}{3}}

\end{document}

%--8<-------------------------------------

You can then use the configuration file to redefine the environment to pass the alt text:


\Preamble{xhtml}
\renewenvironment{alternativetext}[1]{\Configure{Picture-alt}{#1}}{}
\begin{document}
\EndPreamble
danielezambelli commented 2 years ago

Thank you Michal, it works fine!

I copied the file: pgfsys-dvisvgm4ht.def in the working directory.

But if I want to have this feature available to many projects I should copy the file to each dir.

Is there any way to avoid this?

Thanks

michal-h21 commented 2 years ago

Sure, you can place them to the ~/texmf/tex/generic/dvisvgm4ht/ directory, then it should be available everywhere. You can test if the correct file is used by LaTeX by running the

 kpsewhich pgfsys-dvisvgm4ht.def

command. It shouuld list the correct location.

danielezambelli commented 2 years ago

Thank you Michal!

danielezambelli commented 1 year ago

The latest version of the configuration file is suitable for the "alt" tag, but now the math macros no longer work.

Evidently I introduced some error, but I can't figure out what I need to correct.

I attach the config file, can you help me, thanks.

%--8<-------------------------------------
\documentclass[10pt,a4paper]{memoir}
\usepackage[T1]{fontenc} 
\usepackage{textcomp}   
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel} 
\usepackage{amsmath, amssymb, amsthm}% amsfonts è caricato da amssymb
\usepackage{graphicx}
\usepackage{tikz}
\usepackage[normalem]{ulem}

\RequirePackage[colorlinks, hyperindex, pagebackref,
                linkcolor=RoyalBlue, filecolor=RoyalBlue,
                citecolor = black, urlcolor=RoyalBlue]{hyperref}

\RequirePackage[most, minted]{tcolorbox}% per i box colorati

% \RequirePackage[most, minted]{tcolorbox}% per i box colorati
% 
% \RequirePackage[colorlinks, hyperindex, pagebackref,
%                 linkcolor=RoyalBlue, filecolor=RoyalBlue,
%                 citecolor = black, urlcolor=RoyalBlue]{hyperref}

\NewDocumentCommand \immagine{s O{1} m m}{
  % Macro for placing image with:
  % - Alternative text,
  % - scale (default value: 1),
  % - centered without an asterisk, with asterisk does NOT center the image.
  % Examples:
  %% \immagine{}{}
  %% \immagine{Alternative text}{\miaimmagine}
  %% \immagine[.5]{Alternative text}{\miaimmagine}
  %% \immagine*[.5]{Alternative text}{\miaimmagine}
  %
  \begin{alternativetext}{#3}
  \IfBooleanF{#1}
    {\begin{center}}
  \scalebox{#2}{#4}
  \IfBooleanF{#1}
    {\end{center}}
  \end{alternativetext}
}

\newenvironment{alternativetext}[1]{}{}

\newcommand{\pitagora}[2]{
  \begin{tikzpicture}[x=5 mm, y=5 mm, smooth, line cap=round]
    \filldraw [gray, fill=yellow] 
      (0, 0) -- (0, -#1) -- (+#1, -#1) -- (+#1, 0) -- cycle
      (0, 0) -- (-#2, 0) -- (-#2, +#2) -- (0, +#2) -- cycle
      (0, +#2) -- (+#2, +#1+#2) -- (+#1+#2, +#1) -- 
      (+#1, 0) -- cycle;
    \draw [very thick, pink!70!black] 
      (0, 0) -- (#1, 0) -- (0, #2) -- cycle; 
  \end{tikzpicture}
}

\DeclareRobustCommand{\N}{\mathbb{N}}
\DeclareRobustCommand{\Nz}{\mathbb{N}^0}
\DeclareRobustCommand{\Z}{\mathbb{Z}}
\DeclareRobustCommand{\Zz}{\mathbb{Z}^0}
\DeclareRobustCommand{\Q}{\mathbb{Q}}
\newcommand{\tonda}[1]{\left( #1 \right)}
\newcommand{\quadra}[1]{\left[ #1 \right]}
\DeclareRobustCommand{\coppia}[2]{\tonda{#1;~#2}}
\newcommand{\terna}[3]{\tonda{#1;~#2;~#3}}
\newcommand{\punto}[3]{#1\tonda{#2;~#3}}

\begin{document}

There are a simple way to set alt tag for images?

\immagine{Pythagorean theorem}{\pitagora{4}{3}}

Some math symbols:

\(\N;\quad \Nz;\quad \Z;\quad \Zz;\quad \Q;\)

\(\tonda{a +b}{a -b};\quad \quadra{\tonda{a +b}{a -b}}-\tonda{a +b}^2;\quad 
\coppia{x}{y};\quad \terna{x}{y}{z};\quad \punto{P}{+3}{-5}\)

\end{document}

%--8<-------------------------------------

ml_make4ht.zip

michal-h21 commented 1 year ago

There is an error in your MathJax configuration, it contains extra document string, which causes Javascript error. Here is a fixed version:

\Preamble{xhtml}

\renewenvironment{alternativetext}[1]{\Configure{Picture-alt}{#1}}{}

\catcode`\:=11
\Configure{underbrace}
   {\Configure{mathop*}{*}
         {<\a:mathml munder
                 \a:mathml accentunder="false"><\a:mathml mrow>}
         {</\a:mathml mrow><\a:mathml mo>\string
               &\#x23DF;</\a:mathml mo></\a:mathml munder>}
         {\Configure{mathop}{}{}{}{}}%
   }
   {\let\o:underbrace:=\displaystyle}
   {}
\Configure{overrightarrow}
  {\leavevmode\hbox\bgroup$\Tg<\a:mathml mover accent="true" class="overrightarrow">%
   \Tg<\a:mathml mrow\Hnewline>\:gobble}
  {\Tg</\a:mathml mrow>\HCode{<\a:mathml mo>&\#x2192;</\a:mathml mo>}%
   \Tg</\a:mathml mover>$\egroup}
\Configure{overleftarrow}
  {\leavevmode\hbox\bgroup$\Tg<\a:mathml mover accent="true" class="overleftarrow">%
   \Tg<\a:mathml mrow\Hnewline>\:gobble}
  {\Tg</\a:mathml mrow>\HCode{<\a:mathml mo>&\#x2190;</\a:mathml mo>}%
   \Tg</\a:mathml mover>$\egroup}

\catcode`\:=12

\Configure{uline}
   {\HCode{<span class="uline">}\NoFonts} {\EndNoFonts\HCode{</span>}}
\Configure{uline-math}
   {\HCode{<span class="uline">}\NoFonts} {\EndNoFonts\HCode{</span>}}
\Css{.uline,.uline-math{ text-decoration:underline; }}
\Configure{uuline}
   {\HCode{<span class="uuline">}\NoFonts} {\EndNoFonts\HCode{</span>}}
\Configure{uuline-math}
   {\HCode{<span class="uuline">}\NoFonts} {\EndNoFonts\HCode{</span>}}
\Css{.uuline,.uuline-math{ text-decoration:underline double; }}
\Configure{sout}
   {\HCode{<span class="sout">}\NoFonts} {\EndNoFonts\HCode{</span>}}
\Configure{sout-math}
   {\HCode{<span class="sout">}\NoFonts} {\EndNoFonts\HCode{</span>}}
\Css{span.sout, .sout-math {text-decoration: line-through }}
\Configure{xout}
   {\HCode{<span class="xout">}\NoFonts} 
   {\EndNoFonts\HCode{</span>}}
\Configure{xout-math}
   {\HCode{<span class="xout-math">}\NoFonts} 
   {\EndNoFonts\HCode{</span>}}
\Css{span.xout, .xout-math {text-decoration: line-through underline }}
\Configure{uwave}
   {\HCode{<span class="uwave">}\NoFonts} 
   {\EndNoFonts\HCode{</span>}}
\Configure{uwave-math}
   {\HCode{<span class="uwave-math">}\NoFonts} 
   {\EndNoFonts\HCode{</span>}}
\Css{span.uwave, .uwave-math {text-decoration:underline wavy; }}
\Configure{dashuline}
   {\HCode{<span class="dashuline">}\NoFonts} 
   {\EndNoFonts\HCode{</span>}}
\Configure{dashuline-math}
   {\HCode{<span class="dashuline-math">}\NoFonts} 
   {\EndNoFonts\HCode{</span>}}
\Css{span.dashuline, .dashuline-math {text-decoration:underline dashed;}}
\Configure{dotuline}
   {\HCode{<span class="dotuline">}\NoFonts} 
   {\EndNoFonts\HCode{</span>}}
\Configure{dotuline-math}
   {\HCode{<span class="dotuline-math">}\NoFonts} 
   {\EndNoFonts\HCode{</span>}}
\Css{span.dotuline, .dotuline-math {text-decoration:underline dotted;}}

\ifcsname a:MathJaxConfig\endcsname

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Configurations for MathJax %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\catcode`\#=11 
\Configure{MathJaxConfig}{{ 
  tex: { 
    tags: "ams", 
    packages: {'[+]': ['html','enclose']},
    \detokenize{% 
      macros: { 
        uline: ["\\enclose{bottom}{#1}",1], 
        uuline: ["\\enclose{bottom}{\\enclose{bottom}{#1}}",1],  
        uwave: ["#1",1], 
%         uwave: ["\\enclose{bottom}{#1}",1],
%         uwave: ["\\style{border-bottom:1px underline wavy black;}{#1}",1], 
        sout: ["\\enclose{horizontalstrike}{#1}",1], 
        xout: ["\\enclose{bottom}{\\enclose{horizontalstrike}{#1}}",1], 
        dashuline: ["\\style{border-bottom:1px dashed black;}{#1}",1], 
        dotuline: ["\\style{border-bottom:1px dotted black;}{#1}",1], 
        N: "\\mathbb{N}", 
        Nz: "\\mathbb{N}^0",
        Z: "\\mathbb{Z}",
        Zz: "\\mathbb{Z}^0",
        Q: "\\mathbb{Q}",
        Qz: "\\mathbb{Q}^0",
        Qp: "\\mathbb{Q}^+",
        J: "\\mathbb{J}",
        R: "\\mathbb{R}",
        Rz: "\\mathbb{R}^0",
        Rp: "\\mathbb{R}^+",
        C: "\\mathbb{C}",
        IR: "{}^*\\hspace{-.12em}\\mathbb{R}",
        IC: "{}^*\\hspace{-.12em}\\mathbb{C}",
        A: "\\mathbb{A}",
        K: "\\mathbb{K}",
        Dispari: "\\mathbb{D}",
        Pari: "\\mathbb{P}",
        Pol: "\\mathbb{P}",
        vari: "\\mathrm{Var}",
        cfvar: "\\mathrm{CV}",
        cvar: "\\mathrm{CVar}",
        spV: "\\mathbf{V}",
        Kor: "\\mathbf{K}",
        Rel: "\\mathfrak{R}",
        Dom: "\\mathcal{D}",
        ID: "\\mathrm{I.D.}",
        Cod: "\\mathcal{C}",
        II: "\\mathrm{I.I.}",
        divint: "\\;\\mathrm{div}\\;",
        mcd: "\\mathrm{MCD}",
        mcm: "\\mathrm{mcm}",
        card: "\\mathrm{card}",
        CE: "\\mathrm{C.E.}",
        IS: "\\mathrm{I.S.}",
        Area: "\\mathrm{Area}",
        media: "\\mathrm{media}",
        mediana: "\\mathrm{mediana}",
        moda: "\\mathrm{moda}",
        dd: "\\mathrm{d}",
        dx: "\\mathrm{d}x",
%         effestar: "{}^*\\hspace{-.15em}f",
        star: ["{}^*\\hspace{-.15em}#1", 1],
        cels: "{}^\\circ\\hspace{-.10em}\\mathrm{C}",
        meuro: "\\, \\text{€}",
        munit: ["\\, \\mathrm{#1}", 1],
        Ast: "{}^{\\ast}",
        grado: "{}^{\\circ}",
        aC: "a.e.v.",
        dC: "p.e.v.",
        aev: "a.e.v.",
        paev: "p.e.v.",
        epsilon: "\\varepsilon",
        theta: "\\vartheta",
        rho: "\\varrho",
        phi: "\\varphi",
        tonda: ["\\left(#1\\right)", 1],
        quadra: ["\\left[#1\\right]", 1],
        graffa: ["\\left\\{#1\\right\\}", 1],
        abs: ["\\left\\lvert#1\\right\\lvert", 1],
        valass: ["\\left\\lvert#1\\right\\lvert", 1],
        angolare: ["\\left\\langle#1\\right\\rangle", 1],
        sistema: ["\\left\\{\\begin{array}{lcl}#1\\end{array}\\right.", 1],
        fatratti: ["\\left\\{\\begin{array}{rclcl}#1\\end{array}\\right.", 1],
        vettore: ["\\left(\\begin{array}{c}#1\\end{array}\\right)", 1],
        matrice: ["\\tonda{\\begin{array}{#1}#2\\end{array}}", 2],
        vect: ["\\overrightarrow{#1}", 1],
        coppia: ["\\tonda{#1;~#2}", 2],
        terna: ["\\tonda{#1;~#2;~#3}", 3],
        punto: ["#1\\tonda{#2;~#3}", 3],
        Deriv: ["\\mathit{D}\\quadra{#1}", 1],
        intervcc: ["\\left[#1;~#2\\right]", 2],
        intervac: ["\\left]#1;~#2\\right]", 2],
        intervca: ["\\left[#1;~#2\\right[", 2],
        intervaa: ["\\left]#1;~#2\\right[", 2],
        sand: "~ \\wedge ~",
        sor: "~ \\vee ~",
        scap: "~ \\cap ~",
        scup: "~ \\cup ~",
        svert: "~ \\vert ~",
        sLarrow: "~ \\Leftarrow ~",
        ssLarrow: "\\quad \\Leftarrow \\quad",
        sLRarrow: "~ \\Leftrightarrow ~",
        ssLRarrow: "\\quad \\Leftrightarrow \\quad",
        sRarrow: "~ \\Rightarrow ~",
        ssRarrow: "\\quad \\Rightarrow \\quad",
        slarrow: "~ \\leftarrow ~",
        sslarrow: "\\quad \\leftarrow \\quad",
        slrarrow: "~ \\leftrightarrow ~",
        sslrarrow: "\\quad \\leftrightarrow \\quad",
        srarrow: "~ \\rightarrow ~",
        ssrarrow: "\\quad \\rightarrow \\quad",
        stext: ["~~\\text{#1}~~", 1],
        sstext: ["\\quad\\text{#1}\\quad", 1],
        ost: "\\mathrm{st}",
        st: ["\\ost \\tonda{#1}", 1], 
        omon: "\\mathrm{mon}",
        mon: ["\\omon \\tonda{#1}", 1],
        oGal: "\\mathrm{Gal}",
        Gal: ["\\oGal \\tonda{#1}", 1],
        infvic: "\\approx",
        indist: "\\sim",
        sen: "\\sin",
        tg: "\\tan",
        unit: "\\,",
        relax: "",
      } 
    } 
  } 
} 
//}}}
\catcode`\#=6 

\else

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% code for MathML            %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\fi

\begin{document} 
\EndPreamble