PGF/TikZ 与法国 Babel(Beamer)冲突

PGF/TikZ 与法国 Babel(Beamer)冲突

我正在尝试在法语演示文稿中包含 TikZ 图形。未指定语言的 Beamer 脚本如下:

\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/(#3*sqrt(2*pi))*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.1*gauss(\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}

在此处输入图片描述

如您所见,此代码按预期工作,但是一旦我通过取消注释以下行来指定语言:

%\usepackage[french]{babel}

它给了我这个错误信息:

在此处输入图片描述

我尝试通过添加\shorthandoff{;}after来规避该问题\begin{tikzpicture},正如这个答案,但问题仍然存在;图形没有出现。我也试过了这个答案,但它对我来说也不起作用。

如能提供任何有关如何解决该问题的线索,我们将不胜感激。

答案1

babel您可以使用tikz 库和脆弱框架来避免该问题:

\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/(#3*sqrt(2*pi))*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.1*gauss(\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}

答案2

编辑:

  • 将 MWE 重写为更简单、更简短、更清晰的代码,并将shorthands=off, 添加到 babel 选项后,它可以正常工作。
  • 对于下面的填充区域,使用高斯函数指令,该指令绘制封闭路径并通过宏中的选项\closedcycle启用简单着色获得的形状:fill=<coloraddplot
\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/(#3*sqrt(2*pi))*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.1*gauss(\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}

在此处输入图片描述

相关内容