8

Why does the package iftex have to be loaded outside of a \if...\else\fi structure?

(The same goes for packages ifpdf, ifluatex, ifxetex.)

Consider:

\documentclass{myclass}\begin{document}\end{document}

This works:

i.e., when \RequirePackage{iftex} is outside of \ifodt\else\fi

\NeedsTeXFormat{LaTeX2e}[1996/06/01]
\ProvidesClass{myclass}[2013/12/15 v0.1 My Class]
\LoadClass{article}
\newif\ifodt\odttrue

\RequirePackage{iftex}
\ifodt
 \RequirePackage[utf8]{inputenc}
 \RequirePackage[T1]  {fontenc}
\else 
 \ifLuaTeX\RequirePackage{fontspec}\fi
 \ifXeTeX \RequirePackage{fontspec}\fi
 \ifPDFTeX \RequirePackage[utf8]{inputenc}
           \RequirePackage[T1]  {fontenc}\fi
\fi

But not this:

i.e., when\RequirePackage{iftex} is within \ifodt\else\fi

\NeedsTeXFormat{LaTeX2e}[1996/06/01]
\ProvidesClass{myclass}[2013/12/15 v0.1 My Class]
\LoadClass{article}
\newif\ifodt\odttrue

\ifodt
 \RequirePackage[utf8]{inputenc}
 \RequirePackage[T1]  {fontenc}
\else
 \RequirePackage{iftex}
 \ifLuaTeX\RequirePackage{fontspec}\fi
 \ifXeTeX \RequirePackage{fontspec}\fi    
 \ifPDFTeX \RequirePackage[utf8]{inputenc}
           \RequirePackage[T1]  {fontenc}\fi
\fi

Error:

! Undefined control sequence.
l.12      \ifXeTeX
                   \RequirePackage{fontspec}\fi
?

2 Answers 2

8

In your \else branch you have

\else
 \RequirePackage{iftex}
 \ifLuaTeX\RequirePackage{fontspec}\fi
 \ifXeTeX \RequirePackage{fontspec}\fi    

Now \ifXeTeX is an undefined command which is OK as this is being skipped but the \fi is \fi so terminates the outer \ifodt and things go wrong. Never define conditionals within a TeX primitive \if.. as this always happens:-)

0
7

You have to delay the conditionals when (and if) the iftex package has been loaded.

\NeedsTeXFormat{LaTeX2e}[1996/06/01]
\ProvidesClass{myclass}[2013/12/15 v0.1 My Class]

\newif\ifodt
\DeclareOption{odt}{\odttrue}
\ProcessOptions\relax

\LoadClass{article}

\ifodt
  \RequirePackage[utf8]{inputenc}
  \RequirePackage[T1]  {fontenc}
  \expandafter\@gobble
\else
  \RequirePackage{iftex}
  \expandafter\@firstofone
\fi
 {\ifLuaTeX
    \RequirePackage{fontspec}
  \fi
  \ifXeTeX
    \RequirePackage{fontspec}
  \fi
  \ifPDFTeX
    \RequirePackage[utf8]{inputenc}
    \RequirePackage[T1]{fontenc}
  \fi}

When \ifodt is true, the part in braces is gobbled, while if it is false, it's evaluated; since iftex has already been loaded, the conditionals will be defined and so matched with the corresponding \fi.

There should be a better control, though, because when

 \documentclass[odt]{myclass}

is found and LuaLaTeX or XeLaTeX are used, the packages inputenc and fontenc would be loaded, which is discouraged.

0

You must log in to answer this question.

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