15

I read here (second comment to the question)

When should I use \input vs. \include?

that \@input "does not throw an error if the file does not exist".

If I try

\documentclass{article}
\begin{document}
\@input{toBeIncluded.tex}
\end{document}

I indeed get no fatal error, but still i get 3 compilation errors and, more importanly, my pdf contains the word "inputtoBeIncluded.tex".

Is there a simple way to completely ignore the input command if the input file does not exist?

Thanks a lot

Andrew Swann
  • 95,762
Giulioo
  • 153

2 Answers2

21

Instead of using \@input the better method is probably to use

\InputIfFileExists{file}{then}{else}

This will run \input on file if it exists, and then execute the code in then. If the file does not exist, the code in else is executed. You could for example add a warning in the else part, just to inform you that this particular file was not found.

If you just want a blink input if the file exists, just use

\InputIfFileExists{file}{}{}

For more details on this macro see texdoc source2e it is described in the ltfiles.dtx part, secion 19, File handling

daleif
  • 54,450
  • the behavior of InputIfFileExists looks very very odd to me. To begin with if i try \documentclass{article} \begin{document} \InputIfFileExists{iDontExist.tex}{}{} \end{document} i simply get an error! Also it seems to "remember" things from the previously compiled pdf file.. What am i missing? – Giulioo Jun 30 '17 at 07:24
  • @Giulioo Exactly which error do you get? Your example compiles fine for me (pdflatex), thoug you get a warning (not an error) that there is no output. – daleif Jun 30 '17 at 09:42
  • thx. I was compiling through texnich center.. but even if i do through pdflatex i still get no pdf. But i think the issue is that he does not like a completely empty pdf as output. Indeed even this does not work for me:\documentclass{article} \begin{document} \end{document} (I dont undertsand why: looks like a bug to me). As soon as i have some text it seems to work. Let me play a bit more with it and will see. – Giulioo Jun 30 '17 at 12:36
  • @Giulioo exactly which LaTeX version do you have? If the only contents of your file is the attempt to input a non-existing file, then of course there is no output. If your LaTeX installation is too old that may lead to an error – daleif Jun 30 '17 at 12:39
  • pdflatex --version returns MiKTeX-pdfTeX (MiKTeX 2.9.6300 64-bit). Dont you agree that the output of \documentclass{article} \begin{document} \end{document} should be an empty pdf rather than no pdf at all? – Giulioo Jun 30 '17 at 12:44
  • @Giulioo no, if there is no output to the PDF, there is no reason to make an empty pdf. – daleif Jun 30 '17 at 12:53
  • ok thx a lot for your help. All the confusion came from the fact that my example was too simplified.. I m now using InputIfFileExists and will see how it goes – Giulioo Jun 30 '17 at 12:57
  • What to write in the {else} to make the compilation stop with error of missing file? – Diaa Dec 23 '20 at 11:13
  • @Diaa I usually use \ERROR or use the packageerror commands – daleif Dec 23 '20 at 11:50
  • @daleif I asked the question here https://tex.stackexchange.com/q/576137/2288 in case you are interested in giving another answer, Thanks – Diaa Dec 23 '20 at 11:52
4

You need to add \makeatletter to enable the use of the symbol @ in a macro.

\documentclass{article}
\begin{document}
abc
\makeatletter
\@input{myfile.tex}
\makeatother   

\end{document}

EDIT

As noted by @Emil Jeřábek, this has the side effect of changing the catcode of @ whilst myfile.tex is being read. This is unlikely to have any adverse effects, but it could be avoided as follows:

\documentclass{article}

\makeatletter
\let\conditionalinput\@input
\makeatother

\begin{document}
abc
\conditionalinput{fred.tex}
\end{document}

That said, it's probably better to use \InputIfFileExists{file}{}{}, as suggested by @daleif.

Ian Thompson
  • 43,767
  • 3
    Isn't it easier to just use \InputIfFileExists{file}{}{}? – daleif Jun 29 '17 at 07:08
  • @daleif --- probably, but I didn't know that one. – Ian Thompson Jun 29 '17 at 08:17
  • thanks a lot to everybody!!! \makeatletter ... seems to do the job but i ll also explore the other option.. thanks again – Giulioo Jun 29 '17 at 08:44
  • This has the side effect that @ is treated as a letter during the tokenization of the content of myfile.tex. This might or might not be desirable. – Emil Jeřábek Jun 29 '17 at 16:47
  • I undedrstand that there might be an issue if your included doc has a @? Right? Dont understand details though? Could you pls give me an example? – Giulioo Jun 30 '17 at 07:23
  • @Giulioo --- It's unlikely to have any adverse effects, because most .tex files don't use this symbol (it is common in classes and packages). I have edited my question to show how the issue can be avoided, but you can almost get away with ignoring it. – Ian Thompson Jun 30 '17 at 10:16
  • @Giulioo --- No problem. Note that 'almost' should read 'almost certainly' in my last comment. – Ian Thompson Jun 30 '17 at 15:46