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

5
  • Welcome to TeX - LaTeX! Because of the special character @ in the command name you need to write \makeatletter\@input{...}\makeatother. Jun 29, 2017 at 6:55
  • @AndrewSwann --- quicker by 6 seconds. If you make your comment into an answer, I will delete mine. Jun 29, 2017 at 6:57
  • See tex.stackexchange.com/q/6240/15925 Jun 29, 2017 at 6:59
  • 2
    \InputIfFileExists{file}{then}{else}, you can just leave then, else blank. For details see texdco source2e
    – daleif
    Jun 29, 2017 at 7:08
  • @daleif Please, add an answer
    – egreg
    Jun 29, 2017 at 8:50

2 Answers 2

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.

7
  • 3
    Isn't it easier to just use \InputIfFileExists{file}{}{}?
    – daleif
    Jun 29, 2017 at 7:08
  • @daleif --- probably, but I didn't know that one. Jun 29, 2017 at 8: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, 2017 at 8: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. Jun 29, 2017 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, 2017 at 7:23
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

10
  • 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, 2017 at 7: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, 2017 at 9: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, 2017 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, 2017 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, 2017 at 12:44

You must log in to answer this question.

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