1

I am trying to include a nice LaTeX snippet (duly credited) in my default class. But since it gets compiled on several different systems, I would like to have so minor error handling code in case a package or another is not available.

Since I am not experienced with LaTeX 3, I can't see past the fact that there is LaTeX 3 code in here, and that the problem might come from there:

  \IfFileExists{expl3.sty}{%
    \IfFileExists{etoolbox.sty}{%
      %% requires LaTeX3 dependencies
      \RequirePackage{expl3}

      \ExplSyntaxOn
      \prop_new:N \g_cite_map_prop
      \tl_new:N \l_citekey_result_tl

      \cs_new:Npn \mapcitekey #1#2 {
        \clist_map_inline:nn {#2}
             {  \prop_gput:Nnn  \g_cite_map_prop  {##1} {#1}   }
      }

      \cs_new:Npn \getcitekey #1 {
         \prop_get:NoN \g_cite_map_prop{#1}  \l_citekey_result_tl
         \quark_if_no_value:NF \l_citekey_result_tl
             {  \tl_set_eq:NN #1  \l_citekey_result_tl  }
      }

      \cs_new:Npn \showcitekeymaps {\prop_show:N  \g_cite_map_prop }
      \ExplSyntaxOff

      %% patch citation commands
      \RequirePackage{etoolbox}
      \makeatletter
      \patchcmd{\@citex}{\if@filesw}{\getcitekey\@citeb \if@filesw}%
          {\typeout{*** SUCCESS ***}}{\typeout{*** FAIL ***}}
      \patchcmd{\nocite}{\if@filesw}{\getcitekey\@citeb \if@filesw}%
          {\typeout{*** SUCCESS ***}}{\typeout{*** FAIL ***}}
      \makeatletter

      %% info message
      \PackageInfo{mapcitekey-package}{%
        ``mapcitekey'' patched cite and nocite.\MessageBreak
        If you use another citation package (such as natbib),
        in conjunction with ``mapcitekey'', you must also patch
        their citation command.\MessageBreak
      }
    }{%
      \PackageWarning{mapcitekey-package}{%
        ``etoolbox'' is not available.\MessageBreak
        Cannot patch the citation commands for ``mapcitekey''\MessageBreak
      }%
    }%
  }{%
    \PackageWarning{mapcitekey-package}{%
      LaTeX 3 is not available.\MessageBreak
      Cannot properly define the ``mapcitekey'' macro.\MessageBreak
      Attempt to define a bogus ``mapcitekey'' command.\MessageBreak
    }%
    \newcommand{\mapcitekey}[2]{}
  }% ERRORLINE

The error message that I get right now is:

ERROR: Incomplete \ifeof; all text was ignored after line ERRORLINE.

but the whole block has a bunch of issues that I can't figure out. I'm not sure if the issue is the IfFileExists block, or using LaTeX 3 code, or patching cite and nocite.

How should I tweak this so that it compiles?

  • 1
    Well, you can't compile a snippet or even a .sty file - and neither can we. So we can't reproduce the error message you're reporting. And we have no idea what you are trying to compile or for what purpose. Given that, the easiest way to tweak it is probably to just remove the code. For all you've said, that will work just as well and achieve your purposes perfectly. – cfr Jul 31 '16 at 3:13
  • Since you do need etoolbox, it's rather dubious branch for its existence. Just assume etoolbox is available. – egreg Jul 31 '16 at 19:26
5

\IfFileExists is a command with 3 arguments:

  \long\def \IfFileExists#1#2#3 ....

This means at first that you can't change to expl3 catcodes inside the arguments. Neither \ExplSyntaxOn nor \makeatletter is doing what you expect as you can easily test:

\documentclass{article}
\usepackage{expl3}
\IfFileExists{expl3.sty}{%
      \ExplSyntaxOn
      \prop_new:N \g_cite_map_prop
      \ExplSyntaxOff
    }{}%

\begin{document}
blub
\end{document}

And at second this means that the #1 and #2 in your code is referring to the arguments of \IfFileExists, so you are reinserting them again -- I'm not trying to find out what then happens, but it is really not wonder that it explodes.

It is not a good idea to put large complicated code is such an argument. If you really think such tests are necessary, them better move it to some external style or some external command and then do something like

\IfFileExists{expl3.sty}
 {\RequirePackage{my-expl-3-code}}
 {}%

or

\IfFileExists{expl3.sty}
 {\mapcitekey@patchcitex}
 {}%
| improve this answer | |
  • Thanks for your answer. I have How would you suggest I move all of that to an external command? Simply do a \def\mytempcommand{ ...everything in here... }? Unrelated but, it's funny, I've been using (and citing) another one of your tips for about 9 years! – Jérémie Aug 1 '16 at 4:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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