# Is it possible to add an entry to the HEAD section of the HTML from inside the latex file with tex4ht?

I use a global .cfg file for all my latex source tree when compiling with tex4ht. This file has generic configuration common to all latex files.

I'd like to add a specific META tags to the HEAD section of each specific HTML. (such as description tag and may be others)

I can't add <meta name="description" content="my page description"> to the global .cfg as this something has to be done locally for each latex file. This has to go to the HEAD section of the HTML generated.

But I am not able to figure how to insert such an entry into the HEAD section of the HTML. Also make4ht will not accept two .cfg files, one global and one local. It will only use the first one on the command line. So something like this

make4ht -c global.cfg  -c local.cfg  foo.tex


does not work.

So the best thing is to edit each latex file itself and add these additional HEAD configuration in the latex file itself.

I know it is possible to localy modify .css for each file using this method 1

\documentclass[11pt]{article}
\begin{document}

%this below will be added to the foo.css file in addition to what
%is the global .cfg file
\ifdefined\HCode
\Css{body{background-color:green;}}
\ConfigureEnv{quote}{\Tg<quote>}{\Tg</quote>}{}{}
\fi

This is a test
\end{document}


The above modifies the local .css file correctly. So I need something similar to the above, but which modifies/add to the HEAD section instead of the .css file.

I still need to use the global .cfg file ofcourse. I tried this

\documentclass[11pt]{article}
\begin{document}

\ifdefined\HCode
\ConfigureEnv{quote}{\Tg<quote>}{\Tg</quote>}{}{}
\fi

This is a test
\end{document}


Then make4ht -c ~/my_global.cfg foo.tex but the generated foo.html does not have the line meta name="description" there. So the syntax I am using is wrong and tried many other things for the last hr, nothing is working for me. I also tried \ConfigureEnv{@HEAD}....

Is it possible to add an entry to the HEAD section of the HTML from inside the latex file with tex4ht?

references

http://tug.org/pipermail/tex4ht/2013q2/000803.html

The basic HTML structure is inserted at \begin{document}, so when you configure @HEAD in the document body, it cannot have an effect. In fact, most of what tex4ht does is executed at \begin{document}, including redefinitions of LaTeX packages.

So the configuration must be placed in the document preamble and executed at the \begin{document}. This can be achieved using the \AtBeginDocument command:

\documentclass[11pt]{article}

\ifdefined\HCode
\AtBeginDocument{%
\ConfigureEnv{quote}{\Tg<quote>}{\Tg</quote>}{}{}
}
\fi

\begin{document}
This is a test
\end{document}


The result:

<!DOCTYPE html>
<html lang="en-US" xml:lang="en-US" >
<meta  charset="utf-8" />
<meta name="generator" content="TeX4ht (http://www.tug.org/tex4ht/)" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<meta name="src" content="sample.tex" />
<meta name="description" content="description"/>
>
<!--l. 11--><p class="noindent" >This is a test </p>
</body>
</html>


You can use the \AtBeginDocument trick also for your common code. Just save the configurations from your global config file to a myconfig.sty file:

\AtBeginDocument{%
\ConfigureEnv{quote}{\Tg<quote>}{\Tg</quote>}{}{}
}
\endinput


And the require it from a local config file:

\Preamble{xhtml}
\RequirePackage{myconfig}
\begin{document}
\EndPreamble


Result:

<!DOCTYPE html>
<html lang="en-US" xml:lang="en-US" >
<meta  charset="utf-8" />
<meta name="generator" content="TeX4ht (http://www.tug.org/tex4ht/)" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<meta name="src" content="sample.tex" />
<meta name="description" content="description"/>
<meta name="test" content="test"/>
>
<!--l. 9--><p class="noindent" >This is a test </p>
</body>
</html>

• Thanks. I did try it in the preamble also, but did not have the \AtBeginDocument{ in there. Your solution worked very well. I'll stick to the first one (putting it in the latex file) than the second option you showed, as it is simpler. – Nasser Nov 22 '18 at 8:45
• @Nasser the second solution is more general. You can include more sty files in the config file, so you can break the configuration to logical blocks and use them only when necessary. It is also easier to fix bugs this way. – michal.h21 Nov 22 '18 at 9:43