PGFPlots + TikZ:陌生人 Beamer 行为

PGFPlots + TikZ:陌生人 Beamer 行为

我正在尝试在用主题制作的演示文稿中包含一个PGFPlots+TikZ图形。如果我在普通类(或)中编译该图形,则该图形没有问题。该图形的代码如下:beamermetropolisarticlestandalone

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\usepackage[utf8]{inputenc} % Required for including letters with accents

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        width=\textwidth,
        height=8cm,
        hide y axis,
        axis x line*=bottom,
        xtick={-3,-2,-1,0,1,2,3},
        xticklabels={$\bar x-3s$,$\bar x-2s$,$\bar x-s$,$\bar x$,$\bar x+s$,$\bar x+2s$,$\bar x+3s$},
        xmin = -3.5,
        xmax = 3.5,
        ymin = 0,
        ymax = 0.9,
    ]
    \draw[dashed] (axis cs:-3,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:-3,0.85);
    \draw[dashed] (axis cs:-2,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:-2,0.7);
    \draw[dashed] (axis cs:-1,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:-1,0.55);
    \draw[dashed] (axis cs:1,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:1,0.55);
    \draw[dashed] (axis cs:2,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:2,0.7);
    \draw[dashed] (axis cs:3,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:3,0.85);
    \draw[stealth-stealth](-1,0.55) -- (1,0.55) node[midway,fill=white]{68\%};
    \node at(0,0.5) {1 desviación};
    \node at(0,0.45) {estándar};    
    \draw[stealth-stealth](-2,0.7) -- (2,0.7) node[midway,fill=white]{95\%};
    \node at(0,0.65) {2 desviaciones estándar};
    \draw[stealth-stealth](-3,0.85) -- (3,0.85) node[midway,fill=white]{99.7\%};    
    \node at(0,0.8) {3 desviaciones estándar};  
    \addplot[samples=201,ultra thick] {exp(-x^2/2)/sqrt(2*pi)};
    \draw[thick,dashed] (axis cs:0,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:0,{1/sqrt(2*pi)});
    \end{axis}
\end{tikzpicture}

\end{document}

由此得出该图: 独立测试

如果我现在尝试将该图形放入beamer演示文稿中(使用metropolis theme),我使用的代码是:

%!TEX TS-program = xelatex

\documentclass{beamer}
\usetheme{metropolis}           % Use metropolis theme

\usepackage[utf8]{inputenc} % Required for including letters with accents

\usepackage{tikz}
\usetikzlibrary{shapes}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
  \begin{frame}{Test}
      \centering
      \begin{tikzpicture}
        \begin{axis}[
            width=\textwidth,
            height=8cm,
            hide y axis,
            axis x line*=bottom,
            xtick={-3,-2,-1,0,1,2,3},
            xticklabels={$\bar x-3s$,$\bar x-2s$,$\bar x-s$,$\bar x$,$\bar x+s$,$\bar x+2s$,$\bar x+3s$},
            xmin = -3.5,
            xmax = 3.5,
            ymin = 0,
            ymax = 0.9,
          ]
        \draw[dashed] (axis cs:-3,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:-3,0.85);
        \draw[dashed] (axis cs:-2,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:-2,0.7);
        \draw[dashed] (axis cs:-1,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:-1,0.55);
        \draw[dashed] (axis cs:1,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:1,0.55);
        \draw[dashed] (axis cs:2,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:2,0.7);
        \draw[dashed] (axis cs:3,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:3,0.85);
        \draw[stealth-stealth](-1,0.55) -- (1,0.55) node[midway,fill=white]{68\%};
        \node at(0,0.5) {1 desviación};
        \node at(0,0.45) {estándar};    
        \draw[stealth-stealth](-2,0.7) -- (2,0.7) node[midway,fill=white]{95\%};
        \node at(0,0.65) {2 desviaciones estándar};
        \draw[stealth-stealth](-3,0.85) -- (3,0.85) node[midway,fill=white]{99.7\%};    
        \node at(0,0.8) {3 desviaciones estándar};  
        \addplot[samples=201,ultra thick] {exp(-x^2/2)/sqrt(2*pi)};
        \draw[thick,dashed] (axis cs:0,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:0,{1/sqrt(2*pi)});
        \end{axis}
      \end{tikzpicture}
  \end{frame}
\end{document}

编译后会产生如下结果:

Beamer 测试

我不知道发生了什么。任何帮助都将不胜感激。

答案1

2024 年更新:

避免此问题的最简单方法是不使用 metropolis 主题。相反,您可以使用 moloch 主题,它是 metropolis 主题的更新分支:

%!TEX TS-program = xelatex

