2

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} 

enter image description here

6
  • 1
    "Best way" or "better approach" is a difficult term as this really depends on what is best for you which we can't know as it is highly individual. Do you want simpler or shorter code? An approach that can be extended easily? As it stands now, your question is hard to answer. But inside your code you show something that you could maybe explain a bit better: "Can I import the list of special cells too?" Do you want something like a key? Commented May 21 at 18:57
  • 1
    If you are willling/able to include something like \def\mycells{(1,2), (1,4)} you can \input the file and use the macro. You might need to use the expand once key handler (page 989-990). Commented May 21 at 19:01
  • Thanks Jasper. As you can see, I had to fiddle a bit to get the \pgfplotstableset do what I intended. I tried something similar for the coordinates, but it didn't work. I'm not picky about the approach! Commented May 21 at 22:00
  • Thanks John. Do you mean that the data-cells.txt file would contain \def\mycells{(1,2), (1,4)}. How do I "expand"? I tried variations of [special cell/.list=\mycells] without success. Commented May 23 at 2:11
  • 1
    @PatrickT special cell/.list/.expand once=\mycells is needed otherwise PGFfor just sees \mycells as one entry in the list. Commented May 23 at 3:26

1 Answer 1

2

Based on suggestions by @John Kormylo and @Qrrbrbirlbel, I have a solution to the original question. However, I would love to see an answer based on key handlers, as @Jasper Habicht suggested, so I'll hold off selecting my own answer for now.

\documentclass[tikz]{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, }}}, } % 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-def.txt} \def\mycells{(1,2)} \end{filecontents*} \begin{document} % load external data to build table \pgfplotstableread[skip first n=0]{data-matrix.csv}\mytable % load external data to style matrix | where data is stored as \def\mycells{} \input{data-cells-def.txt} \begin{tikzpicture}[special cell/.list/.expand once=\mycells] \pgfplotstabletypeset{\mytable} \end{tikzpicture} \end{document} 
1
  • I'm accepting this until a better solution comes along... Commented Nov 11 at 20:08

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.