5

How to stop compliation and output an error message if conditional statement resolves to true?

\documentclass[varwidth]{standalone}
\usepackage{expl3}
\begin{document}
  \ExplSyntaxOn
  \bool_set_true:N\l_tmpa_bool % OUTPUT ERROR
  \bool_if:NTF\l_tmpa_bool
    { % OUTPUT ERROR TO CONSOLE
    }
    { NO ERROR
    }
  \ExplSyntaxOff
\end{document}
8

Use \msg_new:nnn(n) to declare an error message, then use \msg_fatal:nn(nnnn) to raise a fatal error message, after which the compilation is aborted (see the l3msg module documentation for more options):

\documentclass[varwidth]{standalone}
\usepackage{expl3}
\begin{document}
\ExplSyntaxOn
% Declare message
\msg_new:nnnn { bp2017 } { logical-error }
  { The~thing~that~wasn't~supposed~to~happen~happened. }
  { I~don't~know~how~to~help~you~further. }%
%
\bool_set_true:N \l_tmpa_bool % OUTPUT ERROR
\bool_if:NTF \l_tmpa_bool
  { \msg_fatal:nn { bp2017 } { logical-error } }
  { NO ERROR }
\ExplSyntaxOff
\typeout{I'm still here!}
\end{document}

After that the terminal shows (note that I'm still here! isn't printed):

! Fatal Package bp2017 Error: The thing that wasn't supposed to happen
(bp2017)                      happened.

For immediate help type H <return>.
 ...

l.14   { NO ERROR }

? h

I don't know how to help you further.

This is a fatal error: LaTeX will abort.

?


phelype@oleinik:~/home/phelype/tex.sx$

However this will not work with standalone because it does everything inside a \vbox (with the varwidth option), and when you use (TeX's) \end in a \vbox you get: ! You can't use `\end' in internal vertical mode. Without varwidth, then standalone uses an \hbox, and then you get a ! Missing } inserted.

Edit: With an updated (June 3rd, 2019) version of expl3, \msg_fatal:nn(nnnn) ends the compilation regardless of where it's called from by issuing the error message and then having the underlying TeX engine stop with a low-level fatal error.

  • 2
    hmm I wonder if it ought to try harder to stop (put itself in batch mode \vbox{\batchmode\end} – David Carlisle May 31 at 18:13
  • @DavidCarlisle I like the idea. It's supposed to stop the engine either way, so I guess it's better than more error messages... – Phelype Oleinik May 31 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.