使用 tikz 和 pgfplot 绘制的无法解释的错误消息

使用 tikz 和 pgfplot 绘制的无法解释的错误消息

我对情节感到困惑,无法解释,更不用说解决了。

一个简单的例子是这样的(还有一条从数据绘制的曲线,但我跳过了它,因为它不会影响结果):

\begin{tikzpicture}[scale=1.7]
    \begin{axis}[
        xmin=0,
        xmax=0.08,
        ymin=0.04,
        ymax=0.12,
        xtick=\empty,
        ytick=\empty,
        xlabel={$\scriptscriptstyle \sigma$},
        ylabel={$\scriptscriptstyle \mu$},
        ytick={0.05,0.088,0.0947,0.1},
        yticklabels={\color{teal} $\scriptscriptstyle 5\%$,\color{blue} $\scriptscriptstyle 8.8\%$,\color{blue} $\scriptscriptstyle 9.47\%$,\color{blue} $\scriptscriptstyle 10\%$},
        xtick={0.0447,0.0485,0.0542},
        xticklabels={\color{blue} $\scriptscriptstyle 4.47\%$,\color{blue} $\scriptscriptstyle 4.85\%$,\color{blue} $\scriptscriptstyle 5.42\%$},
        every axis y label/.style={
            at={(ticklabel* cs:1.01)},
            anchor=south,
        },
        x label style={at={(axis description cs:1,-0.01)},anchor=south},
        x tick label style={yshift={-mod(\ticknum,2)*1em}},
        title={\footnotesize Efficient frontier}
        ]
        
        \draw [dashed, color=blue] (axis cs:0,0.088) --  (axis cs:0.0447,0.088);
        \draw [dashed, color=blue] (axis cs:0,0.0947) --  (axis cs:0.0485,0.0947);
        \draw [dashed, color=blue] (axis cs:0,0.1) --  (axis cs:0.0542,0.1);
        \draw [dashed, color=blue] (axis cs:0.0447,0) --  (axis cs:0.0447,0.088);
        \draw [dashed, color=blue] (axis cs:0.0485,0) --  (axis cs:0.0485,0.0947);
        \draw [dashed, color=blue] (axis cs:0.0542,0) --  (axis cs:0.0542,0.1);
            
        \draw[blue,fill=blue] (axis cs:0.0447,0.088) circle[radius=1] node[below left,blue]{$\scriptscriptstyle mvp$};
        \draw[blue,fill=blue] (axis cs:0.0485,0.0947) circle[radius=1] node[right,blue]{$\scriptscriptstyle T$};
        \draw[blue,fill=blue] (axis cs:0.0542,0.1) circle[radius=1] node[above,blue]{$\scriptscriptstyle p$};
    \end{axis}
\end{tikzpicture}

此代码提供以下图表:

在此处输入图片描述

此外,我还收到以下错误消息:

! Undefined control sequence.
<argument> -mod(\ticknum 
                         ,2)*1em
l.50        \end{axis}
                 
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

! Missing number, treated as zero.
<to be read again> 
                   p
l.50        \end{axis}
                 
A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)

但是,如果我运行以下代码(再次没有数据),一切都很好,并且没有错误消息:

\begin{tikzpicture}[scale=1.7]
    \begin{axis}[
        xmin=0.04,
        xmax=0.22,
        ymin=0.05,
        ymax=0.22,
        xtick=\empty,
        ytick=\empty,
        xlabel={$\scriptscriptstyle \sigma$},
        ylabel={$\scriptscriptstyle \mu$},
        ytick={0.1,0.1082,0.1401,0.2},
        yticklabels={\color{red} $\scriptscriptstyle 10\%$,\color{blue} $\scriptscriptstyle 10.82\%$,\color{blue} $\scriptscriptstyle 14.01\%$,\color{red} $\scriptscriptstyle 20\%$},
        xtick={0.09,0.1,0.2},
        xticklabels={\color{blue} $\scriptscriptstyle 9\%$,\color{red} $\scriptscriptstyle 10\%$,\color{red} $\scriptscriptstyle 20\%$},
        every axis y label/.style={
            at={(ticklabel* cs:1.01)},
            anchor=south,
        },
        x label style={at={(axis description cs:1,-0.01)},anchor=south},
        x tick label style={yshift={-mod(\ticknum,2)*1em}},
        title={\footnotesize Minimum-variance frontier}
        ]
        
        \draw [dashed, color=red] (axis cs:0,0.1) --  (axis cs:0.1,0.1);
        \draw [dashed, color=blue] (axis cs:0,0.1082) --  (axis cs:0.09,0.1082);
        \draw [dashed, color=blue] (axis cs:0,0.1401) --  (axis cs:0.09,0.1401);
        \draw [dashed, color=red] (axis cs:0,0.2) --  (axis cs:0.2,0.2);
        \draw [dashed, color=blue] (axis cs:0.09,0) --  (axis cs:0.09,0.1401);
        \draw [dashed, color=red] (axis cs:0.1,0) --  (axis cs:0.1,0.1);
        \draw [dashed, color=red] (axis cs:0.2,0) --  (axis cs:0.2,0.2);
                
        \draw[blue,fill=blue] (axis cs:0.09,0.1082) circle[radius=1] node[right,blue]{$\scriptscriptstyle I$};
        \draw[blue,fill=blue] (axis cs:0.09,0.1401) circle[radius=1] node[right,blue]{$\scriptscriptstyle E$};
        \draw[red,fill=red] (axis cs:0.1,0.1) circle[radius=1] node[below right,red]{{\tiny Stock 1}};
        \draw[red,fill=red] (axis cs:0.2,0.2) circle[radius=1] node[above,red]{{\tiny Stock 2}};
    \end{axis}
\end{tikzpicture}

我知道错误出在这部分:

x label style={at={(axis description cs:1,-0.01)},anchor=south},
x tick label style={yshift={-mod(\ticknum,2)*1em}},

但我就是搞不清楚到底是怎么回事。两个代码几乎完全相同,本质上只是数字不同,但不知何故第一个代码不起作用,$\dot 10^{-2}$右下角出现了奇怪的内容。

我很感激任何帮助。

相关内容