我仍然想创建像\ ionH这样的控制序列 它应该扩展为$ {H} ^ {+} $,对于其他离子也是如此。 foreach循环似乎在循环内部定义了控制序列,我希望“ \ global”使其在循环外部可见。 没用有任何想法如何解决这个问题吗?
\def\Element{
1 / H / Hydrogen / + / 1+,
11 / Na / Sodium / + / 1+,
17 / Cl / Chlorine / - / 1-,
19 / K / Potassium / + / 1+,
20 / Ca / Calcium / ++ / 2+,
}
\foreach \Z/\atom/\name/\charge/\ions in \Element{
\global\expandafter\def\csname ion\trimspaces\atom\endcsname{${\trimspaces\atom}^{\trimspaces\charge}$}
\immediate\write16{def{ion\trimspaces\atom}{\csname ion\trimspaces\atom \endcsname}}
}
% Without using \ionH in the text, the output looks like this:
% def{ionH}{${H}^{+}$}
% With using \ionH like below, the following error is generated:
%! Undefined control sequence.
%<argument> \atom
%
%l.3 \ionH
\ionH
答案 0 :(得分:1)
也许以下满足您的需求:
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{elements.csv}
1 , H , Hydrogen , + , 1+
2 , He , Helium , , 0
3 , Li , Lithium , + , 1+
4 , Be , Beryllium , ++ , 2+
5 , B , Boron , --- , 3- 3+
6 , C , Carbon , 4+ , 4+
7 , N , Nitrogen , --- , 3-
8 , O , Oxygen , -- , 2-
9 , F , Fluorine , - , 1-
10 , Ne , Neon , , 0
11 , Na , Sodium , + , 1+
12 , Mg , Magnesium , ++ , 2+
13 , Al , Aluminum , +++ , 3+
14 , Si , Silicon , 4+ , 4+ 4-
15 , P , Phosphorus , 5+ , 5+ 3+ 3-
16 , S , Sulfur , -- , 2- 2+ 4+ 6+
17 , Cl , Chlorine , - , 1-
18 , At , Argon , , 0
19 , K , Potassium , + , 1+
20 , Ca , Calcium , ++ , 2+
21 , Sc , Scandium , +++ , 3+
22 , Ti , Titanium , 4+ , 4+ 3+
23 , V , Vanadium , ++ , 2+ 3+ 4+ 5+
24 , Cr , Chromium , ++ , 2+ 3+ 6+
25 , Mn , Manganese , ++ , 2+ 4+ 7+
26 , Fe , Iron , ++ , 2+ 3+
27 , Co , Cobalt , ++ , 2+ 3+
28 , Ni , Nickel , ++ , 2+
29 , Cu , Copper , + , 1+ 2+
30 , Zn , Zinc , ++ , 2+
31 , Ga , Gallium , +++ , 3+
32 , Ge , Germanium , 4- , 4- 2+ 4+
33 , As , Arsenic , --- , 3- 3+ 5+
34 , Se , Selenium , -- , 2- 4+ 6+
35 , Br , Bromine , - , 1- 1+ 5+
36 , Kr , Krypton , , 0
37 , Rb , Rubidium , + , 1+
38 , Sr , Strontium , ++ , 2+
39 , Y , Yttrium , +++ , 3+
40 , Zr , Zirconium , 4+ , 4+
41 , Nb , Niobium , +++ , 3+ 5+
42 , Mo , Molybdenum , +++ , 3+ 6+
43 , Tc , Technetium , 6+ , 6+
44 , Ru , Ruthenium , +++ , 3+ 4+ 8+
45 , Rh , Rhodium , 4+ , 4+
46 , Pd , Palladium , ++ , 2+ 4+
47 , Ag , Silver , + , 1+
48 , Cd , Cadmium , ++ , 2+
49 , In , Indium , +++ , 3+
50 , Sn , Tin , ++ , 2+ 4+
51 , Sb , Antimony , --- , 3- 3+ 5+
52 , Te , Tellurium , -- , 2- 4+ 6+
53 , I , Iodine , - , 1-
54 , Xe , Xenon , , 0
55 , Cs , Cesium , + , 1+
56 , Ba , Barium , ++ , 2+
57 , La , Lanthanum , +++ , 3+
58 , Ce , Cerium , +++ , 3+ 4+
59 , Pr , Praseodymium , +++ , 3+
60 , Nd , Neodymium , +++ , 3+ 4+
61 , Pm , Promethium , +++ , 3+
62 , Sm , Samarium , +++ , 3+
63 , Eu , Europium , +++ , 3+
64 , Gd , Gadolinium , +++ , 3+
65 , Tb , Terbium , +++ , 3+ 4+
66 , Dy , Dysprosium , +++ , 3+
67 , Ho , Holmium , +++ , 3+
68 , Er , Erbium , +++ , 3+
69 , Tm , Thulium , +++ , 3+
70 , Yb , Ytterbium , +++ , 3+
71 , Lu , Lutetium , +++ , 3+
72 , Hf , Hafnium , 4+ , 4+
73 , Ta , Tantalum , 5+ , 5+
74 , W , Tungsten , 6+ , 6+
75 , Re , Rhenium , ++ , 2+ 4+ 6+ 7+
76 , Os , Osmium , +++ , 3+ 4+ 6+ 8+
77 , Ir , Iridium , +++ , 3+ 4+ 6+
78 , Pt , Platinum , ++ , 2+ 4+ 6+
79 , Au , Gold , + , 1+ 2+ 3+
80 , Hg , Mercury , + , 1+ 2+
81 , Tl , Thallium , + , 1+ 3+
82 , Pb , Lead , ++ , 2+ 4+
83 , Bi , Bismuth , +++ , 3+
84 , Po , Polonium , ++ , 2+ 4+
85 , At , Astatine , , ?
86 , Rn , Radon , , 0
87 , Fr , Francium , , ?
88 , Ra , Radium , ++ , 2+
89 , Ac , Actinium , +++ , 3+
90 , Th , Thorium , 4+ , 4+
91 , Pa , Protactinium , 5+ , 5+
92 , U , Uranium , +++ , 3+ 4+ 6+
\end{filecontents*}
\usepackage{readarray,pgffor}
\readarraysepchar{,}
\readdef{Elements.csv}\ElementFileContents
\readarray*\ElementFileContents\ElementArray[92,5]
\makeatletter
\foreach \Element in {1,...,\ElementArrayROWS} {%
\edef\x{\noexpand\global\noexpand\@namedef{\ElementArray[\Element,2]Num}{\ElementArray[\Element,1]}}\x % Define element number
\edef\x{\noexpand\global\noexpand\@namedef{\ElementArray[\Element,2]Name}{\ElementArray[\Element,3]}}\x % Define element name
\edef\x{\noexpand\global\noexpand\@namedef{\ElementArray[\Element,2]Charge}{\ElementArray[\Element,4]}}\x % Define element charge
\edef\x{\noexpand\global\noexpand\@namedef{\ElementArray[\Element,2]Other}{\ElementArray[\Element,5]}}\x % Define element "other"
}
\makeatother
\usepackage[version=4]{mhchem}
\newcommand{\ionic}[1]{%
\edef\x{\noexpand\ce{#1\noexpand^{\csname #1Charge\endcsname}}}\x
}
\begin{document}
\ionic{He}\ \ \ionic{Fe}\ \ \ionic{Kr}
\bigskip
Fe Num: \FeNum \par
Fe Name: \FeName \par
Fe Charge: \FeCharge \par
Fe Other: \FeOther
\end{document}
以上示例读取了elements.csv
,并为每个元素<element>
存储了宏
\<element>Num
\<element>Name
\<element>Charge
\<element>Other
捕获与<element>
关联的四个字段。