What is the best way to load external data into a tikzpicture? I have a list of comma-separated pairs of values, i.e. coordinates, e.g. (1,1),(1,2),... to use to style cells in a grid.
In the image below I have a chessboard style grid. I want to highlight coordinates in red from a given list. I have found a way to load the matrix data using the pgfplotstable package. Perhaps there is a better approach? Can I likewise import a list of coordinates?
\documentclass[tikz,margin=0mm]{standalone} \usetikzlibrary{matrix} \tikzset{ every matrix style/.style={ matrix of nodes, nodes in empty cells, nodes={ minimum size=10mm, inner sep=0pt, outer sep=0pt, anchor=center, draw=none, fill=none, text=black, }, every even column/.style={ every odd row/.append style={ nodes={ fill=black, text=white, }}}, every odd column/.style={ every even row/.append style={ nodes={ fill=black, text=white, }}}, }} % set a list of cells to highlight \tikzset{ special cell/.style args = {(#1,#2)}{ row #2 column #1/.style={ nodes={ minimum size=10mm, inner sep=0pt, outer sep=0pt, draw=none, fill=red, }}}, highlight special cell/.style = { special cell/.list={ (1,2) } }, } % get data from file \usepackage{pgfplotstable} \pgfplotstableset{ col sep=comma, header=false, debug=true, numeric type, skip coltypes=true, every head row/.style={output empty row}, typeset cell/.code={% \ifnum\pgfplotstablecol=\pgfplotstablecols \pgfkeyssetvalue{/pgfplots/table/@cell content}{#1\\}% \else \pgfkeyssetvalue{/pgfplots/table/@cell content}{#1\pgfmatrixnextcell}% \fi }, begin table={\matrix (M) [every matrix style] \bgroup}, end table={\egroup;}, } % matrix data \begin{filecontents*}{data-matrix.csv} 1,2 3,4 \end{filecontents*} % special coordinates \begin{filecontents*}{data-cells.txt} (1,2) \end{filecontents*} \begin{document} \begin{tikzpicture} \matrix (M) [every matrix style, highlight special cell]{ 1&2\\ 3&4\\ }; \end{tikzpicture} % load external data to build table \pgfplotstableread[skip first n=0]{data-matrix.csv}\mytable % question: can I import the list of special cells too?\\ % maybe [highlight special cell from list = \mycells] \pgfplotstableread[skip first n=0]{data-cells.txt}\mycells \begin{tikzpicture} \pgfplotstabletypeset{\mytable} \end{tikzpicture} \end{document} 
\def\mycells{(1,2), (1,4)}you can\inputthe file and use the macro. You might need to use theexpand oncekey handler (page 989-990).\pgfplotstablesetdo what I intended. I tried something similar for the coordinates, but it didn't work. I'm not picky about the approach!data-cells.txtfile would contain\def\mycells{(1,2), (1,4)}. How do I "expand"? I tried variations of[special cell/.list=\mycells]without success.special cell/.list/.expand once=\mycellsis needed otherwise PGFfor just sees\mycellsas one entry in the list.