\documentclass{beamer}
\usetheme{moloch}% modern fork of the metropolis theme

\usepackage[utf8]{inputenc} % Required for including letters with accents

\usepackage{tikz}
\usetikzlibrary{shapes}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
  \begin{frame}
      \frametitle{Test}
      \centering
      \begin{tikzpicture}
        \begin{axis}[
            width=\textwidth,
            height=8cm,
            hide y axis,
            axis x line*=bottom,
            xtick={-3,-2,-1,0,1,2,3},
            xticklabels={$\bar x-3s$,$\bar x-2s$,$\bar x-s$,$\bar x$,$\bar x+s$,$\bar x+2s$,$\bar x+3s$},
            xmin = -3.5,
            xmax = 3.5,
            ymin = 0,
            ymax = 0.9,
          ]
        \draw[dashed] (axis cs:-3,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:-3,0.85);
        \draw[dashed] (axis cs:-2,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:-2,0.7);
        \draw[dashed] (axis cs:-1,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:-1,0.55);
        \draw[dashed] (axis cs:1,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:1,0.55);
        \draw[dashed] (axis cs:2,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:2,0.7);
        \draw[dashed] (axis cs:3,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:3,0.85);
        \draw[stealth-stealth](-1,0.55) -- (1,0.55) node[midway,fill=white]{68\%};
        \node at(0,0.5) {1 desviación};
        \node at(0,0.45) {estándar};    
        \draw[stealth-stealth](-2,0.7) -- (2,0.7) node[midway,fill=white]{95\%};
        \node at(0,0.65) {2 desviaciones estándar};
        \draw[stealth-stealth](-3,0.85) -- (3,0.85) node[midway,fill=white]{99.7\%};    
        \node at(0,0.8) {3 desviaciones estándar};  
        \addplot[samples=201,ultra thick] {exp(-x^2/2)/sqrt(2*pi)};
        \draw[thick,dashed] (axis cs:0,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:0,{1/sqrt(2*pi)});
        \end{axis}
      \end{tikzpicture}
  \end{frame}
\end{document}

在此处输入图片描述

原始答案

经过一番侦查工作,结果发现问题是由

  • 大都市主题
  • 并从pgfplotsthemetol装在轮盘末端的包裹中
  • 哪些设置\pgfplotsset{compat=1.9}

作为一种解决方法,使用\pgfplotsset{compat=newest}之后\begin{document}

%!TEX TS-program = xelatex

\documentclass{beamer}
\usetheme{metropolis}           % Use metropolis theme
%

\usepackage[utf8]{inputenc} % Required for including letters with accents

\usepackage{tikz}
\usetikzlibrary{shapes}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\pgfplotsset{compat=newest}
  \begin{frame}
      \frametitle{Test}
      \centering
      \begin{tikzpicture}[x=1cm,y=1cm]
        \begin{axis}[
            width=\textwidth,
            height=8cm,
            hide y axis,
            axis x line*=bottom,
            xtick={-3,-2,-1,0,1,2,3},
            xticklabels={$\bar x-3s$,$\bar x-2s$,$\bar x-s$,$\bar x$,$\bar x+s$,$\bar x+2s$,$\bar x+3s$},
            xmin = -3.5,
            xmax = 3.5,
            ymin = 0,
            ymax = 0.9,
          ]
        \draw[dashed] (axis cs:-3,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:-3,0.85);
        \draw[dashed] (axis cs:-2,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:-2,0.7);
        \draw[dashed] (axis cs:-1,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:-1,0.55);
        \draw[dashed] (axis cs:1,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:1,0.55);
        \draw[dashed] (axis cs:2,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:2,0.7);
        \draw[dashed] (axis cs:3,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:3,0.85);
        \begin{scope}
        \draw[stealth-stealth](-1,0.55) -- (1,0.55) node[midway,fill=white]{68\%};
        \node at(0,0.5) {1 desviación};
        \node at(0,0.45) {estándar};    
        \draw[stealth-stealth](-2,0.7) -- (2,0.7) node[midway,fill=white]{95\%};
        \node at(0,0.65) {2 desviaciones estándar};
        \draw[stealth-stealth](-3,0.85) -- (3,0.85) node[midway,fill=white]{99.7\%};    
        \node at(0,0.8) {3 desviaciones estándar}; 
                    \end{scope} 
        \addplot[samples=201,ultra thick] {exp(-x^2/2)/sqrt(2*pi)};
        \draw[thick,dashed] (axis cs:0,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:0,{1/sqrt(2*pi)});
        \end{axis}
      \end{tikzpicture}
  \end{frame}
\end{document}

在此处输入图片描述

相关内容