TransWikia.com

How to make InputIfFileExists force end the compilation with a descriptive error message when the file is missing?

TeX - LaTeX Asked on February 6, 2021

The following gives me a message saying the Process exited normally with a corrupted output PDF file despite the absence of any file called dummy.

So, I need to have a compilation-halting error with a message about the missing input file.

documentclass{article}
begin{document}
    InputIfFileExists{dummy}{}{}
end{document}

Why can’t I get the intended following message in the log file?

Error: Missing file: dummy

documentclass{article}
begin{document}
    InputIfFileExists{dummy}{}{%
        % https://tex.stackexchange.com/a/377312/2288
        % https://tex.stackexchange.com/a/383190/2288
        typeout{Error: Missing file: #1}
        ExitDueToUndefinedControlSequence
    }
end{document}

One Answer

documentclass{article}

makeatletter
newcommand{thereisnosuchfile}{%
  @latex@error{No file to input}{The file you asked for doesn't exist}%
}
makeatother

begin{document}

InputIfFileExists{dummy}{}{thereisnosuchfile}

input{dummy}

end{document}

What's the difference? In the first case there is no request for another file name. On the other hand, in the second case, just hitting return would exit from the loop by inputting the empty .tex file.

! LaTeX Error: No file to input.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.10 ...tIfFileExists{dummy}{}{thereisnosuchfile}
                                                  
? 

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

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

Enter file name: <<hit return>>
(/usr/local/texlive/2020/texmf-dist/tex/latex/tools/.tex File ignored)

If you add stop at the end of the definition of thereisnosuchfile, the LaTeX run would stop after hitting return.

An expl3 version:

documentclass{article}

ExplSyntaxOn
NewDocumentCommand{saferinput}{m}
 {
  file_if_exist_input:nF { #1 } { msg_error:nnn { diaa } { no-file } { #1 } }
 }
msg_new:nnnn { diaa } { no-file }
 { No~file~'#1' }
 { The~file~'#1'~you~asked~for~does~not~exist }
ExplSyntaxOff

begin{document}

saferinput{dummy}

end{document}

Console output

! Package diaa Error: No file 'dummy'

For immediate help type H <return>.
 ...                                              
                                                  
l.15 saferinput{dummy}
                       
? h

The file 'dummy' you asked for does not exist

? 

Answered by egreg on February 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP