2

I’m fighting with babel that keeps spawning Package tikz Error: + or - expected. when I use the calc library. In particular, some codes only work when \usetikzlibrary{babel} is on, while some other codes only work when \usetikzlibrary{babel} is off… My sanity is seriously endangered ^^’

Right now, my only hack to make this work is to disable \shorthandoff{!} before the \scalebox{}… but I find this very inelegant as it kind of defeats the purpose of babel, in particular when nodes contain !. Also, as I’m writting a library, it is hard to predict in advance the list of shorthands to disable automatically…

MWE that needs \usetikzlibrary{babel} to compile

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage[french]{babel}

\usepackage{tikz}
\usetikzlibrary{calc}
%\usetikzlibrary{babel} % If you enable this, it compiles this example, but not the other one

\NewDocumentCommand{\myAnimatedNode}{}{%
  \node(afirsthidden){};%
  \node(asecondhidden){};%
  % Last node to be drawn
  \path node[at={($(afirsthidden.center)!.5!(asecondhidden.center)$)}]{A};%
}


\begin{document}

\begin{tikzpicture}
  \myAnimatedNode
\end{tikzpicture}

\end{document}

MWE that fail with \usetikzlibrary{babel}

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage[french]{babel} % Use \begin{frame}[fragile] everywhere or you will get weird stuff with tikz

\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{babel} % If you disable this, then it compiles this example, but not the other one

\begin{document}
\scalebox{.5}{
  \begin{tikzpicture}
    \node(a){};
    \node(b){};
    \node[] at ($(a.north)!.5!(b.north)$){};
  \end{tikzpicture}
}
%\end{frame}
\end{document}

Combinaison of the two that will never compile

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage[french]{babel}

\usepackage{tikz}
\usetikzlibrary{calc}
% This document will never compile
\usetikzlibrary{babel} 

\NewDocumentCommand{\myAnimatedNode}{}{%
  \node(afirsthidden){};%
  \node(asecondhidden){};%
  % Last node to be drawn
  \path node[at={($(afirsthidden.center)!.5!(asecondhidden.center)$)}]{A};%
}


\begin{document}

\begin{tikzpicture}
  \myAnimatedNode
\end{tikzpicture}

\scalebox{.5}{
  \begin{tikzpicture}
    \node(a){};
    \node(b){};
    \node[] at ($(a.north)!.5!(b.north)$){};
  \end{tikzpicture}
}


\end{document}
6
  • 1
    Active characters are a nightmare… consider switching to lualatex to get rid of them in French. Your document compiles fine here (without loading \usetikzlibrary{babel} ). Commented May 3, 2023 at 17:47
  • 1
    \scalebox{.5}{ \begin{tikzpicture} \end{tikzpicture} } like any macro will not allow catcode changes, you also need % to avoid adding spaces Commented May 3, 2023 at 17:49
  • @DanielFlipo Oh, good to know that in lualatex it compiles fine… that’s a bit weird to me but interesting ^^’
    – tobiasBora
    Commented May 3, 2023 at 17:56
  • isn't there a TikZ command to scale by 0.5 factor? \scalebox freezes the catcodes of its argument, so tikz babel library can not undo that.
    – user691586
    Commented May 3, 2023 at 19:27
  • 1
    @tobiasBora Nothing weird here: with pdflatex characters ;:!? need to be active to provide automatic spacing before them. With lualatex (and xelatex too), there are other much safer ways to achieve the same thing (and more). See the babel-french documentation for more details. Commented May 3, 2023 at 19:45

1 Answer 1

2

There are many ways of making ! safe, for example

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathtools}


\usepackage[french]{babel}

\usepackage{tikz}
\usetikzlibrary{calc}
% This document will never compile
\usetikzlibrary{babel} 


\NewDocumentCommand{\myAnimatedNode}{}{%
  \node(afirsthidden){};%
  \node(asecondhidden){};%
  % Last node to be drawn
  \path node[at={($(afirsthidden.center)!.5!(asecondhidden.center)$)}]{A};%
}


\begin{document}

\begin{tikzpicture}
  \myAnimatedNode
\end{tikzpicture}


\scalebox{.5}{%
  \begin{tikzpicture}
    \node(a){};
    \node(b){};
    \node[] at \expanded{($(a.north)\string!.5\string!(b.north)$){}};
  \end{tikzpicture}%
}


\end{document}
2
  • Thanks! The issue with this solution is that the end user needs to worry about ! (i.e. the fix is applies outside of the preambule), I’d prefer a solution where people that don’t define new macros don’t need to dare about !… I tried to use \string inside \NewDocument… and disable the tikz library, but same error.
    – tobiasBora
    Commented May 3, 2023 at 18:54
  • 1
    @tobiasBora write in English:-) Commented May 3, 2023 at 19:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .