用查找值替换 PGFPlotstable 中的列

用查找值替换 PGFPlotstable 中的列

我有一张表,其中有一列表示原子序数的整数值。我知道这个命令:

string replace={---}{\textemdash}

但是这是全表的。我想匹配特定列中的值,并将其替换为查找值。要么通过多个替换语句(我只会处理几个整数:我认为 1、6、7、8、16 是我唯一可能需要的整数)。或者最好通过某种查找表。

有没有这种替换的方法。我可以重写生成这些表的 python 脚本,但它们分布在许多位置,并且为了这个单一目的而将它们整合在一起需要付出相当大的努力。我想替换在 atomicnum 列中找到的值。我在下面提供了一个示例 csv 文件:

atomnum,atomicnum,x,y,z
1,6,0.000001,0.700530,-1.910890 2,6,0.000001,1.438902,-0.717628
3,6,0.000000,0.696419,0.453609 4,6,0.000000,-0.696419,0.453609
5,6,-0.000001,-1.438902,-0.717628 6,6,-0.000001,-0.700530,-1.910890
7,1,0.000001,1.224386,-2.862243 8,1,0.000002,2.524808,-0.733291
9,6,0.000001,-0.786842,1.970076 10,1,0.890609,-1.241092,2.412264
11,6,-0.000001,0.786842,1.970076 12,1,0.890605,1.241095,2.412266
13,1,-0.890609,1.241092,2.412264 14,1,-0.890605,-1.241095,2.412266
15,1,-0.000002,-2.524808,-0.733291 16,1,-0.000001,-1.224386,-2.862243

“看到”的表pgfplotstable将根据如下伪代码数组进行替换:

temp[] = {1=>"H", 6=>"C", 7=>"N", 8=>"O", 16=>"S"}

然后使用的表和输出是:

atomnum,atomicnum,x,y,z
1,C,0.000001,0.700530,-1.910890
2,C,0.000001,1.438902,-0.717628
3,C,0.000000,0.696419,0.453609
4,C,0.000000,-0.696419,0.453609
5,C,-0.000001,-1.438902,-0.717628
6,C,-0.000001,-0.700530,-1.910890
7,H,0.000001,1.224386,-2.862243
8,H,0.000002,2.524808,-0.733291
9,C,0.000001,-0.786842,1.970076
10,H,0.890609,-1.241092,2.412264
11,C,-0.000001,0.786842,1.970076
12,H,0.890605,1.241095,2.412266
13,H,-0.890609,1.241092,2.412264
14,H,-0.890605,-1.241095,2.412266
15,H,-0.000002,-2.524808,-0.733291
16,H,-0.000001,-1.224386,-2.862243

答案1

如果我正确理解了您的问题,您可以对该列进行本地替换。

\documentclass{standalone}
\usepackage{pgfplots,pgfplotstable}
\pgfplotstableread[col sep=comma]{
atomnum,atomicnum,x,y,z
1,6,0.000001,0.700530,-1.910890
2,6,0.000001,1.438902,-0.717628
3,6,0.000000,0.696419,0.453609
4,6,0.000000,-0.696419,0.453609
5,6,-0.000001,-1.438902,-0.717628
6,6,-0.000001,-0.700530,-1.910890
7,1,0.000001,1.224386,-2.862243
8,1,0.000002,2.524808,-0.733291
9,6,0.000001,-0.786842,1.970076
10,1,0.890609,-1.241092,2.412264
11,6,-0.000001,0.786842,1.970076
12,1,0.890605,1.241095,2.412266
13,1,-0.890609,1.241092,2.412264
14,1,-0.890605,-1.241095,2.412266
15,1,-0.000002,-2.524808,-0.733291
16,1,-0.000001,-1.224386,-2.862243
}\mytable
\begin{document}
\pgfplotstabletypeset[
columns/atomicnum/.append style={string type},
columns/atomicnum/.append style={string replace={6}{C}},
columns/atomicnum/.append style={string replace={1}{H}},
columns/x/.append style={dec sep align},
columns/y/.append style={dec sep align},
columns/z/.append style={dec sep align},
]\mytable
\end{document}

在此处输入图片描述

相关内容