[Tex/LaTex] Rationale to use \input{filename} on the pdflatex command line

best practicesinput

In a tex.SX user profile, I read the best practice recommendation

Execute pdflatex \input{filename} instead of pdflatex filename.

I have always used the latter one, directly, in scripts, and in Makefiles.

What would be a reason to always use pdflatex \input{filename} instead?

Best Answer

Advantages

  • It is a good trick, if someone wants to put TeX code before. Examples:

    pdflatex '\def\foo{bar}\input{filename}'
    pdflatex '\includeonly{introduction}\input{filename}'
    

    or inside \write18 (shell escape feature):

    \immediate\write18{\detokenize{pdflatex '\def\foo{bar}\input{filename}'}}
    \immediate\write18{\detokenize{pdflatex '\includeonly{introduction}\input{filename}'}}
    

    e-TeX's \detokenize prevents the expansion of the macros inside \write, see question.

  • Also LaTeX catches the error message a little better, because it checks for the file first and throws a proper error message.

    The TeX behaviour of '\input' without braces or on the command line would be:

    pdflatex filename
    
    ! I can't find file `filename'.
    <*> filename
    
    (Press Enter to retry, or Control-D to exit)
    Please type another input file name: 
    

    Then you can escape from there with Control-D (Unix) or Control-Z (Windows). or the file x.tex of LaTeX's tools bundle can be used for this purpose.

    LaTeX throws instead:

    pdflatex '\input{filename}
    
    ! LaTeX Error: File `filename.tex' not found.
    
    Type X to quit or <RETURN> to proceed,
    or enter new name. (Default extension: tex)
    
    Enter file name: 
    

    Then LaTeX already looks for x or X as answer to abort the job.

    Answer to a comment: However, in both variants an error message is thrown, if the file does not exist. An interactive prompt in a command chain (pdflatex test && do something) can be avoided by the pdfTeX options interaction=batchmode or --interaction=nonstopmode.

Disadvantages

  • It's longer than filename.
  • The backslash needs special treatment (quoting, doubling) with many shells.

Summary

IMHO the two described "advantages" are indeed too weak for always using pdflatex \input{test} over pdflatex test. At least I have tried to find possible reasons. And the trick with putting TeX stuff before \input is worth to be remembered.

Related Question