# Use labels from file in Tikz

I want to compare data from two files in a 4 by 4 grid. The data files are like this:

data1.dat:

A  B   C  D
.....


data2.dat:

1   2   3   4
.........


I am able to set up the grid with the following code using snippets from this site. The labels for each grid point have to be entered manually. I want some suggestions to modify the program to read the labels from the files instead.

\documentclass{article}
\usepackage{tikz}
\begin{filecontents}{Data1.dat}
A        B         C          D
E        F         G          H
I        J         K          L
M        N         O          P
\end{filecontents}
\begin{filecontents}{Data2.dat}
1      2    3     4
5   6   7     8
9     10    11  12
11   12     13     14
\end{filecontents}

\begin{document}
\begin{tikzpicture}
\draw[step=1.0cm,color=gray] (-2,-2) grid (2,2);
\node at (-1.5,+1.75) {A};
\node at (-1.5,+1.25) {1};
\node at (-0.5,+1.75) {B};
\node at (-0.5,+1.25) {2};
\node at (+0.5,+1.75) {C};
\node at (+0.5,+1.25) {3};
\node at (+1.5,+1.75) {D};
\node at (+1.5,+1.25) {4};
%..
\node at (+1.5,-1.25) {P};
\node at (+1.5,-1.75) {16};

\end{tikzpicture}
\end{document}

-

A proof of concept; the idea is to use pgfplotstable; the first table is typeset, appropriately reserving space for the values of the second table; then, the second table is typeset and superimposed to the first one, using a simple \llap:

\documentclass{article}
\usepackage{pgfplotstable}

\newcolumntype{C}{>{\centering\arraybackslash\rule{0pt}{15pt}}p{2em}}

1      2    3     4
5   6   7     8
9     10    11  12
11   12     13     14
A      B    C     D
E   F   G     H
I     J    K  L
M   N     O     P

\begin{document}

\pgfplotstabletypeset[%
every first row/.style={before row= & & & \\\hline},
every nth row={1}{before row= & & & \\\hline},
every last row/.style={after row= & & & \\\hline},
display columns/0/.style={string type,column type=|C},
display columns/1/.style={string type,column type=|C},
display columns/2/.style={string type,column type=|C},
display columns/3/.style={string type,column type=|C|}
\llap{%
\pgfplotstabletypeset[
columns/0/.style={column type=|C},
columns/1/.style={column type=|C},
columns/2/.style={column type=|C},
columns/3/.style={column type=|C|},
every head row/.style={output empty row,before row=\multicolumn{4}{c}{}\\[2ex]},
every first row/.style={before row=,after row=& & &  \\},
every nth row={1}{after row=& & &  \\},
every last row/.style={output empty row,after row=\multicolumn{4}{c}{}},
}

\end{document}


-

You can use the pgfplotstable for this to get the element of the particular row column however if this is your real example then you can come up with the corresponding capital letter without tables. But I guess this is a simplified case.

\documentclass{article}
A        B         C          D
E        F         G          H
I        J         K          L
M        N         O          P
}\firsttable
1      2    3     4
5      6    7     8
9     10   11    12
11    12   13    14
}\secondtable

\begin{document}
\begin{tikzpicture}
\draw[step=1.0cm,color=gray] (-2,-2) grid (2,2);

\foreach\x[evaluate=\x as \xi using int(\x-1)] in {4,...,1}{
\foreach\y[evaluate=\y as \yi using int(4-\y)] in {1,...,4}{
\pgfplotstablegetelem{\yi}{[index]\xi}\of\firsttable
\node (nup-\x-\y) at ({(2*\x-1)/2-2},{(2*\y-1)/2-2+0.25}) {\pgfplotsretval};
\pgfplotstablegetelem{\yi}{[index]\xi}\of\secondtable
\node (ndown-\x-\y) at ({(2*\x-1)/2-2},{(2*\y-1)/2-2-0.25}) {\pgfplotsretval};
}
}
\end{tikzpicture}
\end{document}


-