foreach 循环中多个“\immedite\write”缺少 } 错误;当前绘图没有 gnuplot 的坐标错误

foreach 循环中多个“\immedite\write”缺少 } 错误;当前绘图没有 gnuplot 的坐标错误

我怀疑这些错误来自一段代码,在该代码中我循环遍历主数据文件并对其进行重构,以便我可以将新生成的文件作为参数提供给 pgfplots。如果我用\iffalse...\fi“缺失 }”包围此部分,错误就会消失,只剩下 gnuplot 警告:“当前图没有坐标”,尽管 pgfplots(使用相同的数据文件(使用可能有问题的 foreach 循环制作))渲染正常。我尝试}在 之后放置一个额外的\end{axis},但这并不能修复错误,并在它之前放置一个,这会产生“额外的 }”错误。

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\usepackage{filecontents}
\usepackage{indentfirst}
\usepackage{amsmath}
\usepackage{float}
\usepackage{hhline}
\usepackage{multirow}
\usepackage{datatool}
\usepackage{booktabs}
\usepackage[strict]{changepage}
\usepackage[position=top]{subfig}

\begin{filecontents}{data.dat}
28.7 28.4 43.4
0.2416 0.2415 nan
0.2420 0.2416 nan
0.2421 0.2416 nan
0.2416 0.2422 nan
0.2410 0.2422 nan
35.9 35.6 50.4
0.2695 0.2691 nan
0.2679 0.2691 nan
0.2691 0.2697 nan
0.2691 0.2691 nan
0.2686 0.2694 nan
42.6 42.3 57.4
0.2951 0.2953 nan
0.2950 0.2951 nan
0.2952 0.2956 nan
0.2951 0.2957 nan
0.2951 0.2954 nan
47.2 46.9 61.9
0.3091 0.3104 nan
0.3095 0.3107 nan
0.3106 0.3107 nan
0.3105 0.3110 nan
0.3107 0.3103 nan
51.3 51.1 66.1
0.3234 0.3231 nan
0.3231 0.3235 nan
0.3238 0.3233 nan
0.3229 0.3241 nan
0.3243 0.3230 nan
57.4 57.2 72.3
0.3417 0.3431 nan
0.3426 0.3431 nan
0.3422 0.3437 nan
0.3430 0.3427 nan
0.3429 0.3437 nan
\end{filecontents}

\pgfplotsset{compat=1.14}
\pgfplotstableread{data.dat}\Data

\begin{document}
\newwrite\ostreama
\immediate\openout\ostreama=table1.dat
\foreach\row in {0,...,35}
{%
        \newcount\counttable
        \counttable=\row
        \divide\counttable by 6
        \multiply\counttable by 6
        \ifnum\row=\counttable
                %
        \else
                \pgfplotstablegetelem{\row}{0}\of\Data \pgfmathsetmacro\yval{\pgfplotsretval}%
                \pgfmathsetmacro\ysval{\yval ^ 2}%
                \pgfplotstablegetelem{\counttable}{0}\of\Data \pgfmathsetmacro\xval{\pgfplotsretval}%
                \immediate\write\ostreama{\xval\space\ysval}%
        \fi%
}

\begin{tikzpicture}
\begin{axis}[
    title style = {align=center},
        title = {Distance Fallen Plotted Against Time to Fall for Steel Ball},
        axis lines = left,
        xlabel = {distance in centimeters, $y$},
        ylabel = {time squared in seconds squared, $t^2$},
        domain = {20:60},
        xmin = 20, xmax = 60,
        ymin = 0.05, ymax = 0.12,
        xtick = {20, 25, 30, 35, 40, 45, 50, 55, 60},
        ytick = {0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.11, 0.12},
        xmajorgrids = true,
        ymajorgrids = true,
        legend pos = outer north east,
]
\addplot[only marks, mark = *] file {table1.dat};
\addlegendentry{$(y, t^2)$}

\addplot+[raw gnuplot, mark = none, thick, blue] gnuplot {
        f(x)=a*x+b;
        a = 1;
    b = 1;
    set fit errorvariables;
                fit f(x) 'table1.dat' using 1:2 via a, b;
                plot [x=20:60] f(x);
        set print "pars1.dat";
        print a, a_err;
        print b, b_err;
};
\addlegendentry{\pgfplotstableread{pars1.dat}\parameters
        \pgfplotstablegetelem{0}{0}\of\parameters \pgfmathsetmacro\paramA{\pgfplotsretval}
        \pgfplotstablegetelem{1}{0}\of\parameters \pgfmathsetmacro\paramB{\pgfplotsretval}
        $\Delta t^2 = \pgfmathprintnumber{\paramA}y + \pgfmathprintnumber{\paramB}$
};
\end{axis}
\end{tikzpicture}

\end{document}

答案1

我发现了这个问题。我不知道为什么会导致这个错误,但这只是没有关闭用于写入保存重组数据的文件的写入流的问题。然后错误传递到 gnuplot,它无法读取任何点,然后传递到 pgfplots 部分并以某种方式表现为缺少花括号。

相关内容