[Tex/LaTex] How to make nonstopmode really not stop on a missing input file

errorsinput

I have a file test.tex with these contents:

\input{missing}
\input{present}

When I run PDFLaTeX with --interaction=nonstopmode it stops after the first missing input file:

$ pdflatex --interaction=nonstopmode test
This is pdfTeXk, Version 3.1415926-1.40.9 (Web2C 7.5.7)
 %&-line parsing enabled.
entering extended mode
(./test.tex
LaTeX2e <2005/12/01>
Babel <v3.8l> and hyphenation patterns for english, usenglishmax, dumylang, nohyphenation, german-x-2008-06-18, ngerman-x-2008-06-18, ancientgreek, ibycus, arabic, basque, bulgarian, catalan, pinyin, coptic, croatian, czech, danish, dutch, esperanto, estonian, farsi, finnish, french, galician, german, ngerman, monogreek, greek, hungarian, icelandic, indonesian, interlingua, irish, italian, latin, lithuanian, mongolian, mongolian2a, bokmal, nynorsk, polish, portuguese, romanian, russian, sanskrit, serbian, slovak, slovenian, spanish, swedish, turkish, ukenglish, ukrainian, uppersorbian, welsh, loaded.

! LaTeX Error: File `missing.tex' not found.

Type X to quit or <RETURN> to proceed,
or enter new name. (Default extension: tex)

Enter file name: 
! Emergency stop.
<read *> 

l.1 \input{missing}
                   ^^M
!  ==> Fatal error occurred, no output PDF file produced!
Transcript written on test.log.

How can I make PDFLaTeX continue even when missing.tex is not there? I would like to do this with either command-line options or code I can put at the beginning of any arbitrary source file, not changing the body of the source file.

Best Answer

The LaTeX macro

\IfFileExists{<filename>}{<true code>}{<false code>}

is what you're looking for in the following way:

\IfFileExists{missing.tex}{\input{missing}}{}

An abbreviated version of the above exists in the form:

\InputIfFileExists{<filename>}{<before code>}

In short, the above defaults to

\IfFileExists{<filename>}{<before code>\input{<filename>}}

Edit: In your comment, you mention that you are interested in appending .tex to missing if it is not supplied and only then. To that end, the xstring package provides \IfEndWith{<string>}{<substr>}{<true code>}{<false code>} that executes <true code> if <string> ends with <substr>, and <false code> otherwise. I used .tex for <substr> in the following code:

\usepackage{xstring}% http://ctan.org/pkg/xstring
\let\OldInputIfFileExists\InputIfFileExists
\renewcommand{\InputIfFileExists}[2]{%
  \IfFileExists{#1}%
    {\OldInputIfFileExists{#1}{#2}}%
    {\IfEndWith{#1}{.tex}{\typeout{INPUT #1}}{\typeout{INPUT #1.tex}}}%
  }