# Preamble vs. external style file - same code doesn't work in the later

I'm experiencing the following issue when using todonotes along with TikZ and its externalize library. If my document contains no \todo's then it compiles just fine and seems like the externalization of TikZ does it trick. However, once there are \todo's in the document, then some of them fail the compilation.

If I surround such a problematic \todo as follows:

\begingroup
\tikzexternaldisable
\todo{Note's content}%
\tikzexternalenable
\endgroup

Then the compilation runs smoothly again. So, the next step is naturally to re-define the \todo. Thus, in the preamble I added the following:

\makeatletter
\renewcommand{\todo}[2][]{%
\begingroup \tikzexternaldisable
\@todo[#1]{#2} \tikzexternalenable
\endgroup}
\makeatother

and everything seems to be right. The problem I have is that I stuffed all my preamble in a separated .sty file. When I move the last snippet to the .sty file then the problem occurs again. (The last snippet is coming from this answer). Otherwise, the .sty file loads correctly, as far as I can tell.

Summary: My question is why the same code when it is in the "main" file behaves as expected, but when it is moved to a style file which is loaded (correctly) it doesn't work anymore. I guess it has nothing to to in particular with neither TikZ nor todonotes - but I mention them as they caused a problem in my case.

-
What happens if you put only \tikzexternalize in your document instead of the style file? –  T. Verron Oct 24 '12 at 8:51