使用带有 siunitx 和 pgfplotstable 的破折号

使用带有 siunitx 和 pgfplotstable 的破折号

我有一个表格设置,其中大部分是数字列。但是,偶尔会有一些单元格有空白条目,目前在 excel csv 文件中输出为“---”。一切都很顺利,直到我想让单元格在小数位上对齐。我发现包S的列效果很好siunitx,我的文档其他地方也在使用这个列。所以我决定使用那些。但是,现在带有“---”的“空白”单元格造成了麻烦和冲突。我尝试使用此处线程中的某些方法来解决问题:

Siunitx:如何处理无效的数字输入‘-’?

没有任何成功(使用\textemdash,或用括号保护单元格{---})。我还尝试添加string type到列样式中,因为我在pgfplotstable手册中读到,这导致pgfplotstable只读取单元格而不进行进一步处理。

根据我使用的方法,我要么尝试该方法Missing \endcsname inserted时得到结果,要么 根本不拘泥于一种方法或另一种方法。我确实想使用,因为这只是我需要在我的文档中包含的大约 45 个这种格式的外部表中的一个,而且我不希望手动将它们全部排版为乳胶代码。textemdashCould not parse input '---' as a floating point numberpgfplotstable

这是一个“mwe”,因为它实际上不起作用:(

\documentclass{memoir}
\usepackage{pgfplotstable}
\usepackage{siunitx}
\usepackage{longtable}

\pgfplotstableset{
    begin table=\begin{longtable},
    end table=\end{longtable},
}

\begin{document}
    \pgfplotstabletypeset[%
        col sep=comma,%
        columns/atomnum/.style={int detect,column type=c},%
        columns/atomtype/.style={string type,column type=c},%
        columns/x/.style={string type,column type=S},%
        columns/y/.style={string type,column type=S},%
        columns/z/.style={string type,column type=S},%
        columns/nmrshieldtens/.style={column type=S},%
        columns/nmrscaledshifts/.style={column type=S},%
        header=true,
        every head row/.style={before row=\toprule, after row=\midrule\endhead},%
        every last row/.style={after row=\bottomrule}%
    ]{nankakurineSI.csv}%
\end{document}

我使用了一个外部文件,但我不知道如何在这里提供它,所以我在下面包含了文件内容。您可以在第 43 行和第 48 行看到问题所在。

atomnum,atomtype,x,y,z,nmrshieldtens,nmrscaledshifts
1, C ,2.645924,-0.284964,1.121739,145.6803,38.1985
2, C ,3.061887,-0.314747,-1.738533,138.1991,45.6797
3, C ,4.192868,-0.744373,-0.81303,152.3163,31.5625
4, C ,3.609622,-1.293585,0.487818,158.2814,25.5974
5, H ,2.179934,-0.733573,2.004648,29.7536,2.1014
6, H ,3.222428,0.584598,1.466674,30.9379,0.9171
7, H ,2.482083,-1.204262,-2.037409,28.7647,3.0903
8, H ,3.460033,0.135058,-2.653243,29.1953,2.6597
9, H ,4.8172,-1.495056,-1.309066,30.3061,1.5489
10, H ,4.826006,0.126259,-0.596497,30.5419,1.3131
11, H ,4.407002,-1.530092,1.200573,30.3571,1.4979
12, H ,3.088045,-2.236207,0.278539,30.219,1.636
13, C ,-0.54058,-1.145592,0.929915,143.9137,39.9651
14, C ,0.528585,-0.902319,-0.152946,143.2553,40.6235
15, C ,1.550392,0.221805,0.156845,124.581,59.2978
16, C ,0.851532,1.450518,0.80068,133.676,50.2028
17, C ,0.027293,1.042491,2.023216,147.7008,36.178
18, C ,-1.109706,0.152585,1.53285,140.8497,43.0291
19, H ,-0.057217,-1.683382,1.759072,30.1175,1.7375
20, H ,0.034879,-0.650236,-1.095222,30.0622,1.7928
21, H ,1.056471,-1.849267,-0.318437,29.5661,2.2889
22, H ,1.648183,2.146325,1.095154,30.7693,1.0857
23, H ,0.631687,0.517402,2.770335,29.9253,1.9297
24, H ,-0.375923,1.941621,2.506378,30.4385,1.4165
25, H ,-1.749305,-0.12778,2.381335,30.3971,1.4579
26, C ,-0.086446,2.231626,-0.127235,121.2219,62.6569
27, C ,-2.015491,0.916314,0.549213,113.5813,70.2975
28, H ,-0.424133,3.132366,0.42693,29.8436,2.0114
29, H ,0.446378,2.597638,-1.011354,28.8531,3.0019
30, C ,-1.676245,-2.03098,0.39515,138.255,45.6238
31, H ,-2.274539,-2.393781,1.244793,30.6875,1.1675
32, H ,-1.25699,-2.917557,-0.100028,30.3609,1.4941
33, C ,-2.623346,-1.297773,-0.5652,156.7968,27.082
34, H ,-2.066223,-1.014811,-1.468623,29.9263,1.9287
35, C ,-3.14384,-0.01614,0.093255,140.5753,43.3035
36, H ,-2.46664,1.767252,1.104586,29.9266,1.9284
37, H ,-3.843967,0.497571,-0.574314,29.7727,2.0823
38, H ,-3.719835,-0.292135,0.988647,31.0288,0.8262
39, C ,-3.780339,-2.203913,-0.982867,159.2459,24.6329
40, H ,-4.373272,-2.494855,-0.106965,31.3512,0.5038
41, H ,-4.447095,-1.695357,-1.687373,30.882,0.973
42, H ,-3.413599,-3.118855,-1.46021,30.8992,0.9558
43, N ,-1.242229,1.456093,-0.582487,{---},{---}
44, C ,-2.064595,2.286645,-1.451361,138.6569,45.2219
45, H ,-1.433234,2.735618,-2.223064,29.7894,2.0656
46, H ,-2.833329,1.693382,-1.950341,29.0105,2.8445
47, H ,-2.559918,3.103363,-0.892348,30.514,1.341
48, N ,2.23005,0.694822,-1.070475,{---},{---}
49, H ,1.515066,0.996756,-1.729488,30.5467,1.3083

答案1

您几乎已经完成了:要使用S列类型,您还必须使用string type,如果这样做,---则用 替换\textemdash即可。您可以通过设置 让 PGFPlotstable 负责替换string replace={---}{\textemdash}

请注意,使用S列类型时,还应指定数字格式,以便siunitx知道要分配多少空间。如果您的数字在小数点分隔符前有一位,在小数点分隔符后有六位,并且包含一个符号,则应该说column type={S[table-format=-1.6]}

\documentclass{memoir}
\usepackage{pgfplotstable}
\usepackage{siunitx}
\usepackage{longtable}

\pgfplotstableset{
    begin table=\begin{longtable},
    end table=\end{longtable},
}

\begin{document}
    \pgfplotstabletypeset[%
        col sep=comma,
        string replace={---}{\textemdash}, %
        columns/atomnum/.style={int detect,column type=c},%
        columns/atomtype/.style={string type,column type=c},%
        columns/x/.style={string type,column type={S[table-format=-1.6]}},%
        columns/y/.style={string type,column type={S[table-format=-1.6]}},%
        columns/z/.style={string type,column type={S[table-format=-1.6]}},%
        columns/nmrshieldtens/.style={string type, column type={S[table-format=3.4]}},%
        columns/nmrscaledshifts/.style={
            string type,        
            column type={S[table-format=2.4]}},%
        header=true,multicolumn names,
        every head row/.style={before row=\toprule, after row=\midrule\endhead},%
        every last row/.style={after row=\bottomrule}%
    ]{
atomnum,atomtype,x,y,z,nmrshieldtens,nmrscaledshifts
1, C ,2.645924,-0.284964,1.121739,145.6803,38.1985
2, C ,3.061887,-0.314747,-1.738533,138.1991,45.6797
3, C ,4.192868,-0.744373,-0.81303,152.3163,31.5625
4, C ,3.609622,-1.293585,0.487818,158.2814,25.5974
5, H ,2.179934,-0.733573,2.004648,29.7536,2.1014
6, H ,3.222428,0.584598,1.466674,30.9379,0.9171
7, H ,2.482083,-1.204262,-2.037409,28.7647,3.0903
8, H ,3.460033,0.135058,-2.653243,29.1953,2.6597
9, H ,4.8172,-1.495056,-1.309066,30.3061,1.5489
10, H ,4.826006,0.126259,-0.596497,30.5419,1.3131
11, H ,4.407002,-1.530092,1.200573,30.3571,1.4979
12, H ,3.088045,-2.236207,0.278539,30.219,1.636
13, C ,-0.54058,-1.145592,0.929915,143.9137,39.9651
14, C ,0.528585,-0.902319,-0.152946,143.2553,40.6235
15, C ,1.550392,0.221805,0.156845,124.581,59.2978
16, C ,0.851532,1.450518,0.80068,133.676,50.2028
17, C ,0.027293,1.042491,2.023216,147.7008,36.178
18, C ,-1.109706,0.152585,1.53285,140.8497,43.0291
19, H ,-0.057217,-1.683382,1.759072,30.1175,1.7375
20, H ,0.034879,-0.650236,-1.095222,30.0622,1.7928
21, H ,1.056471,-1.849267,-0.318437,29.5661,2.2889
22, H ,1.648183,2.146325,1.095154,30.7693,1.0857
23, H ,0.631687,0.517402,2.770335,29.9253,1.9297
24, H ,-0.375923,1.941621,2.506378,30.4385,1.4165
25, H ,-1.749305,-0.12778,2.381335,30.3971,1.4579
26, C ,-0.086446,2.231626,-0.127235,121.2219,62.6569
27, C ,-2.015491,0.916314,0.549213,113.5813,70.2975
28, H ,-0.424133,3.132366,0.42693,29.8436,2.0114
29, H ,0.446378,2.597638,-1.011354,28.8531,3.0019
30, C ,-1.676245,-2.03098,0.39515,138.255,45.6238
31, H ,-2.274539,-2.393781,1.244793,30.6875,1.1675
32, H ,-1.25699,-2.917557,-0.100028,30.3609,1.4941
33, C ,-2.623346,-1.297773,-0.5652,156.7968,27.082
34, H ,-2.066223,-1.014811,-1.468623,29.9263,1.9287
35, C ,-3.14384,-0.01614,0.093255,140.5753,43.3035
36, H ,-2.46664,1.767252,1.104586,29.9266,1.9284
37, H ,-3.843967,0.497571,-0.574314,29.7727,2.0823
38, H ,-3.719835,-0.292135,0.988647,31.0288,0.8262
39, C ,-3.780339,-2.203913,-0.982867,159.2459,24.6329
40, H ,-4.373272,-2.494855,-0.106965,31.3512,0.5038
41, H ,-4.447095,-1.695357,-1.687373,30.882,0.973
42, H ,-3.413599,-3.118855,-1.46021,30.8992,0.9558
43, N ,-1.242229,1.456093,-0.582487,---,---
44, C ,-2.064595,2.286645,-1.451361,138.6569,45.2219
45, H ,-1.433234,2.735618,-2.223064,29.7894,2.0656
46, H ,-2.833329,1.693382,-1.950341,29.0105,2.8445
47, H ,-2.559918,3.103363,-0.892348,30.514,1.341
48, N ,2.23005,0.694822,-1.070475,---,---
49, H ,1.515066,0.996756,-1.729488,30.5467,1.3083    
}%
\end{document}

相关内容