I am writing a long latex document and use many macros for defining mathematical symbols. I would like to ensure that there is no clash in the notation. For example, something like


should not happen. Is there something like an assertion in latex such that I can do

\assert{\symbolone != \symboltwo} 


  • 7
    Welcome to TeX.SX! Nice first question. You could make it even better by adding a small minimal working example (MWE) that we can start working with for testing solutions. – Paul Gessler Feb 16 '15 at 20:40
  • There is no “register” of all defined macros, so it's impossible to test each pair to see whether they do the same thing. – egreg Feb 16 '15 at 21:16
  • 2
    You can test if two macros have the same definition (with \ifx) but for example if \symbolone is \hat{A} and \symboltwo is \hat A` they are not equal with \ifx, but typeset to the same thing in all cases. So it is difficult to have a robust test that matches common expectations. – David Carlisle Feb 16 '15 at 21:19
  • Welcome to TeX.SX! Usually, we don't put a greeting or a “thank you” in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say “thank you” to users who helped you. – Martin Schröder Feb 17 '15 at 10:49

My solution introduces the macro \singledef which defines control sequence (like \def) but provides two more features:

  • it checks if the control sequence is defined. If yes, then error message is printed and the control sequence keeps its original meaning.
  • it saves the name of defined control sequence into the \singledeflist internal macro. When the next control sequence is defined via \singledef then it is defined but it is checked if the same control sequence (equivalent via \ifx) was defined and saved in \singledeflist. If yes, then error message is printed and the new control sequence is kept undefined.

The code:


      \errmessage{The \string#1 is defined already}% 
         \errmessage{The \string#1 is the same as \string#2}%

\singledef\A{aha}  % this is like \def\A{aha}
\singledef\B{bha}  % this is like \def\B{bha}
\singledef\C{bha}  % this prints the error: The \C is the same as \B
| improve this answer | |

@egreg has shown how to compare two fully expanded strings (On testing two fully expanded character strings for equality), and you could apply his technique here.

I have added an error message in case you reuse a symbol.


\newcommand{\symbolII}{\pi r^2}
\newcommand{\symbolIII}{\pi r^2}

            ERROR: \noexpand#2 (#2) is the same as \noexpand#1.%



\[ \symbolI = \symbolII \]

| improve this answer | |
  • 1
    as egreg warns in the referenced answer this isn't really safe for anything other than letters, try \newcommand{\symbolIII}{\sqrt{x}} – David Carlisle Feb 16 '15 at 21:37
  • What's \expandafter for? – egreg Feb 16 '15 at 22:29
  • Well, I wanted to typeout the expansion of #2 and didn't realize it happened automatically. Thanks--I fixed it. – musarithmia Feb 16 '15 at 22:45

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.