5

I'm trying to use some nested if statements to decide if a file exists and what kind of loadings are possible:

\IfFileExists{./emoji.sty}{
  \ifxetex
    \usepackage[bw,font=seguiemj.ttf]{emoji}
    \usepackage{fontspec}
  \else
    \usepackage{ifluatex}
    \ifluatex
      \usepackage[bw,font=Symbola_hint.ttf]{emoji}
      \usepackage{fontspec}
    \else
      \usepackage[bw]{emoji}
    \fi
  \fi
  \newcommand{\doxygenemoji}[3]{
    \IfFileExists{bw/#3.png}{
      \emoji{#3}
    }{
      #1
    }
  }
}{
  \renewcommand{\doxygenemoji}[3]{ #1 }
}

but unfortunately I get the error message:

! Extra \else.
\IfFileExists ...ileonpath {#1}{#2}{#3}}\fi \else
                                                  \closein \@inputcheck \ede...
l.60 }

where line 60 is the last closing bracket (}).

(Not yet a real MWE but hopefully I can get some hints already).

5
  • 1
    Load ifluatex before starting \IfFileExists; otherwise \ifluatex is not a conditional unless you are using luatex and therefore a missing conditional error ensues.
    – egreg
    Sep 28, 2018 at 15:43
  • @egreg, but one still needs the double ##, correct?
    – albert
    Sep 28, 2018 at 15:47
  • Yes, I was explaining the “apparently doesn't like the \ifluatex conditional“ in Phelype's answer. By the way, you have several spurious spaces in your code.
    – egreg
    Sep 28, 2018 at 15:52
  • the ones with the renewcommand (should be newcommand) around the #1 are known. Which others?
    – albert
    Sep 28, 2018 at 15:57
  • @egreg Ooh, that explains the problem with the conditional. Same problem as in my question about docstrip a few weeks ago. I'm a slow learner :P Sep 28, 2018 at 16:34

2 Answers 2

5

You need to double the parameter characters because \IfFileExists grabs the true and false branch as arguments. Also, it apparently doesn't like the \ifluatex conditional. You can use iftex's conditionals:

\usepackage{iftex}
\IfFileExists{./emoji.sty}{
  \ifXeTeX
    \usepackage[bw,font=seguiemj.ttf]{emoji}
    \usepackage{fontspec}
  \else
    \ifLuaTeX
      \usepackage[bw,font=Symbola_hint.ttf]{emoji}
      \usepackage{fontspec}
    \else
      \usepackage[bw]{emoji}
    \fi
  \fi
  \newcommand{\doxygenemoji}[3]{
    \IfFileExists{bw/##3.png}{
      \emoji{##3}
    }{
      ##1
    }
  }
}{
  \newcommand{\doxygenemoji}[3]{ ##1 }
}

Or, to avoid doubling the parameter characters and for an extra degree of robustness you can use expl3's conditionals:

\usepackage{expl3}
\ExplSyntaxOn
\file_if_exist:nTF { ./emoji.sty }
  {
    \sys_if_engine_xetex:T
      {
        \usepackage[bw,font=seguiemj.ttf]{emoji}
        \usepackage{fontspec}
      }
    \sys_if_engine_luatex:T
      {
        \usepackage[bw,font=Symbola_hint.ttf]{emoji}
        \usepackage{fontspec}
      }
    \sys_if_engine_pdftex:T
      {
        \usepackage[bw]{emoji}
      }
    \cs_new:Npn \doxygenemoji #1 #2 #3
      {
        \file_if_exist:nTF { bw/#3.png }
          {
            \emoji { #3 }
          }{
            #1
          }
      }
  }
  {
    \cs_new:Npn \doxygenemoji #1 #2 #3
      { ##1 }
  }
\ExplSyntaxOff
3

There are two errors in your code. The first is loading ifluatex inside a conditional; if the code is skipped (because the engine is XeLaTeX), \ifluatex will not yet be a conditional, but an undefined token, resulting in an unbalanced conditional.

The second error is not doubling # in the definition.

I'd reorganize the code in a different fashion, avoiding complicated code in the true and false branches of \IfFileExists.

\usepackage{ifluatex,ifxetex}

\newif\ifemoji
\IfFileExists{./emoji.sty}{\emojitrue}{\emojifalse}

\ifemoji
  \ifxetex
    \usepackage{fontspec}
    \PassOptionsToPackage{font=seguiemj.ttf}{emoji}
  \fi
  \ifluatex
    \usepackage{fontspec}
    \PassOptionsToPackage{font=Symbola_hint.ttf}{emoji}
  \fi  
  \usepackage[bw]{emoji}
  \newcommand{\doxygenemoji}[3]{%
    \IfFileExists{bw/#3.png}{%
      \emoji{#3}%
    }{%
      #1%
    }%
  }
\else
  \newcommand{\doxygenemoji}[3]{#1}
\fi 

You must log in to answer this question.

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