# Conflict between french option, babel package, babel tikzlibrary and etoolbox package

I'm sorry for the pretty vague title. I've tried to make it as clear as I could.

In my documents, I'm setting up the language option with babel package and to avoid some troubles I had to load babel tikzlibrary too. Everything is fine for my drawings except with contents I want to display at the end. To achieve that, I load etoolbox package and use \AtEndDocument macro. And at this moment, pdfLaTeX gives the following error : File ended while scanning use of \pgfplots@addplotimpl@expression@curly.

After some researches it appears that it comes from the french option that I'm using. I've reduced the code to a MWE by troubleshooting the code but I can't find a solution. Either by using shorthanoff{;} nor by using other fixes from related posts.

Here are some observations :

• Commenting the babel tikzlibrary solved the problem in the MWE but I want to keep it otherwise I've many others errors for my drawings ;
• Remove the french option solved the problem but students have already some difficulties with physic stuffs in french. I prefer to keep that option :]

Can you help me to find how I can display my tikzdrawings at the end of my document ?

Thanks for any help.

M(N)WE :

\documentclass[french]{book}
\usepackage{babel}
\usepackage{etoolbox}
\usepackage{tikz}
\usetikzlibrary{babel}

\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\chapter{foo}
\section{bar}
You have to solve $P=NP$ problem. Good luck !

\AtEndDocument{%
\subsection*{Resolution of the homework}
Here is my resolution :

\begin{tikzpicture}
\begin{axis}
\end{axis}
\end{tikzpicture}
}
\end{document}

• Try moving the \AtEndDocument{...} before the \begin{document} command; it needs to be in the preamble. – Herb Schulz Apr 22 '20 at 18:05
• Add \shorthandoff{;} before the \AtEndDocument to disable the shorthand. – Ulrike Fischer Apr 22 '20 at 18:06
• @HerbSchulz it works but I'm pretty sure it should work also in the document. I want to keep this portion of code as close as possible to where it is related. For corrections, commenting, inserting in another document,... Otherwise, I've just to insert it to the end of the document by hand without any hook. But thank you ! – Piroooh Apr 22 '20 at 19:46
• @UlrikeFischer "before the \AtEndDocument." -> This is the main point. I've tested \shorthandoff{;} but in the hook... I was focusing on the tikzpicture environment and only added the change of cat code before tikz environment. It solves my problem. If you want, turn it into an answer. Thank you ! – Piroooh Apr 22 '20 at 19:51

Add \shorthandoff{;} before the \AtEndDocument to disable the shorthand.

Inside the argument is too late, the catcode are already frozen then.

• To understand more about it, why is it too late after \AtEndDocument ? The problem goes wrong in the axis environment. No ? If I desactivate ; I think i've to reactivate it afterwards. I'll add it in my side but maybe you could add a note too in your answer. – Piroooh Apr 22 '20 at 20:13

Using a \savebox for the plots inside \AtEndDocument{...} also solves the problem and I think it will in general. It might be generally, i.e. regardless of babel and so, be a good idea to use \saveboxes when putting an axis in an \AtEndDocument{...} hook, see also the second code below.

\documentclass[french]{book}
\usepackage{babel}
\usepackage{etoolbox}
\usepackage{tikz}
\usetikzlibrary{babel}

\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\newsavebox\myplotA
\begin{document}
\chapter{foo}
\section{bar}
You have to solve $P=NP$ problem. Good luck !

\begin{lrbox}\myplotA
\begin{tikzpicture}
\begin{axis}
\end{axis}
\end{tikzpicture}
\end{lrbox}%
\AtEndDocument{%
\subsection*{Resolution of the homework}
Here is my resolution :

\usebox\myplotA
}
\end{document}


And this another way to solve the problem. I do not know enough about babel to understand why this happens specifically for babel, but this is a possible way to cure the expansion issue that happens here. (I find it actually a bit courageous to put an axis into the \AtEndDocument{...} hook but if you say that usually works then I am happy to believe it. ;-)

\documentclass[french]{book}
\usepackage{babel}
\usepackage{etoolbox}
\usepackage{tikz}
\usetikzlibrary{babel}

\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\chapter{foo}
\section{bar}
You have to solve $P=NP$ problem. Good luck !

\AtEndDocument{%
\subsection*{Resolution of the homework}
Here is my resolution :

\begin{tikzpicture}
\begin{axis}

• I've just tested and it works. Thank you ! Three points : First, why does it work to pass it as a macro ? Second, despite the workaround works I prefer to use a solution I can apply easily to other situations. In case of more complex axis environments, it can be very tedious to define such commands. So I'll parametrize the macro where I call \AtEndDocument to (des)(re)activate \shorthand before and after the hook as pointed out by @UlrikeFischer in the comments. It's the easier, and i think should be preferable, way. Third, what do you mean by "using a \savebox" ? – Piroooh Apr 22 '20 at 20:05
• @Piroooh I think that there is a problem beyond the shorthands. All this answer does is to solve a pgfplots expansion problem. It does not at all address the babel issue. As for the \savebox, you can use \newsavebox\myplot and then \savebox\myplot{\begin{tikzpicture}...\end{tikzpicture}} use \usebox\myplot. This can also be uses with \AtEndDocument and solves the expansion problems because everything is already expanded in the savebox. – user194703 Apr 22 '20 at 20:26