[Tex/LaTex] tikz declare function and babel french option

babelfrenchtikz-pgf

I would like to declare functions in tikz for multiple uses of the same function within the code.

Sadly, it seems that the tikz "declare function" is not compatible with the french option in babel.
Indeed, the following code (that I simplifed on purpose) runs fine :

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

\begin{document}
\begin{tikzpicture}
    \tikzset{declare function={Carre(\t)=\t*\t;}}
    \draw plot [domain=-1:1] (\x,{Carre(\x)});
\end{tikzpicture}
\end{document}

But when I replace

\documentclass[english]{article}

with

\documentclass[english,french]{article}

compilation generates an error. This is what the log file says :

Missing character: There is no = in font nullfont!
Missing character: There is no @ in font nullfont!
Missing character: There is no @ in font nullfont!

Runaway argument?
-1:1] (\x ,{Carre(\x )}); \end {tikzpicture} \end {document}
! Paragraph ended before \pgfmath@local@@functions was complete.

\par
l.18

I suspect you've forgotten a `}', causing me to apply this
control sequence to too much text. How can we recover?
My plan is to forget the whole thing and hope for the best.

Before posting this, I updated all packages without success.

Any help welcome!

Best Answer

The semicolon is made active by the french babel option, which throws the TikZ parser off. You can say \shorthandoff{;} in your tizpicture to fix this.

You can do this either by manually putting \shorthandoff{;} at the start of each tikzpicture, or you can use a TikZ style for inserting the code automatically into each tikzpicture by setting

\tikzset{
    every picture/.prefix style={
        execute at begin picture=\shorthandoff{;}
    }
}

or, as Tobi points out in a comment, you can load the etoolbox package and use

\AtBeginEnvironment{tikzpicture}{\shorthandoff{;}}

to patch the tikzpicture environment.