# problem with macro using \ifx

[Edit 1] originally titled "can't understand why this simple macro causes complex error"

[original post] I've written a book, and now I'm trying to produce the e-book using tex4ht. Adapting this post, I introduced the following macro:

\newcommand{\gpifpdf}[2]{\ifx\HCode\UnDef{#1}\else{#2}\fi}


Which should return the first argument when producing the pdf file, and the second when using mk4ht to produce the html file.

Here is my m(n)we:

\documentclass[10pt]{book}
\usepackage{hyperref}
\newcommand{\gpifpdf}[2]{\ifx\HCode\UnDef{#1}\else{#2}\fi}
\gpifpdf{
\usepackage{fontspec}
}{
\hypersetup{tex4ht}
}
\begin{document}
\gpifpdf{this is pdf text}{this is HTML text}
\end{document}


mk4ht produces an html file which displays "this is HTML text." But using Latexian to generate a pdf generates the following error:

Undefined control sequence.
\clist_if_empty:NF #1->\if_meaning:w
#1\c_empty_tl \prg_return_true: \else: \prg_return_false: \fi: \c_zero {}
l.9 \begin{document}


HOWEVER, when I comment out \usepackage{fontspec}, it works perfectly, producing a pdf reading "this is pdf text," so the macro seems to work correctly in the document body. When I leave in the \usepackage{fontspec}, and comment out the line before and the 3 lines afterwards (lines 4, 6, 7, and 8) it also produces the pdf just fine.

In other words: the macro seems to function correctly, and when I do what the macro is supposed to do by hand, everything works, but when I try to get the macro to do it, it doesn't.

I just upgrade to MacTeX 2014 from 2013 (so TeXlive 2014), and the problem continues. I am using Latexian, with the XeTeX option.

-
Aren't the braces aroun {#1} and {#2} acting as a group? You can remove them. –  Manuel Aug 30 at 12:47
You might want to change the title to a better one that explains your problem (\ifx) which could be good for future users that need to search for this. –  Manuel Aug 30 at 15:12
Thanks for your help @Manuel, changed the title as suggested, hope that is clearer. –  Nat Kuhn Aug 31 at 2:30

The syntax is not

\ifx<firsttoken><secondtoken>{<true code>}\else{<false code>}\fi


The correct one is

\ifx<firsttoken><secondtoken><true code>\else<false code>\fi


So you can remove the braces around {#1} and {#2} (which, in the case of \usepackage{fontspec} act as a group delimiter).

-
Perfect! Thank you so much. –  Nat Kuhn Aug 30 at 13:16