Pgfplots 坐标小于一导致错误

Pgfplots 坐标小于一导致错误

我正在生成一个 pgfplots 图表来显示随时间变化的百分比。我正在使用 XeLaTeX 编译文档。当任何 y 坐标小于 1 时,文档无法编译并出现错误:

! Missing $ inserted.
<inserted text> 
                $
l.49 ^^I\end{axis}

当所有 y 坐标至少为 1 时,它就可以工作。这是一个工作示例:

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}[trim axis left, trim axis right]
    \begin{axis}
        [ymin=0,
        tick label style={/pgf/number format/assume math mode},
        every axis plot/.append style={ultra thick},
        ymajorgrids,
        legend style={at={(0.5,-0.2)}, anchor=north, legend columns=-1},
        ylabel={},
        symbolic x coords={
Aug 17,
Sep 17,
Oct 17,
Nov 17,
Dec 17,
Jan 18,
Feb 18,
Mar 18,
Apr 18,
May 18,
Jun 18,
Jul 18,},
        xtick=data,
        yticklabel={\pgfmathprintnumber\tick\%},
        nodes near coords,
        nodes near coords align={vertical},
        x tick label style={rotate=35, anchor=north east},]
    \addplot [draw=blue,
        nodes near coords={\pgfmathfloatifflags{\pgfplotspointmeta}{0}{}{\pgfmathprintnumber{\pgfplotspointmeta}\%}},
        nodes near coords align={horizontal},
        nodes near coords style={font=\Large,/pgf/number format/assume math mode}]
        coordinates{
(Aug 17,1.02)
(Sep 17,1.04)
(Oct 17,1.12)
(Nov 17,1.07)
(Dec 17,1.20)
(Jan 18,1.25)
(Feb 18,1.32)
(Mar 18,1.15)
(Apr 18,1.06)
(May 18,1.10)
(Jun 18,1.16)
(Jul 18,1.15)
        };
    \end{axis}
\end{tikzpicture}
\end{document}

将任何 y 坐标更改为小于 1 的值都会导致错误。有人知道是什么原因造成的吗?

答案1

仅仅假设数字处于数学模式是不够的,你还必须确保确实如此。pgfmanual 在第 954 页说

如果您知道数学模式处于活动状态,请使用假设数学模式=true。在这种情况下,最终数字将按原样排版,无需进行进一步检查。

发生的情况是,节点被排版为 $2\cdot10^{-2}\%$,如果您不在数学模式中,这显然会失败。

切换nodes near coords到数学模式后,错误消失。

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}[trim axis left, trim axis right]
    \begin{axis}
        [ymin=0,
        tick label style={/pgf/number format/assume math mode},
        every axis plot/.append style={ultra thick},
        ymajorgrids,
        legend style={at={(0.5,-0.2)}, anchor=north, legend columns=-1},
        ylabel={},
        symbolic x coords={
Aug 17,
Sep 17,
Oct 17,
Nov 17,
Dec 17,
Jan 18,
Feb 18,
Mar 18,
Apr 18,
May 18,
Jun 18,
Jul 18,},
        xtick=data,
        yticklabel={\pgfmathprintnumber\tick\%},
        nodes near coords,
        nodes near coords align={vertical},
        x tick label style={rotate=35, anchor=north east},]
    \addplot [draw=blue,
        nodes near coords={$\pgfmathfloatifflags{\pgfplotspointmeta}{0}{}{\pgfmathprintnumber{\pgfplotspointmeta}\%}$},
        nodes near coords align={horizontal},
        nodes near coords style={font=\Large,/pgf/number format/assume math mode=true}
        ]
        coordinates{
(Aug 17,0.02)
(Sep 17,1.04)
(Oct 17,1.12)
(Nov 17,1.07)
(Dec 17,1.20)
(Jan 18,1.25)
(Feb 18,1.32)
(Mar 18,1.15)
(Apr 18,1.06)
(May 18,1.10)
(Jun 18,1.16)
(Jul 18,1.15)
        };
    \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容