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

  \bool_set_true:N\l_tmpa_bool % OUTPUT ERROR
    { NO ERROR

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):

% 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 }
\typeout{I'm still here!}

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.



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.

| improve this answer | |
  • 2
    hmm I wonder if it ought to try harder to stop (put itself in batch mode \vbox{\batchmode\end} – David Carlisle May 31 '19 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 '19 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.