0

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}
Diaa
  • 9,599
  • you should get no error from that, please show the complete log file. (I get no error with texlive 2020 pdflatex) – David Carlisle Dec 23 '20 at 11:35
  • the answer to the question though would be to use \input instead of \InputIfFileExists as \input will give an error for a missing file, the point of \InputIfFileExists is exactly to avoid the error. – David Carlisle Dec 23 '20 at 11:41
  • @DavidCarlisle It will be even worse; \documentclass{article} \begin{document} \input{dummy} \end{document} doesn't stop when building with lualatex.exe -synctex=1 -shell-escape -file-line-error -halt-on-error "filemissing".tex – Diaa Dec 23 '20 at 11:50
  • for the version in the comment you could use -interaction=nonstopmode instead of \halt-on-error but you have still provided no log for the version in the question. That should run to completion with no error at all. – David Carlisle Dec 23 '20 at 11:54
  • The version in question didn't stop. I had to hit stop button of texstudio. – Diaa Dec 23 '20 at 11:57
  • that is really weird. as I say show the log file, that should not happen. It should finish cleanly (with no pdf output) – David Carlisle Dec 23 '20 at 11:58
  • @DavidCarlisle I am sorry, it stops now :D. Here it is https://pastebin.com/7ranuvvL – Diaa Dec 23 '20 at 12:09
  • That looks as expected:-) No error or corrupted PDF, just warning (pdf backend): no pages of output. – David Carlisle Dec 23 '20 at 12:15
  • @DavidCarlisle Before you asked for the log, it didn't stop but no one would believe me :D – Diaa Dec 23 '20 at 12:16
  • OK I will take credit for fixing your document, then we can all agree it's fixed now. and egreg gets his 15 points and a green tick, so we are all happy. – David Carlisle Dec 23 '20 at 12:21
  • @DavidCarlisle His expl3 solution is great, but I would like to have the solution in the old school way with typing the missing file name in the log. – Diaa Dec 23 '20 at 12:25

1 Answers1

1
\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

?

egreg
  • 1,121,712
  • I would be grateful if you could take a look at my question update and explain why I can't the find that message in the log file. Thanks – Diaa Dec 23 '20 at 12:04
  • @Diaa You can't use #1 in that place. – egreg Dec 23 '20 at 12:07
  • I just followed this answer https://tex.stackexchange.com/a/383190/2288. If you can correct it, kindly do. – Diaa Dec 23 '20 at 12:08
  • @Diaa No, that's a different situation. You cannot refer to the first argument to \InputIfFileExists in the third argument by #1; that code is defining a new command where the file name is passed as argument. – egreg Dec 23 '20 at 12:10
  • I understand. Is there a reason why I get Package diaa Error: No file 'dummy' in the log file without the second sentence The file 'dummy' you asked for does not exist? – Diaa Dec 23 '20 at 12:14
  • @Diaa That only pops out if you hit h at the error prompt. – egreg Dec 23 '20 at 12:18
  • Can the first solution be edited to pass the missing file name to \thereisnosuchfile to show up in the log file? I need it to show up in the first message No file to input since I am using an editor where I can't press buttons after the error. – Diaa Dec 23 '20 at 12:35
  • 1
    @Diaa What's the reason in insisting to use \InputIfFileExists in the document? It's not thought to be used in the document! Define \saferinput in those terms and you can pass the argument. – egreg Dec 23 '20 at 12:41