1

I'm defining a macro using \addcontentsline. However, I have no idea whether that command is robust or not. If it's fragile , \protect is mandatory. I have no idea whether a duplicate \protect, which \protecting a robust command would result into, can produce any problems, so I assume it might do something just to be safe. Google just won't understand my search, so can someone help me on that detail?

In case someone is interested, I define a command that basically generalizes \chapter, \section etc. by putting the header level into a parameter pretty much like \addcontentsline does, however printing both to ToC and to the text like the sectioning commands do. It also allows part level. I already know how to basically do it, but need to know that little detail to actually program it out. (I've also yet to figure out how to get the header sizes for the levels, but I should already find info on that.)

  • \protect only makes a difference in a moving argument and it's hard to think that you would ever need \addcontentsline in such an argument. – David Carlisle Dec 3 '17 at 19:56
3

\protect is not mandatory, or even recommended, with fragile commands, it is only needed if the command is used in a "moving argument". The most useful definition of a "moving argument" is slightly tautologous in this context as "an argument where \protect is needed) but basically moving arguments are arguments that get expanded or written to auxiliary files etc. So the argument to a section heading is a moving argument as it is written to the table of contents, but \addcontentsline itself is not likely to be used in such a context and so the question of whether it is robust or not doesn't really matter (as \protect is just \relax and does nothing in contexts other than moving arguments.)

Technically it is fragile, but if you are using it where \protect is not \relax, then something is probably wrong with the expansion order in the code, so prefixing by \protect is unlikely to ever be useful.

  • If it's fragile, I'll probably insert \protect just in case the expansion side of \newcommand (where I obviously use it, as I define a macro expanding to it) is a moving argument. – Egor Hans Dec 3 '17 at 20:40
  • 1
    @EgorHans as I tried to explain, this is not really a good idea. Although most likely it makes no difference, if it ever does make a difference, something else is wrong and adding \protect here wouldn't be the right fix – David Carlisle Dec 3 '17 at 20:47
  • @EgorHans \newcommand does no expansion. – David Carlisle Dec 3 '17 at 20:49
  • OK, that's good to know. \newcommand is safe on that matter, noticed and memorized. – Egor Hans Dec 4 '17 at 7:46
0

After asking this, I browsed some further and found this question, the accepted answer on which seems to imply that \protecting a robust command shouldn't do anything bad. In fact, that answer even suggests \protecting all macros in case of error, which most definitely wouldn't be failproof if \protect weren't safe on robust commands. So having that information, my question becomes obsolete, as I know I'm on the safe side with \protect.

  • In case you wonder, I found the answer. I just can't accept my own answer yet. – Egor Hans Dec 3 '17 at 19:51

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.