我有一段来自元素周期表的小摘录。
现在我想将符号(第 2 列)正确地放置在具有 8 行 18 列的 TikZ 矩阵中;其中未占用的单元格应保持空白。
对于我所说的:
\pgfmathsetglobalmacro\PeriodGroupTest{
\PeriodNo==\Row && \GroupNo==\Col ? 1 : 0 }
不知何故,使用我的方法,只有第一个条目是正确的。我做错了什么?
顺便说一句:随着测试数据的列出,它总是比任何纸张尺寸都大,所以我不得不为文档和字体大小设置奇怪的值。
\begin{filecontents}[overwrite]{psetest.csv}
Z; Symbol; Period; Group
1; H; 1; 1
2; He; 1; 18
4; Be; 2; 2
\end{filecontents}
\documentclass[paper=A2, fontsize=2pt]{scrartcl}
\usepackage[margin=1mm, landscape, showframe=false]{geometry}
\usepackage{lmodern}
\usepackage{etoolbox}
\usepackage{tikz}
\usetikzlibrary{matrix}
\def\pgfmathsetglobalmacro#1#2{%
\pgfmathparse{#2}%
\global\let#1\pgfmathresult}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}
\pgfplotstableread[col sep = semicolon, col sep=semicolon]{psetest.csv}{\psetable}
\begin{document}
\pgfplotstabletypeset[string type]{\psetable}
\begin{tikzpicture}
\let\mymatrixcontent\empty
\foreach[evaluate={\Row=int(\row+1)}] \row in {0,...,7}{%%
\foreach[evaluate={\Col=int(\col+1)}] \col in {0,...,17}{%
% Group and Period
\ifnum\row<3% Here for the test-table ====
\pgfplotstablegetelem{\row}{Period}\of{\psetable}
\xdef\PeriodNo{\pgfplotsretval}
\pgfplotstablegetelem{\row}{Group}\of{\psetable}
\xdef\GroupNo{\pgfplotsretval}
% PeriodGroupTest
\pgfmathsetglobalmacro\PeriodGroupTest{
\PeriodNo==\Row && \GroupNo==\Col ? 1 : 0 }
\fi%==========
% Insert Symbol
\ifnum\PeriodGroupTest=1%
\pgfplotstablegetelem{\row}{Symbol}\of{\psetable}
\xdef\symbol{\pgfplotsretval}
%
\xappto\mymatrixcontent{\symbol,
r\row c\col, R\Row C\Col, p\PeriodNo, g\GroupNo, pgt\PeriodGroupTest \expandonce{\&} }
\else%
\xappto\mymatrixcontent{X,
r\row c\col, R\Row C\Col, p\PeriodNo, g\GroupNo, pgt\PeriodGroupTest \expandonce{\&} }
\fi%
}%
\gappto\mymatrixcontent{\\}
}%%
\matrix[matrix of nodes, ampersand replacement=\&,
label={PSE}] (m){
\mymatrixcontent
};
\end{tikzpicture}
\end{document}
答案1
或许是这样的:
\begin{filecontents}[overwrite]{psetest.csv}
Z; Symbol; Period; Group
1; H; 1; 1
2; He; 1; 18
%3; Li; 2; 1
4; Be; 2; 2
% ...
14; Si; 3; 14
\end{filecontents}
\documentclass[paper=A4, %fontsize=2pt
]{scrartcl}
\usepackage[margin=15mm, landscape, showframe=false]{geometry}
\usepackage{lmodern}
\usepackage{etoolbox}
\usepackage{tikz}
\usetikzlibrary{matrix}
\def\pgfmathsetglobalmacro#1#2{%
\pgfmathparse{#2}%
\global\let#1\pgfmathresult}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}
\pgfplotstableread[col sep = semicolon, col sep=semicolon]{psetest.csv}{\psetable}
\pgfplotstablegetrowsof{\psetable}
\pgfmathtruncatemacro\psetableLastRowNo{\pgfplotsretval-1}
\def\Showvalues{1}
\def\symbol{Xyz}
\def\GroupNo{0}
\def\PeriodNo{0}
\def\PeriodGroupTest{0}
\begin{document}
\pgfplotstabletypeset[string type]{\psetable}
\noindent\begin{tikzpicture}
\let\mymatrixcontent\empty
\foreach \row in {1,...,8}{%%
\foreach \col in {1,...,18}{%
% Group and Period
\foreach \tablerow in {0,...,\psetableLastRowNo}{%===========
\pgfplotstablegetelem{\tablerow}{Period}\of{\psetable}
\xdef\PeriodNo{\pgfplotsretval}
\pgfplotstablegetelem{\tablerow}{Group}\of{\psetable}
\xdef\GroupNo{\pgfplotsretval}
% PeriodGroupTest
\pgfmathsetglobalmacro\PeriodGroupTest{
\PeriodNo==\row && \GroupNo==\col ? 1 : 0 }
% Insert Symbol
\ifnum\PeriodGroupTest=1%
\breakforeach% <---- ?
\pgfplotstablegetelem{\tablerow}{Symbol}\of{\psetable}
\xdef\symbol{\pgfplotsretval}
\xdef\Fill{red}
\xdef\Draw{blue}
%\breakforeach% <---- ?
\else%
\xdef\symbol{}
\xdef\Fill{none}
\xdef\Draw{none}
\fi%
}%==============
\xappto\mymatrixcontent{|[fill=\Fill, draw=\Draw]|{\symbol
\ifnum\Showvalues=1 \\ r\row c\col \\ p\PeriodNo g\GroupNo \\ pgt\PeriodGroupTest \fi}
\expandonce{\&} }
}%
\gappto\mymatrixcontent{\\}
}%%
\matrix[matrix of nodes, ampersand replacement=\&,
nodes={align=center,
text width=8mm,
},
column sep=0em,
%label={PSE}
] (m){
\mymatrixcontent
};
\end{tikzpicture}
\end{document}