LaTeX Document – Correct Way to Check for LaTeX, PDFLaTeX, and HTML

latex2htmlpdftextex4ht

What is the correct way to check for if one is running in pdflatex vs latex vs. an html processor such as tex4ht?

Suppose I have some latex code that I only want to show up in html, and not show up in pdf and not show up in .ps.

Since tex4ht works in latex mode, then if I try this:

\ifpdf
  %  pdflatex code, do not show the link here
\else
  %  latex code
     \htmladdnormallink{home}{../../index.htm}
\fi

Then the above will show up the link ok when I run it by tex4ht, and will
not when I run it by pdflatex. But the problem is that the link will also show up when I run dvi2ps to generate .ps files.

if I use

\usepackage{html}
...
\begin{htmlonly}
   % html only code
\end{htmlonly}

to separate the html code, then tex4ht does not understand the above. I ended up with html pages where the code inside the above is missing !

\documentclass[titlepage]{article}%
\usepackage{ifpdf} 
\usepackage{html}
\usepackage{tex4ht} 
\usepackage{hyperref}

\begin{document}

\begin{htmlonly}
\htmladdnormallink{home}{index.htm}
\end{htmlonly}

\end{document}

then

htlatex foo.tex

And there is no link shown in the generated html. Btw, htlatex does handle the \htmladdnormallink fine as I see the links fine when I remove \begin{html}. It just does not know about \begin{html}

tex4ht does not care about pdflatex mode, it seems to work in latex mode.

However the command

latex2html foo.tex

produces an html file with the link in there.

If I try this:

\documentclass[titlepage]{article}%
 \usepackage{ifpdf} 
 \usepackage{html}
 \usepackage{hyperref}

\begin{document}        

\ifpdf
        %  pdflatex code, do not show the link here
\else
      %  latex code
      \begin{htmlonly}
         \htmladdnormallink{home}{../../index.htm} 
      \end{htmlonly}
\fi

\end{document}

Then the same problem. ps does not show the link, which is what I want, but also the html file generated by tex4ht does not show the link!

So, I was trying to use tex4ht now instead of latex2html to generate html to try it out, but I also want to generate .pdf and .ps from the same document.

What is the correct way to do this in latex? in a way that tex4ht would understand as well. Basically what I really want, is to have some code
that only used when generating html using tex4ht. Otherwise, I will just keep using latex2html if there is no way to do this.

update

This is the final setup, a complete self contained example that shows how to use the same latex file with pdflatex, latex/dvips and tex4ht.

\documentclass[titlepage]{article}%
\usepackage{ifpdf} 
\usepackage{hyperref}

\makeatletter
\edef\texforht{TT\noexpand\fi
  \@ifpackageloaded{tex4ht}
    {\noexpand\iftrue}
    {\noexpand\iffalse}}
\makeatother

\begin{document}

\ifpdf
        I am in pdf mode %  pdflatex code,will show up in pdf only
\else
      %  latex code, check if htlatex is loaded and use link only then
      \if\texforht  
         \href{../../index.htm}{home} % show up in HTML only
      \else
       I am in latex mode %  shows up in dvi and .ps only but not in html
      \fi      
\fi

\end{document}            

Now doing htlatex foo.tex and pdflatex foo.tex and latex foo.tex and dvips foo.dvi all work as expected. Only the foo.html will have the link shown.

thanks for everyone's help.

Best Answer

As you want to test for tex4ht using \@ifpackagesloaded, you do not want to load tex4ht in every job! This will interfere with the standard pdfLaTeX settings and make PDF creation more difficult. Instead, leave the package loading out (but include the test) and htlatex will make the necessary arrangements to load tex4ht when you are creating HTML files.

Related Question