Skip to main content
deleted 7 characters in body
Source Link
PatrickT
  • 3.5k
  • 3
  • 28
  • 37
\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,1), (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} 
\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,1), (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} 
\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} 
minor typo
Source Link
PatrickT
  • 3.5k
  • 3
  • 28
  • 37
\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 from list/.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,1), (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} 
\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 from list/.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,1), (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} 
\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,1), (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} 
Source Link
PatrickT
  • 3.5k
  • 3
  • 28
  • 37

tikz: Import coordinates from external file

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 from list/.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,1), (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