13

When I run

\documentclass{article}
\def\pet{dog}
\errmessage{My \pet\ is {sick}}
\begin{document}
My \pet\ is {sick}.
\end{document}

the error message is:

My dog\ is {sick}.

but the PDF output reads:

My dog is sick.

What's going on here? Why is \pet expanded, but \␣ isn't? Why are those braces printed in the error message? Shouldn't I have to write \{ and \} to get braces?

1
  • 1
    Meta: <code>\&nbsp;</code> prints as \ . (I like \␣, too.) – Qrrbrbirlbel May 24 '13 at 18:16
10

The document clsguide.pdf, in section 4.9 mentions \space, which is not (that is backslash followed by a space).

\newcommand{\foo}{FOO}
   \PackageError{ethel}{%
      Your hovercraft is full of eels,\MessageBreak
      and \protect\foo\space is \foo
   }{%
      Oh dear! Something's gone wrong.\MessageBreak
      \space \space Try typing \space <return>
      \space to proceed, ignoring \protect\foo.
}

Don't confuse \space with ; the former expands to a space, the second one is unexpandable; you find, in the LaTeX kernel,

\def\space{ }
4

Not quite an answer to the question, but related enough to warrant writing it. LaTeX3 provides ways to format nicely messages. In particular, within the command \iow_wrap:nnnN (and hence within all messages), is defined to expand to a space (just like \space). The list of available formatting commands is currently

  • expands to a space
  • \{ expands to a {
  • \} expands to a }
  • \# expands to a #
  • \% expands to a %
  • \~ expands to a ~
  • \\ goes to the next line
  • \iow_indent:n takes one argument and indents it by four spaces.

Example (well, I should probably have made \rescale do something rather than always throwing an error...).

\documentclass{article}
\usepackage{expl3, xparse}
\ExplSyntaxOn
\msg_new:nnnn { mypkg } { percentage-too-high }
  { The~fuction~\token_to_str:N\rescale\ cannot~scale~more~than~1000\% ! }
  {
    You~asked~to~rescale~the~picture\\\\
    \iow_indent:n {#1} \\ \\
    by~#2\%,~but~this~is~impossible.
  }
\NewDocumentCommand{\rescale}{mm}
  { \msg_error:nnnn { mypkg } { percentage-too-high } {#1} {#2} }
\ExplSyntaxOff
\begin{document}
  \rescale{foobar}{3250}
\end{document}
2

What do you think \edef\temp{My \pet\space is sick} gets expanded to?

\documentclass{article}
\def\pet{dog}
\begin{document}
  \edef\temp{My \pet\ is {sick}}
  \meaning\temp
  \show\temp
\end{document}
\temp=macro:->My dog\ is {sick}.
2

According to [clsguide.pdf, §4.9], the correct way to enter a space in error messages is to write \space rather than \␣.

To understand what's going on here, one must bear in mind that \errmessage outputs to a text console, while most LaTeX commands output to the pdf being produced. Commands like \␣ are already fully expanded; all that's left to do is to execute them. But executing this sort of command doesn't make sense in a text-only situation. Hence, only a few commands, like \space, are programmed to make sense inside \errmessage.

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.