58

I want something like \ifmmode\else\GenerateWarning{The command \backslash uzuka is supported only in math mode.}\fi, which is supposed to generate a warning "The command \uzuka is supported only in math mode.". Is there a command or a package with this capacibility?

3 Answers 3

41

For LaTeX, use the commands described in section 4.9 of clsguide.pdf:

  • \PackageWarning and \PackageWarningNoLine if you are writing a package
  • \ClassWarning and \ClassWarningNoLine if you are writing a class
  • The internal generic version for warnings is \GenericWarning

The expl3 bundle has another interface for warnings and similar messages, see Part III, chapter 11 in source3.pdf.

4
  • 5
    I'd add that the expl3 approach has been designed to allow suppression of messages in a selective fashion. Packages such as silence can do the same for LaTeX2e messages but as an 'add on'.
    – Joseph Wright
    Sep 20, 2010 at 11:43
  • 1
    @doncherry Thanks, fixed. (It's called l3kernel now instead of expl3.)
    – Philipp
    Dec 9, 2012 at 20:52
  • What was Part XVII is now Part XVIII Messaging (I presume).
    – stefanct
    Jul 3, 2019 at 11:18
  • 1
    For error instead of warning, use \PackageError
    – tobiasBora
    Jan 27, 2022 at 17:22
22

Just use the built-in \errmessage for errors, e.g. \let\GenerateWarning=\errmessage.

The kind of warnings Latex generates are just messages; \let\GenerateWarning=\message will not interrupt the compile. Latex uses the commands \@latex@warning and \@latex@warning@no@lines to create these for its own warnings: you check the source you find in latex.ltx.

4
  • Yes, that will allow to generate errors. But what about warnings? Is it possible to generate them too?
    – fiktor
    Sep 20, 2010 at 11:13
  • 5
    Thanks to search using \errmessage keyword I've found in TeXBook how to output warning. Actually I've found, that my editor (TeXnicCenter 1.0) treats as warnings output lines like "LaTeX Warning: \uzuka used outside of math mode on input line 211", which can be generated using \message{LaTeX Warning: \noexpand\uzuka used outside of math mode on input line \the\inputlineno}. Thank you.
    – fiktor
    Sep 20, 2010 at 11:21
  • Relevant: tex.stackexchange.com/q/115976/8666
    – 0 _
    Aug 21, 2017 at 0:55
  • Is it possible to generate an expanded message as well?
    – Someone
    Nov 8, 2020 at 21:47
17

You can use \typeout{msg}. The command will print msg on the terminal and in the log file.

You must log in to answer this question.

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