It's really a form of the same trope as summoning demons by pronouncing certain words/names, or following certain rituals. Or making yourself subject to external influences by reading the Necronomicon. How, rigorously, does a demon know whether you've said its name or not? Really good inter-dimensional hearing? Some kind of universal monitoring system that rushes off and tells it whenever someone says its name, like demonic Echelon? How does it detect from another world, what kind of incense you're using and what sigils you've drawn? Similarly, how does it know what mathematics you're doing?
Part of the point of magic is that the mechanism is not rigorous: if it was then it would be physics, or just a political or commercial interaction with the demon.
But as a general theme, the idea seems to be either that certain energies or entities are attracted to certain words/signs/substances/etc (by some form of sympathy), or else that certain states of mind allow the practitioner to perceive or interact with a world they normally cannot.
Converted to mathematical form, it could be that there's some etheric substance that is pushed around by mathematical symbols, and that a proof of a theorem pushes that substance into a mechanism that then is able to do whatever it takes to pierce the veil. So, proving Fermat's Last Theorem is like building the machine in Sagan's Contact except that you're not building it out of material substance, you're building it out of some magical hand-wavey stuff that exists locally, and responds to local stimulus, but then is capable of a distant effect. In that case, a written proof might be a permanent magical doohickey provided that the symbols continue to hold the machine in shape. You still have to explain how the energy "knows" that the symbol "2" means the number that comes after the number represented by the symbol "1", but perhaps what matters is the structure of the patterns/relationships among symbols, and this explains why the actual symbols used are irrelevant.
Alternatively, it might be that humans happen to be innately equipped with some faculty that draws the attention of demons, but that we're unable to use that faculty except when guided into a series of very specific mental states/visualisations, corresponding to the steps of a proof. In that case, a written proof in a book is nothing, but when someone reads it and it has its intended effect on them (of understanding the proof that it represents) then the magic happens and they start dreaming of unknown Kadath. The proof itself is no more or less magical than the mental exercises used by a Zen archer to shoot a bow, (and vice-versa the shooting exercises used to achieve mental and spiritual development). It's just that in the case of demon-summoning humans are actually so awful at it without the exercises as to be incapable.
Or in a broader sense of the law of sympathy, perhaps there is some entity that definitionally is (or has some direct connection with) the platonic ideal of the number 2. Wherever things appear in pairs, or whenever someone thinks about the mathematical properties of the number 2, there that entity is, just as we might say that Thor exists wherever thunder does, or that silver is connected with the powers/associations of the Moon, or that God is omnipresent. Naturally then demons respond to mathematics: to do mathematics is to touch a part of their multi-dimensional self. It's then up to you whether the demon summoned is the demon of Fermat's Last Theorem specifically, or whether the general fuss and commotion caused in the region of the platonic universe corresponding to the intersection between "elliptic curves" and "modular forms", by proving the Taniyama-Shimura conjecture, is what attracts other demons.
As for the rigorous details of why there is a law of sympathy at all, or what causes there to be a God of Thunder or a demon of Banach-Tarski dissections, or how the force is mediated by which symbols to build etheric machines, or by what mechanism humans have an innate capacity to summon demons or shoot bows: those may have to remain as irreducible principles of your in-fiction theory of magic. No doubt you can drill into some of them by introducing further concepts that dictate them.