数据工具中的注释字符并忽略空行?

数据工具中的注释字符并忽略空行?

以下是使用该包的简单示例datatool

\documentclass[a4paper,10pt]{minimal}
\usepackage[utf8]{inputenc} \usepackage{datatool}
\begin{document}

    {\DTLsettabseparator%
    \DTLloadrawdb[noheader,keys={T,R}]{data}{data.csv}}% Brackets necessary in conjunction with some other packages that don't like what DLTsettabseparator does

    \begin{tabular}{c|c}
        \DTLforeach{data}{\first=T,\last=R}{ $\first\ K$ & $\last\ k\Omega$ \\ }
    \end{tabular}

\end{document}

data.csv

# This is 'data.csv'
# I'd like comments like this to be ignored by datatool

# And empty lines as well!

# Now comes the data
# Format:
# T[K]    R[kΩ]
1.4 12.79
8.1 8.81
11.4 8.24
17.3 6.63
25.7 5.27
31.2 3.58
39.0 3.03
44.3 2.38
54.7 1.72
61.2 1.21
69.7 0.987
81.0 0.718
88.5 0.626
99.6 0.491

有没有办法抑制以 开头的行的输出#以及空行的输出?或者甚至定义注释字符?

编辑: 添加了示例.csv文件。

答案1

我不确定您是否可以datatool处理这个问题,但是pgfplotstable具有类似功能的 会忽略注释行和空行。以下代码将排版您的数据的简单表格:

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\begin{filecontents}{testdata.csv}
# This is 'data.csv'
# I'd like comments like this to be ignored by datatool

# And empty lines as well!

# Now comes the data
# Format:
# T[K]    R[kΩ]
1.4 12.79
8.1 8.81
11.4 8.24
17.3 6.63
25.7 5.27
31.2 3.58
39.0 3.03
44.3 2.38
54.7 1.72
61.2 1.21
69.7 0.987
81.0 0.718
88.5 0.626
99.6 0.491
\end{filecontents}


\begin{document}
\pgfplotstableread{testdata.csv}\testdata
\pgfplotstabletypeset{\testdata}
\end{document}

我认为您可能对在 LaTeX 中可以使用这些数据做什么感兴趣。下面是一个示例,我使用pgfplotstable以更漂亮的方式排版结果,使用 提供的线条booktabs。我还使用 绘制数据pgfplots,并使用 拟合指数函数gnuplot。拟合参数显示在图上。所有这些都是使用单个 TeX 文件和单个编译运行完成的。如果您的数据发生变化,表格、图形和拟合参数都将自动反映这些变化:

\documentclass{article}
\usepackage{pgfplots}   % For plotting
\usepgfplotslibrary{units}  % For typesetting units in plot
\usepackage{pgfplotstable} % For typesetting tables from data
\usepackage{siunitx} % For typesetting units
\usepackage{booktabs} % Nicer horziontal lines and corresponding spacing
\usepackage{filecontents} % To include the data file in this .tex file
\begin{filecontents}{testdata.csv}
# This is 'data.csv'
# I'd like comments like this to be ignored by datatool

# And empty lines as well!

# Now comes the data
# Format:
# T[K]    R[kΩ]
1.4 12.79
8.1 8.81
11.4 8.24
17.3 6.63
25.7 5.27
31.2 3.58
39.0 3.03
44.3 2.38
54.7 1.72
61.2 1.21
69.7 0.987
81.0 0.718
88.5 0.626
99.6 0.491
\end{filecontents}


\begin{document}
\pgfplotstableread{testdata.csv}\testdata % Read data file into a table macro
\pgfplotstabletypeset[
    fixed zerofill, % Print numbers in fixed format, add zeros if necessary
    dec sep align, % Align numbers at the decimal point
    columns/0/.style={ % Options for the first column
        precision=1, % One decimal
        column name={
            \shortstack{$T$\\\si{\kelvin}} % Define column name. Use \shortstack to get two lines
        }
    },
    columns/1/.style={
        column name={
            \shortstack{$R$\\\si{\kilo\ohm}}
        }
    },
    every head row/.style={ % Set style for title row
        before row=\toprule, % Rules from booktabs package
        after row=\midrule
    },
    every last row/.style={ % Set style for final row
        after row=\bottomrule
    }
]{\testdata}
\hspace{1cm}
\begin{tikzpicture}[baseline] % "baseline" for correct vertical alignment with the table
\begin{axis}[
    anchor=center, % For correct vertical alignment with the table
    ymin=0, enlarge y limits=upper, % Fix lower y limit at zero, increase upper limit a bit
    xmin=0,xmax=100, % Fix lower and upper limits for x
    axis lines*=left, % Axis lines without arrow tips at the left and bottom
    unit code/.code 2 args={\si{#1#2}}, % Use siunitx for typesetting units
    xlabel=$T$, x unit=\kelvin, % Set labels and units
    ylabel=$R$, y unit=\kilo\ohm, ylabel style={rotate=-90}] % Typeset y label horizontally
\addplot [black, only marks] table \testdata; % Add the data points from the table
\addplot+[raw gnuplot, draw=red, no markers, smooth] gnuplot { % Issue gnuplot command, plot as smoothed line
    f(x)=a*exp(b*x); % Define function to fit
    a=15; % Initial values
    b=-1;
    fit f(x) 'testdata.csv' using 1:2 via a,b; % Perform fit
    plot [x=0:100] f(x); % calculate points for curve
    set print "parameters.dat"; % Open a file to save the parameters into
    print a,b; % Write the parameters to file
};
\pgfplotsextra{ % Execute code at end of plot, when everything else has been run
    \pgfplotstableread{parameters.dat}\parameters % Open the file Gnuplot wrote
    \pgfplotstablegetelem{0}{0}\of\parameters \pgfmathsetmacro\paramA{\pgfplotsretval} % Get first element, save into \paramA
    \pgfplotstablegetelem{0}{1}\of\parameters \pgfmathsetmacro\paramB{\pgfplotsretval}
    \node at (axis cs:5,10) [ % Position a node at the logical coordinate (4,10)
        pin=5:$\pgfmathprintnumber{\paramA} \cdot \mathrm{e}^{\pgfmathprintnumber[fixed,precision=3]{\paramB}\cdot T}$] {}; % Typeset equation using the `pin` argument.
}
\end{axis}
\end{tikzpicture}
\end{document}

相关内容