在 Metropolis beamer 主题中生成 Tikz 图形时出现问题

在 Metropolis beamer 主题中生成 Tikz 图形时出现问题

我有一条使用 Tikz 生成的正态分布曲线,我想将其包含在 Beamer 演示文稿中。当我将 Tikz 代码包含在默认 Beamer 主题中时,它会生成而不会出现错误:

\documentclass{beamer}

\usepackage{amsmath} 
\usepackage{tikz}
\tikzset{>=latex} 
\usepackage{pgfplots} 
\usepackage[outline]{contour} 
\contourlength{1.2pt}
\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}
\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}

在此处输入图片描述

但是,当我在以大都市为主题的投影仪演示文稿中包含相同的 Tikz 代码时,我得到了非常扭曲的结果。

\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{amsmath} 
\usepackage{tikz}
\tikzset{>=latex} 
\usepackage{pgfplots} 
\usepackage[outline]{contour} 
\contourlength{1.2pt}
\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}
\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}

在此处输入图片描述

是什么原因造成的?我必须做什么才能正确呈现 Tikz 图形?提前谢谢您。

答案1

问题是,如果已加载包,metropolis主题会加载该包pgfplotsthemetol(它是主题的一部分) 。包放在序言的最末尾,它会覆盖您事先设置的任何其他值。我认为这是个错误。pgfplotspgfplotsthemetol\pgfplotsset{compat=1.9}compat

在您的情况下,解决方案是将其替换\pgfplotsset{compat=1.12}为,即使在包插入的东西之后也会\AtEndPreamble{\pgfplotsset{compat=1.12}}放置。\pgfplotsset{compat=1.12}pgfplotsthemetol

梅威瑟:

\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{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}

在此处输入图片描述

相关内容