24

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?

2
  • 1
    Well, you know that Tex4ht will fail the \ifpdf test, so since you seem to generate your output with either pdflatex or htlatex... Commented Nov 9, 2010 at 20:18
  • 3
    @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? Commented Nov 9, 2010 at 20:21

3 Answers 3

17

There is always this:

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

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

1
  • 1
    This gives false negative if package tex4ht is not explicitly loaded but we are running tex4ht from the command-line (say: mk4ht oolatex). Maybe your approach should be combined with Nasser's approach below: \ifdefined\HCode, in a disjunction of conditions that defines another condition \ifhtlatex.
    – n.r.
    Commented Feb 16, 2014 at 3:11
14

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
    \@ifpackageloaded{tex4ht}
      {\let\iftexforht\iftrue}
      {\let\iftexforht\iffalse}
    \makeatother
    

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

  2. LaTeX pseudoconditional

    \makeatletter
    \@ifpackageloaded{tex4ht}
      {\let\iftexforhtTF\@firstoftwo}
      {\let\iftexforhtTF\@secondoftwo}
    \makeatother
    

    to be used like

    \iftexforhtTF{<code for TeX4ht>}{<code when TeX4ht isn't used>}
    
  3. Almost like a primitive conditional

    \makeatletter
    \edef\texforht{TT\noexpand\fi
      \@ifpackageloaded{tex4ht}
        {\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.


Prompted by extra \fi error when using \localtableofcontents inside an \if...\else..\fi, there is another reason for using the LaTeX conditional style (case 2 in the list above).

When used in the preamble, a construction such as

\iftexforht
  <code for TeX4ht case>
\else
  <code for non TeX4ht case>
\fi

is usually good and practical. However when used in the document body, you can incur in problems. For instance, if you have

\iftexforht\else\localtableofcontents\fi

as in the linked question, this won't work and throw a mysterious error message, because of how \localtableofcontents is defined. Instead,

\iftexforhtTF{}{\localtableofcontents}

would have worked properly.

The \iftexforhtTF macro can be defined either with the code above or, with Nasser's idea,

\makeatletter
\ifdefined\HCode
   \let\iftexforhtTF\@firstoftwo
\else
   \let\iftexforhtTF\@secondoftwo
\fi
\makeatother

Credit to karlkoeller for spotting the problem in the linked question.

1
  • 2
    \ltx@ifpackageloaded of package ltxcmds can also be used in the document body and does not have the restriction that it can only be used as preamble command. Commented Jun 24, 2013 at 5:40
5

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

\documentclass{article}
%\usepackage{tex4ht}
\makeatletter
\@ifpackageloaded{tex4ht}{\def\iftex4ht{\iftrue}}
                         {\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.

4
  • 1
    \catcode`\4=11 Commented Nov 10, 2010 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 ... Commented Nov 10, 2010 at 14:15
  • I was continuing the joke, but, you know, you could define a convenient \four macro... Commented Nov 10, 2010 at 17:47
  • @Charles: OK, sorry, I took that seriouly. Now with \four I did get it ... Commented Nov 10, 2010 at 18:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .