what is the correct way to check for latex, pdflatex and html in the same latex document?

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
\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}
\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}
\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
{\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.

-
See tex.stackexchange.com/q/5153/86 for ways to build a test for tex4ht. –  Loop Space Jan 15 '13 at 7:43
@AndrewStacey, thanks, I looked at this link, and I tried to implement the solution there, but no luck. I added a note showing what I typed and the errors I get. –  Nasser Jan 15 '13 at 8:25
@Nasser You are loading tex4ht when trying to create a PDF: this sounds like a bad idea. Normally you don't load tex4ht yourself but leave that to htlatex (or similar). –  Joseph Wright Jan 15 '13 at 8:43
@Nasser I would suggest you to use a private configuration file for tex4ht. See figure 6 in E.Gurari "TeX4ht: HTML Production" (tug.org/TUGboat/tb25-1/gurari.pdf). Macro \greetings has different definition for latex and for tex4ht. –  deimi Jan 15 '13 at 8:53
@JosephWright, but this is like catch 20/20. I need to include \usepackage{tex4ht} so that I can use \@ifpackageloaded{tex4ht} as was suggested. I think this whole process is not leading any where. Instead of doing this by bits and pieces, may be someone can show a complete solution. Each one suggest to do something else, and a user like me ends up with many suggestions each conflicting with each others. –  Nasser Jan 15 '13 at 9:03

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.