Graphics, Figures & TablesReferencing a variable name with a variable

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

Referencing a variable name with a variable

Post by topsquark »

Okay, I don't really have much of an example to show, but I cobbled together the code at the bottom.

What I am up to is writing a periodic table. The original code I am working with did not include much information and was able to use a command to put everything into a "minipicture" for each element. Mine includes much more information and the command would include more than 9 arguments, which apparently is too many.

So I've switched to arrays. I can do this by putting all of the information into a 2D array: Elements[0][...] is a vector for Hydrogen, Elements[1][...] is a vector for He, etc. The problem here is that the matrix Elements is rather large and seems to be slowing the system considerable when compiling.

So I thought I'd try the following idea: I know(?) that it is possible to create a variable name, something like x\i, where \i is a variable. So, instead of one big 2D array, I'd create a list of 108 individual arrays ordered by atomic number. So now Element1[...] would be a vector representing H, Element2[...] would represent He, etc.

So we get to the code. If I have to manually enter each array we have the code below for two "elements", x and y. I have to put in code to make a minipicture for each of x and y. But is there a way I can call them x1 and x2 and reference them (something like) x\i[...] in a foreach loop?

Thanks!

-Dan

Code: Select all

\def\print#1{\pgfmathparse{#1}\pgfmathresult}
\def\x{{1,2,3,4}}
\def\y{{1,2,3,4}}
\begin{tikzpicture}
     \node at (0,0) {
          \begin{tikzpicture}[x=1cm,y=1cm]
               {\node at (0.12,-0.12) {\print{\x[0]}};}
               {\node at (0.88,-0.12) {\print{\x[1]}};}
               {\node at (0.88,-0.88) {\print{\x[2]}};}
               {\node at (0.12,-0.88) {\print{\x[3]}};}
          \end{tikzpicture}
     };
     \node at (2,0) {
          \begin{tikzpicture}[x=1cm,y=1cm]
               {\node at (0.12,-0.12) {\print{\y[0]}};}
               {\node at (0.88,-0.12) {\print{\y[1]}};}
               {\node at (0.88,-0.88) {\print{\y[2]}};}
               {\node at (0.12,-0.88) {\print{\y[3]}};}
          \end{tikzpicture}
     };
\end{tikzpicture}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Stefan Kottwitz
Site Admin
Posts: 10340
Joined: Mon Mar 10, 2008 9:44 pm

Referencing a variable name with a variable

Post by Stefan Kottwitz »

Hi Dan,

do you already know those examples and package?
Stefan
LaTeX.org admin
topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

Referencing a variable name with a variable

Post by topsquark »

Stefan Kottwitz wrote:Hi Dan,

do you already know those examples and package?
Stefan
I knew the first one... that's my initial model.

None of three of these adds more than 7 features into the tile for each element: color, name, symbol, and a few numbers. I want to start by adding up to, say, six possible oxidation states, and unless I can find a way to make a table or picture and add that as a single element to the tile I will have more than 9 arguments for the tile to handle: too many to use a command to process. Here's a minimal sample of what I've been working on. (I have a lot more work to do... the positions need to be anchored better, for example, and I don't have the colors handled yet.)

-Dan

Code: Select all

% Adapted from https://texample.net/tikz/examples/periodic-table-of-chemical-elements.
% All Credit to Ivan Griffin.

\def\s{0.47}
\def\ns{2.3}
\def\print#1{\pgfmathparse{#1}\pgfmathresult}
\def\Elements{{{0,0,"yellow!20","red","H","Hydrogen",1,1.0079,14.0,20.2,0.0001,"$1s^1$","$\pm1$"},
               {0,1,"blue!55","black","Li","Lithium",3,6.941,453.5,1615,0.535,"$(He)2s^1$","$1$"},
               {0,2,"blue!55","black","Na","Sodium",11,22.990,370.9,1156,0.968,"$(Ne)3s^1$","$1$"},
               {0,3,"blue!55","black","K","Potassium",19,39.098,336.5,1032,0.856,"$(Ar)4s^1$","$1$"},
               {0,4,"blue!55","black","Rb","Rubidium",37,85.468,312.5,961.1,1.532,"$(Kr)5s^1$","$1$"},
               {0,5,"blue!55","black","Cs","Caesium",55,132.91,301.6,944.1,1.879,"$(Xe)6s^1$","$1$"},
               {0,6,"blue!55","black","Fr","Francium",87,223,294.0,923.1,"-","$(Rn)7s^1$","$1$"}}}

\begin{tikzpicture}[font=\sffamily, scale=\s, transform shape]

% Tile loop
  \foreach \i in {0,...,6} {
       \node at (\ns*\Elements[\i][0],-\ns*\Elements[\i][1]) {
          \begin{tikzpicture}[scale=\ns,x=1cm, y=1cm]
               \path (0.05,0) -- (0.95,0) arc (90:0:0.5mm) -- (1.0,-0.95) arc (0:-90:0.5mm) -- (0.05,-1.0) arc (-90:-180:0.5mm) -- (0,-0.05)
                    arc(180:90:0.5mm) -- cycle;
               \draw (0.05,0) -- (0.95,0) arc (90:0:0.5mm) -- (1.0,-0.95) arc (0:-90:0.5mm) -- (0.05,-1.0) arc (-90:-180:0.5mm) -- (0,-0.05)
                    arc(180:90:0.5mm) -- cycle;
               {\node at (0.14,-0.12) [scale=\s*1] {\print{\Elements[\i][6]}};}
               {\node at (0.75,-0.12) [scale=\s*1] {\print{\Elements[\i][7]}};}
               {\node at (0.14,-0.30) [scale=\s*0.6] {\print{\Elements[\i][8]}};}
               {\node at (0.14,-0.42) [scale=\s*0.6] {\print{\Elements[\i][9]}};}
               {\node at (0.14,-0.6) [scale=\s*0.6] {\print{\Elements[\i][10]}};}
               {\node at (0.6,-0.45) [scale=\s*2.5] {\print{\Elements[\i][4]}};}
               {\node at (0.5,-0.75) [scale=\s*0.8] {\print{\Elements[\i][11]}};}
               {\node at (0.5,-0.9) [scale=\s*1] {\print{\Elements[\i][5]}};}
               {\node at (0.9,-0.3) [scale=\s*0.8] {\print{\Elements[\i][12]}};}
          \end{tikzpicture}
       };
  };

\end{tikzpicture}
Post Reply