Problem with babel and tikz using \draw

I'm trying the next code:

\documentclass[pdftex,spanish]{article}
\usepackage{babel}
\usepackage{tikz}

\begin{document}

Simple arrow:

\begin{tikzpicture}
\draw[->] (-1.5,0) -- (1.5,0);
\end{tikzpicture}

\end{document}


but it doesn't work. It throws the errors:

• ! Argument of \language@active@arg> has an extra }.
• ! Paragraph ended before \language@active@arg> was complete.<to be read again>\par \draw[->]

What can I do if it works right without the babel package?

-
It seems that \usepackage[english]{babel} works, but \usepackage[spanish]{babel} does not. Also, not sure why you have [pdftex,spanish] options for \documentclass - FYI, I am not that familiar with babel. –  Peter Grill Mar 20 at 23:08
Add \shorthandoff{>}\shorthandoff{<} after \begin{document}. < and > are active characters in Spanish. –  Malipivo Mar 20 at 23:13
@PeterGrill using the pdftex option is not necessary with modern LaTeX systems (the driver is automatically detected). Using the idiomatic localization (spanish, in this case) as class option has the advantage that all language-sensitive packages (for example, babel, cleveref) that can eventually be loaded will pick it and produce the idiomatic localizations. –  Gonzalo Medina Mar 20 at 23:49
Thanks everyone! –  Mario Mar 21 at 0:52
@Mario I've updated my answer with new information that might be of interest for you. –  Gonzalo Medina Mar 21 at 12:27

The problem is that the spanish module for babel makes > and < active characters for its special management of Spanish quotations.

Update:

With PGF/TikZ version 3.0.0 there's a babel library that solves the problems produced by changes to category codes made by some babel modules, so it's enough to load the library:

\documentclass[spanish]{article}
\usepackage{babel}
\usepackage{tikz}
\usetikzlibrary{babel}

\begin{document}

Simple arrows:

\begin{tikzpicture}
\draw[->] (-1.5,0) -- (1.5,0);
\end{tikzpicture}

\tikz\draw[->] (-1.5,0) -- (1.5,0);

\end{document}


(Thanks to Claudio Fiandrino for pointing this out in his comment).

PGF/TikZ version 2.10

To deactivate the undesired redefinition for tikzpictures, but maintaining it for other parts of the document, you can use

  execute at begin picture={\deactivatequoting},
execute at end picture={\activatequoting}


for every tikzpicture. Notice that this approach has two advantages:

1. No extra packages are required.

2. It also produces the desired result when using the command version \tikz (see example code below).

The code:

\documentclass[spanish]{article}
\usepackage{babel}
\usepackage{tikz}

\tikzset{
every picture/.append style={
execute at begin picture={\deactivatequoting},
execute at end picture={\activatequoting}
}
}
\begin{document}

Simple arrows:

\begin{tikzpicture}
\draw[->] (-1.5,0) -- (1.5,0);
\end{tikzpicture}

\tikz\draw[->] (-1.5,0) -- (1.5,0);

\end{document}


As a side note, don't use pdftex option; modern LaTeX systems detect the driver automatically.

-
With TikZ 3.0.0 there is a babel library on purpose: see Tikz shortandoff-on newcommand problem. –  Claudio Fiandrino Mar 21 at 6:19
@ClaudioFiandrino Ah, that's very useful information, thank you! I updated my answer accordingly. –  Gonzalo Medina Mar 21 at 12:26
Personally, I think it is one the major features introduced in version 3 in terms of utility (for my own purposes of course). –  Claudio Fiandrino Mar 21 at 13:51
@ClaudioFiandrino I agree. Not having to introduce special fixes for (some of) the babel modules is a great advantage. –  Gonzalo Medina Mar 21 at 19:26

As noted by Malipivo, the spanish option of babel makes > active. You can make it inactive just inside (all) the tikzpicture environment locally, Add these in your preamble:

\usepackage{etoolbox}
\AtBeginEnvironment{tikzpicture}{\shorthandoff{>}\shorthandoff{<}}{}{}


Full code:

\documentclass{article}
\usepackage[spanish]{babel}
\usepackage{tikz}
\usepackage{etoolbox}
\AtBeginEnvironment{tikzpicture}{\shorthandoff{>}\shorthandoff{<}}{}{}

\begin{document}

Simple arrow:

\begin{tikzpicture}
\draw[->] (-1.5,0) -- (1.5,0);
\end{tikzpicture}

\end{document}


-
With TikZ 3.0.0 there is a babel library on purpose: see Tikz shortandoff-on newcommand problem. –  Claudio Fiandrino Mar 21 at 6:20

If you don't need to use spanish quotes, just add es-noquoting to babel options and TiKZ arrows will appear again.

\documentclass[pdftex,spanish]{article}
\usepackage[es-noquoting]{babel}
\usepackage{tikz}

\begin{document}

Simple arrow:

\begin{tikzpicture}
\draw[->] (-1.5,0) -- (1.5,0);
\end{tikzpicture}

\end{document}


If you need spanish quotes use new babel TiKZ 3.0 library (as Claudio suggested). Here you have an example:

\documentclass[pdftex,spanish]{article}
\usepackage{babel}
\usepackage{tikz}
\usetikzlibrary{babel}

\begin{document}

Simple arrow:

\begin{tikzpicture}
\draw[->] (-1.5,0) -- (1.5,0) node[right] {<<Hola>>};
\end{tikzpicture}

<<Hola>>
\end{document}


-