3

Some programming languages have exception handling structures, namely try-catch blocks. One that I know to give an example it Matlab. Consider the following pseudocode masterpiece:

try
    command_that_will_throw_error
catch the_error
    disp('The command_that_will_throw_error has finished unexpectedly! D=')
end

disp('But the show can never stop!')

The program tries to execute the command_that_will_throw_error, fails, stores information about the error in the_error, and then passes the execution to the catch part of the block, so the result of the code above will be:

The command_that_will_throw(error) has finished unexpectedly! D=

But the show can never stop!

My curiosity induced question is: can (La)TeX do something like this?

My guess is that the answer is no, but the last time I said this, @egreg proved me wrong, so...


Before someone asks, what I'm trying to do is to programatically include some pdf files in my document, some of which may not exist. I would like to make TeX just skip them or print something if said pdf file does not exist.

I accept an answer to my actual problem with the pdf files, but I would to know about the error handling issue for general cases.

5
  • You can check if the file exists beforehand. That will make this try-catch-mechanism superfluous.
    – TeXnician
    Jan 30, 2018 at 16:44
  • @TeXnician 1) How do I do that? 2) As I said, this question came to my mind because of this problem, but I would like to know if such thing exist. Sheer curiosity :) Jan 30, 2018 at 16:46
  • Perhaps related.
    – user121799
    Jan 30, 2018 at 16:46
  • 3
    The general problem cannot be solved in TeX: you'd need to implement TeX in TeX itself.
    – egreg
    Jan 30, 2018 at 16:48
  • @egreg one could write something up with shell-escape, I guess :) But I doubt it is worth-it or robust...
    – Skillmon
    Jan 30, 2018 at 16:49

1 Answer 1

4

To your actual problem: You can use a simple command to include pdf files, which does an existence check for you:

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand { \includemypdf } { m }
    {
        \file_if_exist:nTF { #1 }
            {
                % do some cool stuff here
            }
            {
                % maybe issue a warning in the log
            }
    }
\ExplSyntaxOff

\begin{document}
\includemypdf{example-image.pdf}% exists
\includemypdf{quack.pdf}%does not exist
\end{document}
0

Not the answer you're looking for? Browse other questions tagged .