Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
add comment

2 Answers

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}}

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

\begin{document}

\pgfplotstabletypeset[%
header=false,
every head row/.style={before row=\hline},
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|}
]{\loadedtableii}%
\llap{%
\pgfplotstabletypeset[
header=false,
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}{}},
]{\loadedtablei}%
}

\end{document}

enter image description here

share|improve this answer
add comment

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}
\usepackage{pgfplotstable}%<-loads TikZ and pgfplots anyway
\pgfplotstableread[header=false]{
A        B         C          D
E        F         G          H
I        J         K          L
M        N         O          P
}\firsttable
\pgfplotstableread[header=false]{
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}

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.