使用 siunitx 将包含数字和字符的 csv 列导入到 latex

使用 siunitx 将包含数字和字符的 csv 列导入到 latex

当我尝试从同一列中同时包含数字和字符的外部文件导入 csv 表时,siunitx 包出现以下错误。

包 siunitx 错误:数字输入中的标记“n”无效。...olviii} & \num{\csvcolix} & \num{\csvcolx}}

包 siunitx 错误:无效的数字输入‘nan’。...olviii} & \num{\csvcolix} & \num{\csvcolx}}

你能告诉我该找谁来修复它吗?

以下是我尝试修复的代码。请注意,nan第 3 行中有 s。

\documentclass{standalone}
\usepackage{csvsimple}
\usepackage{siunitx}

\begin{filecontents*}{datax.csv}
    SAE;Preston large;Preston medium;Preston small;Thornbury large;Thornbury medium;Thornbury small;Watsonia large;Watsonia medium;Watsonia small
    0.01;0.999719404401668;0.999894028145405;0.999925052593957;0.999724612270785;0.999895424470149;0.999952368552519;0.999801893445968;0.999835811093814;0.999939317307603
    0.02;0.99887728809391;0.999610604031798;0.999822693197714;0.998838278018596;0.999524047487632;0.999743728111286;0.999110837739707;0.999241442842171;0.999820408991234
    0.25;0.870257241788897;0.943192609889118;nan;0.852794258094785;0.926532048135534;nan;0.903983486684707;0.896993365388664;nan
\end{filecontents*}

\begin{document}
    \sisetup{round-mode=places, round-precision=4}
    \begin{tabular}{|l|l|l|l|l|l|l|l|l|l|}%
        
        \textbf{SAE} & \multicolumn{3}{c|}{\textbf{Preston}} &  \multicolumn{3}{c|}{\textbf{Thornbury}} & \multicolumn{3}{c|}{\textbf{Watsonia}} \\%
        & \textbf{Large} & \textbf{Medium} & \textbf{Small} & \textbf{Large} & \textbf{Medium} & \textbf{Small} & \textbf{Large} & \textbf{Medium} & \textbf{small}
        %specify table head
        \csvreader[head to column 
        names, separator=semicolon]{datax.csv}{}% use 
        %head of csv as column names
        {\\\hline\csvcoli & \num{\csvcolii } & \num{\csvcoliii} & 
            \num{\csvcoliv} & \num{\csvcolv} &  \num{\csvcolvi}  &  
            \num{\csvcolvii} & \num{\csvcolviii} &  \num{\csvcolix} & \num{\csvcolx}}% specify your coloumns here
    \end{tabular}


\end{document}

答案1

使用 的siunitx类型S列而不是l类型列和\num

在此处输入图片描述

\documentclass{standalone}
\usepackage{csvsimple}
\usepackage{siunitx}

\begin{filecontents*}{datax.csv}
    SAE;Preston large;Preston medium;Preston small;Thornbury large;Thornbury medium;Thornbury small;Watsonia large;Watsonia medium;Watsonia small
    0.01;0.999719404401668;0.999894028145405;0.999925052593957;0.999724612270785;0.999895424470149;0.999952368552519;0.999801893445968;0.999835811093814;0.999939317307603
    0.02;0.99887728809391;0.999610604031798;0.999822693197714;0.998838278018596;0.999524047487632;0.999743728111286;0.999110837739707;0.999241442842171;0.999820408991234
    0.25;0.870257241788897;0.943192609889118;nan;0.852794258094785;0.926532048135534;nan;0.903983486684707;0.896993365388664;nan
\end{filecontents*}

\begin{document}
    \sisetup{round-mode=places, round-precision=4,table-format=1.4}
    \begin{tabular}{|S[table-format=1.2, round-mode=off]|*{9}{S|}}%

        \textbf{SAE} & \multicolumn{3}{c|}{\textbf{Preston}} &  \multicolumn{3}{c|}{\textbf{Thornbury}} & \multicolumn{3}{c|}{\textbf{Watsonia}} \\%
        & \textbf{Large} & \textbf{Medium} & \textbf{Small} & \textbf{Large} & \textbf{Medium} & \textbf{Small} & \textbf{Large} & \textbf{Medium} & \textbf{small}
        %specify table head
        \csvreader[head to column 
        names, separator=semicolon]{datax.csv}{}% use 
        %head of csv as column names
        {\\\hline\csvcoli & \csvcolii  & \csvcoliii & 
            \csvcoliv & \csvcolv &  \csvcolvi  &  
            \csvcolvii & \csvcolviii &  \csvcolix & \csvcolx}% specify your coloumns here
    \end{tabular}


\end{document}

相关内容