how to prevent \input from failing if the file is missing?

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


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.

Tags:

Input