[Tex/LaTex] tex4ht: configure href to open in new tab

htlatextex4hturls

I'm working with tex4ht (htlatex) and I'm trying to put links into the page it generates. I'm trying to find a way to make it so that when I place a link in the page the link is generated to have the target set to _blank. The reason for this is so that I can have these links open in a new tab.
Here is the code I have in the .tex file

\href{www.somesite.com}{The text}

Here is what it generates

<a href="www.somesite.com">The text</a>

Here is what I'd like to generate for all links except the cross links

<a href="www.somesite.com" target="_blank">The text</a>

I know there are ways to use \Configure but I can't seam to make them change the code generation at all.

Best Answer

You can redefine \href command in the custom config file to include that attribute. Create file hello.cfg:

\Preamble{xhtml}

\begin{document}
\makeatletter
\renewcommand{\href}[2]{\bgroup\let~\H@tilde%
  \Link[#1 target="_blank"]{}{}%
  {#2}\egroup\EndLink}%

\makeatother
\EndPreamble

this definition is modified macro from tex4ht sources. \let~\H@tilde is needed for support urls in form of something.com/~username/whatewer.html.

\Link command has syntax:

\Link[external_url attributes]{internal link}{declare internal link} url text\EndLink

you don't need internal link and declare internal link in your case, it is useful for crossreferences in the document.

compile your document with

htlatex filename hello

and the result:

<!--l. 5--><p class="noindent" ><a 
href="http://www.somesite.com" target="_blank" >The text</a>
</p>
Related Question