Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

To force myself and collaborators to only use AMS-Math commands, I laod 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}
share|improve this question
1  
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

4 Answers 4

up vote 11 down vote accepted

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.

share|improve this answer
    
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.

share|improve this answer

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}
share|improve this answer

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.)

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.