# Conflict: onlyamsmath and TikZ

To force myself and collaborators to only use AMS-Math commands, I load this package in the preamble:

\usepackage[all,warning]{onlyamsmath}


However, when this is combined with TikZ, I get into trouble when using TikZ's calc library, as this involves using $ for computations. How can I resolve this conflict? MWE: Try uncommenting onlyamsmath \documentclass{article} \usepackage[utf8]{inputenc} \usepackage[T1]{fontenc} \usepackage{tikz} \usetikzlibrary{positioning,calc} \usepackage[all,warning]{onlyamsmath} \begin{document} \begin{tikzpicture} \node (node1) [] {Box 2 text}; \node (node2) [right=of node1] {Box 3 text}; \draw[->] (node2.north) -- ++(0,0.5) -| ($ (node1.east) + (0.25,0) $); \end{tikzpicture} \end{document}  - It is always best to compose a MWE that illustrates the problem including the \documentclass so that those trying to help don't have to recreate it. – Peter Grill Oct 17 '11 at 20:14 ## 5 Answers TikZ 3.0.0 has a babel library intended for compatibility with the babel package, that fixes catcodes within tikzpicture, and seems to also solve the onlyamsmath compatibility issue for me. Note: as cfr points out, this does of course still disable onlyamsmath checking within TikZ pictures \documentclass{article} \usepackage[all,warning]{onlyamsmath} \usepackage{tikz} \usetikzlibrary{calc, babel} \begin{document} \begin{tikzpicture} \draw (0,0) coordinate (A) node[below] {inline equation:$a^b$} -- (4,0) coordinate (B) node[below, text width=3cm] { Checking does still occur within nodes, so this produces a warning: $$a^b$$ } -- ($ (A)!0.5!(B)!sin(60)*2!90:(B) $) node[above] {calculated point} -- cycle; \end{tikzpicture} \end{document}  From the TikZ manual: 12.2.3 Handling Catcodes and the Babel Package Inside a TikZ picture, most symbols need to have the category code 12 (normal text) in order to ensure that the parser works properly. This is typically not the case when packages like babel are used, which change catcodes aggressively. To solve this problem, Tik Z provides a small library also called babel (which can, however, also be used together with any other package that globally changes category codes). What it does is to reset the category codes at the beginning of every {tikzpicture} and to restore them at the beginning of every node. In almost all cases, this is exactly would you would expect and need, so I recommend to always load this library by saying \usetikzlibrary{babel}. For details on what, exactly, happens with the category codes, see Section 43. - Welcome! This is a neat use of the library ;). You should probably point out that this will disable checking within TikZ pictures, though. – cfr Aug 30 '15 at 21:09 Added a note to that effect, thanks for pointing it out! – nar8789 Aug 30 '15 at 21:20 Can't upvote again :(. – cfr Aug 30 '15 at 21:22 Actually, taking another look... TikZ babel does make sure to restore catcodes within nodes, so I think checking does still happen there... is there anywhere else within a tikzpicture where checking would be important? – nar8789 Aug 30 '15 at 21:32 It doesn't seem to work with the quotes library: I don't get warnings with $$ in that case. It does seem to work with regular labels and nodes on edges if quotes aren't used, though. – cfr Aug 30 '15 at 21:54 You could adjust the catcode of the  within the tikzpicture environment as follows: \documentclass{article} \usepackage[all,warning]{onlyamsmath} \usepackage{tikz} \usetikzlibrary{calc} \makeatletter \let\@@tikzpicture\tikzpicture \def\tikzpicture{\catcode\=3 \@@tikzpicture} \makeatother \begin{document} \begin{tikzpicture} \draw [red,ultra thick] (0,0) -- ((2,0)+(2,2)); \end{tikzpicture} \end{document}  I have tested this solution (as well as @egreg's) with your MWE and both solutions seem to work fine. - Works. Thanks a lot Peter and eqreq! – Ingo Oct 17 '11 at 20:33 The two packages are indeed incompatible, as onlyamsmath changes  into an active character in order to check whether $$...$$ is used. This conflicts heavily with the workings of tikz. However, the things checked by the package are eqnarray eqnarray* displaymath$$  so it doesn't seem to be really necessary. However, the following code \usepackage{etoolbox} \preto\tikzpicture{\catcode$=3 }
\preto\tikz{\catcode$=3 }  inserted after loading TiKZ may solve the incompatibility. - The solutions from Peter Grill and egreg and jofel involve turning off onlyamsmaths checking in either a tikzpicture (Peter Grill and egreg) environment or the whole document (jofel). However, assuming that one wants to use the full capability of onlyamsmath in the entire document, none of these solutions is quite right. The problem is that onlyamsmath changes the catcode of $ at \begin{document}. So when TikZ is loaded it doesn't know that $ will change its catcode later and so merrily sets up stuff matching $ with its current catcode, namely 3.

Now, because TikZ relies a lot on special symbols (; is only the start of it), it generally is quite robust against catcode changes. If you look at the source of tikz.code.tex you'll see a lot of \ifnum\catcode\;=\active and similar (robust but not steadfast: it only checks for a change between whatever-was-in-use-at-load-time and active). It doesn't do this for $, though. Perhaps it should. One possibility would be to add the necessary checks. This wouldn't be hard since, despite egreg's assertion, $s are not heavily used in TikZ so there would only need to be a few changes made. However, a quicker alternative is to ensure that the right catcode is in place when TikZ is loaded. This can be done with:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\edef\dolcat{\the\catcode\$} \catcode\$=13
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\catcode\$=\dolcat\relax \usepackage[all,warning]{onlyamsmath} \begin{document} \begin{tikzpicture} \node (node1) [] {Box 2 text}; \node (node2) [right=of node1] {Box 3 text}; \draw[->] (node2.north) -- ++(0,0.5) -| ($ (node1.east) + (0.25,0)
$); \end{tikzpicture} \end{document}  which works with your MWE and also complains about $$ in TikZ nodes. (Whilst I agree with egreg about the necessity of onlyamsmath there are plenty of packages that mess with catcodes of symbols of significance and I think that adjusting TikZ - or faking it - to play nicely is the better route to take when conflicts arise.) - Nice answer; however  is heavily used in the calc library. :) My assumption was that people who use $$...$$ in a TikZ node are on their own. :) – egreg Oct 23 '12 at 11:24 @egreg Depends on your definition of "heavily used". I was looking at the code of the library. There are three macros that have  in the parameter text (or body) which would need an alternative with  active. There are three more in tikz.code.tex. So fixing it in the code would not be hard. That's what I meant by "heavily used". – Loop Space Oct 23 '12 at 11:30 @AndrewStacey if you provide a patch against the current TikZ version to accept active dollars in the calc lib, I would commit that to tikz CVS (please include suitable test cases). – Christian Feuersänger Feb 24 '13 at 19:56 The $$ protection from onlyamsmath makes lots of problems, not only with tikz (see e.g. this question). To disable this incompatible feature, but not the other protections from onlyamsmath, add to your preamble after loading onlyamsmath. \AtBeginDocument{\catcode\$=3}

-