# How can I control which file errors are reported in?

Here is a class that can generate errors in two ways. It yields an error unless given one of two options, and also it has a command that always yields an error.

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{foo}[2014/05/06 foo]

\newif\iffoo@foo
\newif\iffoo@bar
\DeclareOption{foo}{\foo@footrue}
\DeclareOption{bar}{\foo@bartrue}
\ProcessOptions

\iffoo@foo
\AtBeginDocument{foo}
\else
\iffoo@bar
\AtBeginDocument{bar}
\else
\PackageError{foo}{Always use foo or bar option}{Please}
\fi
\fi

\newcommand*{\foobar}{
\PackageError{foo}{The \protect\foobar\space command is obsolete}{}}
\endinput


The document

\documentclass{article}
\usepackage{foo}

\begin{document}

hello
\foobar

\end{document}


triggers them both:

! Package foo Error: Always use foo or bar option.

See the foo package documentation for explanation.
Type  H <return>  for immediate help.
...

l.16 ...foo}{Always use foo or bar option}{Please}


and

! Package foo Error: The \foobar command is obsolete.

See the foo package documentation for explanation.
Type  H <return>  for immediate help.
...

l.7 \foobar


I would like the first one to be more like the second one. That is, I'd like it to indicate the \usepackage{foo} line instead of a line in the sty file. After all, that's what the user of the class should edit.

How can I control that?

-

You can't control it, the last line of the TeX error message is issued by the underlying TeX engine showing where it was when an error is detected. It is not under the control of the macro layer

The only thing you can do is change where the error is detected, For example if you use

\AtBeginDocument{\PackageError{foo}{Always use foo or bar option}{Please}}


Then you get

! Package foo Error: Always use foo or bar option.

See the foo package documentation for explanation.
Type  H <return>  for immediate help.
...

l.4 \begin{document}

-
Ah! And with \AtEndOfPackage instead I get at least close to the goal, but unfortunately the line after. –  pst May 6 '14 at 13:36
@pst latex has got to the next line looking for an optional argument before running the code the user would have to put and \relax after \usepackage to tell latex not do that, but that's not really a feasible option to expect that) –  David Carlisle May 6 '14 at 14:29
I don't get the \relax part right. Can you please show that in your answer? –  pst May 6 '14 at 14:41
(I meant to ping @DavidCarlisle in my last comment.) –  pst May 7 '14 at 10:39
@pst it's not usable here really, I was thinking about the comment about \relax at the bottom of page 19 of texdoc clsguide but here if you wanted the error to be on the usepacakge line rather than the line after, the user would have to do \usepackage{yourpackage}\relax but if the user knew to do that they know enough not to make the error.... –  David Carlisle May 7 '14 at 10:49