2

When I need to use something repeatedly within a document, I make my own command. E.g.:

\newcommand{\notification}[2]{      % line 2
    Hello #1, your #2 pie is ready. % line 3
}                                   % line 4

Then I can use it in the document like so:

\notification{Mr. White}{pumpkin}   % line 6

However, if I make some mistake while coding the newcommand, it does not say "l.3"; it will say "l.6". See in the example below (I have added an extra "&"):

\documentclass{article}
    \newcommand{\notification}[2]{
        Hello #1, your #2 & pie is ready.
    }
\begin{document}
    \notification{Mr. White}{pumpkin}
\end{document}

The error message is as follows:

! Misplaced alignment tab character &.
\notification #1#2-> Hello #1, your #2 &
                                         pie is ready. 
l.6     \notification{Mr. White}{pumpkin}

In this case, it is simple to diagnose, because I can quickly recognize the line Hello #1, your #2 pie is ready. and find it with a search; however, in more complex documents, Hello #1, your #2 pie is ready. might appear dozens of times, so I don't know where to look; or the reported error displays other code (which I will assume is underlying TeX code) not actually included within any of the code I wrote, so the messages seem of little help.

What are some general steps one can take to aid in the identification of the source of errors of this type?

1
  • Something like \foo #1…#9-> is always the expansion of a macro, so you have to look not only at the line number given by l.<nr>, but also at the definition of the macro. But if you are searching for a something like symbolic TeX debugger: You are not the only one, but there isn't such a tool. Nov 15, 2011 at 14:09

1 Answer 1

5

I think of this less as a LaTeX issue and more as a work flow one:

  • Whenever I define a newcommand, I test it on a few samples before using it in a real world document. Keep a scratch file if you're writing a lot of them so you don't get a bad aux file contributing to confusion.

  • If I edit an existingnewcommand, I edit only one at a time. So if an error sneaks in, I know where it came from.

  • Reuse newcommands and put comments next to them if they require particular packages or TikZ libraries. I have something like 30 macros that I created years ago for my thesis. I make special ones for particular documents, but reusing code obviously minimises the chances of an error getting in.

  • I know this is a not-so-helpful answer, but it does avoid problems and I never get errors from badly coded macros.

5
  • 2
    A useful simple tool for debugging macros is to set \errorcontextlines=10 so that TeX shows more expansion context and it's sometimes easier to guess at what macro went wrong.
    – egreg
    Nov 15, 2011 at 14:13
  • Oooh. Nice! You should throw that in as an answer!
    – qubyte
    Nov 15, 2011 at 14:15
  • You can add it to your answer. It's just the first trick one should use before going to the big guns such as \tracingmacros=1.
    – egreg
    Nov 15, 2011 at 14:20
  • Nah, I'm maxed out today anyway. The best I'd get is 15 rep. ;) Ahem. I mean, credit where credit is due, eh?
    – qubyte
    Nov 15, 2011 at 14:22
  • FWIW, ConTeXt sets \errorcontextlines = 5 in the format...it does make debugging so much easier.
    – Aditya
    Nov 15, 2011 at 22:51

You must log in to answer this question.

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