3

I am writing a long paper and I have a few variables I use very frequently. They are labelled \z, \Z, \zt, \Zt etc. However I also have a few variables with names 'a' and 'b' in the document which I don't use so frequently. As \a, \b are already defined macros, I have used \avar, \bvar as the macros for these. However while typing my file, I frequently forget that I should use \avar instead of \a. Is there a way for the compiler to give me an error if I use \a, \b in the document? Thanks!

4
  • 1
    Since \a and \b are defined, that means they are defined to do something. If you make them do something else (which is not a good idea in most cases), you might as well make them do what \avar does....
    – jon
    Commented Jan 14, 2017 at 3:58
  • I understand your point. I have no intention of modifying the definition of \a, \b at any point. But is there a way to restrict the use of a macro in some part of the document? The only reason why want to do this is for ease of writing the paper. Whenever I mistakenly write \a, I will get an error and then I change it to \avar and everybody is happy
    – skf23852
    Commented Jan 14, 2017 at 4:05
  • Well, I guess one idea would be to prefix or append an undefined command. E.g., \usepackage{etoolbox} \preto\b{\barf} (where \barf is undefined).
    – jon
    Commented Jan 14, 2017 at 4:12
  • 1
    there is no point in doing this at all! if you decide that it is safe to redefine \a to make an error you may as well have redefined it to make your variable formatting. Commented Jan 14, 2017 at 9:01

1 Answer 1

3

Similar to what is mentioned in Can I redefine an environment to generate an error?, you can add the following to your preamble:

\documentclass{article}

\AtBeginDocument{
  \renewcommand{\a}{\GenericError{}{Don't use \string\a!}{}{}}
  \renewcommand{\b}{\GenericError{}{Don't use \string\b!}{}{}}
}

\begin{document}

This is a test. \a{} and \b.

\end{document}

You must log in to answer this question.

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