Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
1  
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
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? –  Andrew Stacey Nov 9 '10 at 20:21
add comment

4 Answers

up vote 17 down vote accepted

There is always this:

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

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

share|improve this answer
    
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. –  nicolai.rostov Feb 16 at 3:11
add comment

The following can be used. An advantage to this is that it does not need to be defined before. Hence it can be used in the preamble and the body

\ifdefined\HCode
  %htlatex code here
\else
   %pdflatex code here
\fi

some examples

Example 1

\documentclass[]{article}
\begin{document}

\ifdefined\HCode
  This show in tex4ht only
\else
  This shows in PDF only
\fi

\end{document}    

Running the above as pdflatex foo.tex produces This shows in PDF only and running it using htlatex foo.tex produces web page with This show in tex4ht only

Example 2

It can also be used in preamble

\documentclass[]{article}

\ifdefined\HCode
\else
   \usepackage{pdfpages}   %MAKE SURE YOU DO NOT USE THIS WITH htlatex
\fi

\begin{document}
% ....
\end{document}

Example 3

It can be used to load different class options also

\ifdefined\HCode
  \documentclass[]{article}
\else
  \documentclass[twocolumn]{article}  
\fi

\usepackage{lipsum}

\begin{document}    
\lipsum[1-20]    
\end{document}
share|improve this answer
    
This relies on two hypotheses: that \HCode is really undefined by all packages you load and that \undefined is undefined (which it should). Possibly \ifdefined\Hcode (reversing the true and false branches) is better. –  egreg Jun 23 '13 at 23:03
    
@egreg thanks for the suggestion. I changed the order. I really do not know how this works, but it works in my build tree for what I have used it so far. –  Nasser Jun 23 '13 at 23:18
    
This should be the accepted answer. @Nasser 's approach works both when the package tex4ht is explicitly loaded, and when it's not but you're running mk4ht oolatex from the command-line. Whereas @Harald Hanche-Olsen 's approach does not work in the latter case. –  nicolai.rostov Feb 16 at 2:57
add comment

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\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
      \@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.

share|improve this answer
1  
\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. –  Heiko Oberdiek Jun 24 '13 at 5:40
add comment

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.

share|improve this answer
1  
\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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.