# Is there an \iftex4ht command, and if not, how should it be defined?

Background: I'm using TikZ to generate a load of pictures that I want to put on the web as SVGs. So I'm using the pgfsys-tex4ht.def driver and it's working very well. My gut feeling is that the SVG output from TikZ/pgf is "experimental" so while designing the pictures, I'm not using a special driver but just generating the PDFs via pdflatex. Also, I want PDFs as well as SVGs at the end.

So rather than having to comment out the line

\def\pgfsysdriver{pgfsys-tex4ht.def}


each time, I'd rather have a switch that loaded it if tex4ht was loaded; that is, I already specify the output when I decide whether to run pdflatex or htlatex - I don't want to have to specify it twice. So, my question:

Is there an \iftex4ht somewhat akin to the \ifpdf provided by ifpdf.sty?
If not, what would be the "right" way to test for the presence of tex4ht?

-
Well, you know that Tex4ht will fail the \ifpdf test, so since you seem to generate your output with either pdflatex or htlatex... – Charles Stewart Nov 9 '10 at 20:18
@Charles: yes, that did occur to me, but I don't like testing an absence - it doesn't feel safe to me. What if one day I go mad and use plain old latex? – Andrew Stacey Nov 9 '10 at 20:21

There is always this:

\@ifpackageloaded{tex4ht}{LOADED}{NOT LOADED}


Suitable for other packages of interest as well, of course. (Usual caveats about @ character apply.)

-

A conditional named \iftex4ht is impossible to define without side effects. However using directly \@ifpackageloaded{tex4ht} has a big limitation, because it's allowed only in the preamble.

There are a few strategies to circumvent the problem: all use \@ifpackageloaded in the preamble to define something that one will be able to use also in the body of the document.

1. Primitive conditional syntax

\makeatletter
{\let\iftexforht\iftrue}
{\let\iftexforht\iffalse}
\makeatother


and use \iftexforht just like any of the primitive conditionals.

2. LaTeX pseudoconditional

\makeatletter
{\let\iftexforht\@firstoftwo}
{\let\iftexforht\@secondoftwo}
\makeatother


to be used like

\iftexforht{<code for TeX4ht>}{<code when TeX4ht isn't used>}

3. Almost like a primitive conditional

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


to be used as

\if\texforht
<code for TeX4ht>
\else
<code when TeX4ht isn't used>
\fi


Which one to prefer is a matter of taste. Both the first and third methods allow conditional nesting.

-

Based on Harald's answer you can define your \iftex4ht as follows.

\documentclass{article}
%\usepackage{tex4ht}
\makeatletter
{\def\iftex4ht{\iffalse}}
\makeatother

\begin{document}
\iftex4ht TeX4ht\else no!\fi
\end{document}


OK, this answer is kind of a joke, but I wanted to point out that you can't use \newif\iftex4ht or \newcommand\iftex4ht since 4 is not letter. With \def it works (delimited macro with no arguments), but you can't nest it in other \ifs since TeX doesn't see that it's an \if.

-
\catcode\4=11 – Charles Stewart Nov 10 '10 at 14:08
@Charles: Well, of course you can do that. But then you might run into trouble somewhere else, e.g. when there's some \frac45. And you don't want to set the catcode before each call of \iftex4ht ... – Hendrik Vogt Nov 10 '10 at 14:15
I was continuing the joke, but, you know, you could define a convenient \four macro... – Charles Stewart Nov 10 '10 at 17:47
@Charles: OK, sorry, I took that seriouly. Now with \four` I did get it ... – Hendrik Vogt Nov 10 '10 at 18:00