0

I am trying to include a TikZ figure in a French-language presentation. The Beamer script, without specifying the language, is as follows:

\documentclass{beamer}
\usetheme[progressbar=frametitle]{metropolis}
\setbeamertemplate{frame numbering}[fraction]
\useoutertheme{metropolis}
\useinnertheme{metropolis}
\usefonttheme[onlymath]{serif}
\usecolortheme{spruce}
\setbeamercolor{background canvas}{bg=white}

%\usepackage[french]{babel} \usepackage[T1]{fontenc} \usepackage{ragged2e,mathtools,tikz,amsfonts} \addtobeamertemplate{block begin}{}{\justifying}

\metroset{block=fill} \useinnertheme{tcolorbox}

\justifying

\title{Probabilité} \subtitle{Lois Continues} \date[]{}

\usepackage{amsmath} \usepackage{pgfplots} \usepackage[outline]{contour} \contourlength{1.2pt} \tikzset{>=latex} \usetikzlibrary{positioning, calc}

\pgfmathdeclarefunction{gauss}{3}{% \pgfmathparse{1/(#3sqrt(2pi))exp(-((#1-#2)^2)/(2#3^2))}% }

% to fill an area under function \usepgfplotslibrary{fillbetween} \usetikzlibrary{patterns}

\AtEndPreamble{ \pgfplotsset{compat=1.12} }

\begin{document} \begin{frame}{Frame Title} \begin{figure}[h] \caption{\large Densité de la loi normale $N(\mu, \sigma)$} \centering \begin{tikzpicture}[scale=1.2] \def\N{50} \def\B{11} \def\Bs{3.0} \def\xmax{\B+3.2\Bs} \def\ymin{{-0.1gauss(\B,\B,\Bs)}} \def\h{0.08*gauss(\B,\B,\Bs)}

        \begin{axis}[
            every axis plot post/.append style={
                mark=none,
                domain={-0.05*(\xmax)}:{1.08*\xmax},
                samples=\N,
                smooth
            },
            xmin={-0.1*(\xmax)}, xmax=\xmax,
            ymin=\ymin, ymax={1.1*gauss(\B,\B,\Bs)},
            axis y line*=left,
            axis x line*=bottom,
            axis line style={draw=none},
            ticks=none,
            every axis x label/.style={
                at={(current axis.right of origin)},
                anchor=north
            },
            width=0.85*\textwidth, 
            height=0.55*\textwidth,
            y=700pt,
            clip=false
        ]

            % PLOTS
            \addplot[blue, thick, name path=B] {gauss(x,\B,\Bs)};

            % FILL
            \path[name path=xaxis]
                (0,0) -- (\pgfkeysvalueof{/pgfplots/xmax},0); 
            \addplot[blue!50] fill between[of=xaxis and B, soft clip={
                domain={\B-3*\Bs}:{\B+3*\Bs}}
            ];
            \addplot[blue!25] fill between[of=xaxis and B, soft clip={
                domain={\B-2*\Bs}:{\B+2*\Bs}}
            ];
            \addplot[blue!10] fill between[of=xaxis and B, soft clip={
                domain={\B-1*\Bs}:{\B+1*\Bs}}
            ];

            % LINES
            \addplot[black, dotted, thick]
                coordinates {({\B-3*\Bs},{1*gauss(\B-3*\Bs,\B,\Bs)}) ({\B-3*\Bs},{-\h})}
                node[below=-3pt, scale=0.8] {\strut$\mu-3\sigma$};
            \addplot[black, dotted, thick]
                coordinates {({\B-2*\Bs},{1*gauss(\B-2*\Bs,\B,\Bs)}) ({\B-2*\Bs},{-\h})}
                node[below=-3pt, scale=0.8] {\strut$\mu-2\sigma$};
            \addplot[black, dotted, thick]
                coordinates {({\B-1*\Bs},{1*gauss(\B-\Bs,\B,\Bs)}) ({\B-1*\Bs},{-\h})}
                node[below=-3pt, scale=0.8] at ({\B-\Bs},{-\h}) {\strut$\mu-\sigma$};
            \addplot[black, dotted, line width=0.7pt]
                coordinates {(\B,{1*gauss(\B,\B,\Bs)}) (\B,{-\h})}
                node[below=-3pt, scale=0.8] {\strut$\mu$};
            \addplot[black, dotted, thick]
                coordinates {({\B+1*\Bs},{1*gauss(\B+\Bs,\B,\Bs)}) ({\B+1*\Bs},{-\h})}
                node[below=-3pt, scale=0.8] at ({\B+\Bs},{-\h}) {\strut$\mu+\sigma$};
            \addplot[black, dotted, thick]
                coordinates {({\B+2*\Bs},{1*gauss(\B+2*\Bs,\B,\Bs)}) ({\B+2*\Bs},{-\h})}
                node[below=-3pt, scale=0.8] at ({\B+2*\Bs},{-\h}) {\strut$\mu+2\sigma$};
            \addplot[black, dotted, thick]
                coordinates {({\B+3*\Bs},{1*gauss(\B+3*\Bs,\B,\Bs)}) ({\B+3*\Bs},{-\h})}
                node[below=-3pt, scale=0.8] at ({\B+3*\Bs},{-\h}) {\strut$\mu+3\sigma$};

        \end{axis}
    \end{tikzpicture}
\end{figure}

\end{frame}

\end{document}

enter image description here

This code as you see works as intended, but once I specify the language by uncommenting the following line:

%\usepackage[french]{babel}

it gives me this error message:

enter image description here

I tried to circumvent the problem by including \shorthandoff{;} after \begin{tikzpicture}, as recommended in this answer, but the problem persists; the figure does not appear. I also tried this answer, but it also doesn't work for me.

Any clue as to how to fix this problem would be appreciated.

  • 1
    In the future, please directly post the non-working code so we don't have to read all the lengthy code to find the place where one needs to uncomment something to recreate the problem. – samcarter_is_at_topanswers.xyz Dec 13 '23 at 22:27
  • @samcarter_is_at_topanswers.xyz thank you for the comment. I usually paste the whole code as I don't know where exactly the problem is. – Saïd Maanan Dec 13 '23 at 22:54
  • 1
    Most problems can be narrowed down rather easily: remove parts of the code, check if the problem is still there, remove other parts etc. However what I meant to say: it would be much more convenient if you had posted your code with the \usepackage[french]{babel} uncommented so one does not need to search for it just to remove the % sign. This way one could directly reproduce your problem. – samcarter_is_at_topanswers.xyz Dec 13 '23 at 22:59

2 Answers2

2

You can avoid the problem by using the babel tikz library and a fragile frame:

\documentclass{beamer}
\usetheme[progressbar=frametitle]{moloch}% modern fork of the metropolis theme
\setbeamertemplate{page number in head/foot}[totalframenumber]
\usefonttheme[onlymath]{serif}
\usecolortheme{spruce}
\setbeamercolor{background canvas}{bg=white}

\usepackage[french]{babel} \usepackage[T1]{fontenc} \usepackage{ragged2e,mathtools,tikz, %amsfonts } \addtobeamertemplate{block begin}{}{\justifying}

\useinnertheme{tcolorbox}

\justifying

\title{Probabilité} \subtitle{Lois Continues} \date[]{}

\usepackage{amsmath} \usepackage{pgfplots} \usepackage[outline]{contour} \contourlength{1.2pt} \tikzset{>=latex} \usetikzlibrary{positioning, calc}

\pgfmathdeclarefunction{gauss}{3}{% \pgfmathparse{1/(#3sqrt(2pi))exp(-((#1-#2)^2)/(2#3^2))}% }

% to fill an area under function \usepgfplotslibrary{fillbetween} \usetikzlibrary{patterns}

\AtEndPreamble{ \pgfplotsset{compat=1.12} }

\usetikzlibrary{babel}

\begin{document} \begin{frame}[fragile] \frametitle{Frame Title} \begin{figure}%[h] \caption{\large Densité de la loi normale $N(\mu, \sigma)$} \centering \begin{tikzpicture}[scale=1.2] \def\N{50} \def\B{11} \def\Bs{3.0} \def\xmax{\B+3.2\Bs} \def\ymin{{-0.1gauss(\B,\B,\Bs)}} \def\h{0.08*gauss(\B,\B,\Bs)}

        \begin{axis}[
            every axis plot post/.append style={
                mark=none,
                domain={-0.05*(\xmax)}:{1.08*\xmax},
                samples=\N,
                smooth
            },
            xmin={-0.1*(\xmax)}, xmax=\xmax,
            ymin=\ymin, ymax={1.1*gauss(\B,\B,\Bs)},
            axis y line*=left,
            axis x line*=bottom,
            axis line style={draw=none},
            ticks=none,
            every axis x label/.style={
                at={(current axis.right of origin)},
                anchor=north
            },
            width=0.85*\textwidth, 
            height=0.55*\textwidth,
            y=700pt,
            clip=false
        ]

            % PLOTS
            \addplot[blue, thick, name path=B] {gauss(x,\B,\Bs)};

            % FILL
            \path[name path=xaxis]
                (0,0) -- (\pgfkeysvalueof{/pgfplots/xmax},0); 
            \addplot[blue!50] fill between[of=xaxis and B, soft clip={
                domain={\B-3*\Bs}:{\B+3*\Bs}}
            ];
            \addplot[blue!25] fill between[of=xaxis and B, soft clip={
                domain={\B-2*\Bs}:{\B+2*\Bs}}
            ];
            \addplot[blue!10] fill between[of=xaxis and B, soft clip={
                domain={\B-1*\Bs}:{\B+1*\Bs}}
            ];

            % LINES
            \addplot[black, dotted, thick]
                coordinates {({\B-3*\Bs},{1*gauss(\B-3*\Bs,\B,\Bs)}) ({\B-3*\Bs},{-\h})}
                node[below=-3pt, scale=0.8] {\strut$\mu-3\sigma$};
            \addplot[black, dotted, thick]
                coordinates {({\B-2*\Bs},{1*gauss(\B-2*\Bs,\B,\Bs)}) ({\B-2*\Bs},{-\h})}
                node[below=-3pt, scale=0.8] {\strut$\mu-2\sigma$};
            \addplot[black, dotted, thick]
                coordinates {({\B-1*\Bs},{1*gauss(\B-\Bs,\B,\Bs)}) ({\B-1*\Bs},{-\h})}
                node[below=-3pt, scale=0.8] at ({\B-\Bs},{-\h}) {\strut$\mu-\sigma$};
            \addplot[black, dotted, line width=0.7pt]
                coordinates {(\B,{1*gauss(\B,\B,\Bs)}) (\B,{-\h})}
                node[below=-3pt, scale=0.8] {\strut$\mu$};
            \addplot[black, dotted, thick]
                coordinates {({\B+1*\Bs},{1*gauss(\B+\Bs,\B,\Bs)}) ({\B+1*\Bs},{-\h})}
                node[below=-3pt, scale=0.8] at ({\B+\Bs},{-\h}) {\strut$\mu+\sigma$};
            \addplot[black, dotted, thick]
                coordinates {({\B+2*\Bs},{1*gauss(\B+2*\Bs,\B,\Bs)}) ({\B+2*\Bs},{-\h})}
                node[below=-3pt, scale=0.8] at ({\B+2*\Bs},{-\h}) {\strut$\mu+2\sigma$};
            \addplot[black, dotted, thick]
                coordinates {({\B+3*\Bs},{1*gauss(\B+3*\Bs,\B,\Bs)}) ({\B+3*\Bs},{-\h})}
                node[below=-3pt, scale=0.8] at ({\B+3*\Bs},{-\h}) {\strut$\mu+3\sigma$};

        \end{axis}
    \end{tikzpicture}
\end{figure}

\end{frame}

\end{document}

1

Edit:

  • After rewriting your MWE to simpler, much shorter and clear code and adding shorthands=off, to babel options, it works fine.
  • For filling area below Gauss function is used \closedcycle instruction, which draw closed path and enable simple coloring obtained shape by fill=<color option in the addplot macro:
\documentclass{beamer}
\usetheme[progressbar=frametitle]{metropolis}
\setbeamertemplate{frame numbering}[fraction]
\usefonttheme[onlymath]{serif}
\usecolortheme{spruce}

\usepackage[T1]{fontenc} \usepackage[shorthands=off, french]{babel} \usepackage{pgfplots} \usetikzlibrary{babel} % function \pgfmathdeclarefunction{gauss}{3}{% \pgfmathparse{1/(#3sqrt(2pi))exp(-((#1-#2)^2)/(2#3^2))}% } \newcommand\GH[1]{{gauss(#1,\B,\Bs)}}

%---------------------------------------------------------------% % make metropolis compatible with pgfplots % \AtEndPreamble{\pgfplotsset{compat=1.18}} %---------------------------------------------------------------% \begin{document} \begin{frame}%[fragile] \frametitle{Diagram of the Gauss density function} \begin{figure} \caption{Densité de la loi normale $\mathcal{N}(\mu,\sigma)$} % or just $N(\mu,\sigma)$ \centering \begin{tikzpicture}[ lbl/.style = {inner sep=2pt, font=\scriptsize, below}, arr/.style = {draw=red, very thin, shorten >=\tl}, ] % \def\B{11} % median, $\mu$ \def\Bs{3} % standard deviation, $\sigma$ % \def\N{101} % number of samples \def\xmax{\B+3.2\Bs} % width of function domain \def\tl{-2mm} % tick length % \begin{axis}[ width=0.85\textwidth, height=0.55\textwidth, % axis lines=none, % xmin=0, xmax={1.2\xmax}, ymin=0, ymax={1.1gauss(\B,\B,\Bs)}, % samples=\N, no marks, clip=false ] % PLOTS \scoped[draw=none] { \addplot [domain=0:{1.1\xmax}, fill=blue!45] {\GH{x}} \closedcycle; \addplot [domain=\B-2\Bs:\B+2\Bs, fill=blue!30] {\GH{x}} \closedcycle; \addplot [domain=\B-\Bs:\B+\Bs, fill=blue!15] {\GH{x}} \closedcycle; } \addplot [domain=0:{1.1*\xmax}, draw=blue, thick] {gauss(x,\B,\Bs)};

\draw[arr] (\B-3\Bs,\tl) node[lbl] {$\mu-3\sigma$} -- + (0,0) -- + (0, \GH{\B-3\Bs}); \draw[arr] (\B-2\Bs,\tl) node[lbl] {$\mu-2\sigma$} -- + (0,-\tl) -- + (0, \GH{\B-2\Bs}); \draw[arr] (\B-\Bs,\tl) node[lbl] {$\mu-\sigma$} -- + (0,-\tl) -- + (0, \GH{\B-\Bs}); \draw[arr] (\B,\tl) node[lbl] {$\mu$} -- + (0,-\tl) -- + (0, \GH{\B}); \draw[arr] (\B+\Bs,\tl) node[lbl] {$\mu+\sigma$} -- + (0,-\tl) -- + (0, \GH{\B-\Bs}); \draw[arr] (\B+2\Bs,\tl) node[lbl] {$\mu+2\sigma$} -- + (0,-\tl) -- + (0, \GH{\B-2\Bs}); \draw[arr] (\B+3\Bs,\tl) node[lbl] {$\mu+3\sigma$} -- + (0,0) -- + (0, \GH{\B-3\Bs}); \end{axis} \end{tikzpicture} \end{figure} \end{frame} \end{document}

enter image description here

Zarko
  • 296,517