# How to draw multiple Rectangles from input data file

I want to draw set of rectangles on axis based on input file. My .tex file should be some thing like that

\documentclass{article}
\usepackage{tikz,pgf}
\usetikzlibrary{plotmarks}
\usepackage{pgfplots, pgfplotstable}
\usetikzlibrary{fit,calc}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
width=8cm,
height=8cm,
scale only axis,
xmin=-1, xmax=20,
ymin=-1, ymax=20,
xlabel={i},
ylabel={j},
title={Hi}]
for each row in table
if (\gg=0)
{
\draw [black, fill=blue, thick] (axis cs:\x,\y) rectangle (axis cs:\xx,\yy);
\end{axis}
else if (\gg=1)
\draw [black, fill=red, thick] (axis cs:\x,\y) rectangle (axis cs:\xx,\yy);
\end{axis}
else if (\gg=2)
\draw [black, fill=red, thick] (axis cs:\x,\y) rectangle (axis cs:\xx,\yy);
\end{axis}
\end{tikzpicture}
\end{document}


Where as my input file look like

%% data.dat
x   y   xx  yy  gg
0   0   5   4   0
0   4   5   9   1
0   9   5   13  0
0   13  5   16  1
5   0   9   5   2


How can I draw them?

-
Welcome to TeX.SE! –  percusse Nov 13 '12 at 0:25
What is the difference between \gg=1 and \gg=2? –  Qrrbrbirlbel Nov 13 '12 at 0:49
Cross-posting is no-go unless you link properly (if you ask me)! latex-community.org/forum/viewtopic.php?t=21907 –  Svend Tveskæg Nov 13 '12 at 2:30

The following solution does use datatool instead of pgfplotstable (I can't find a reference to loop over data in a pgfplotstable) and is based on a previous answer of mine.

I replaced all TAB characters with , as TAB is not very LaTeX-friendly and , is the default separator.

The solution uses etoolbox's tools to concatenate all \draw commands in one macro \mydraws.

There are two macros used:

• \appto<macro>{<code>} and
• \eappto<macro>{<code>}.

While the first one just appends <code> to the macro <macro> the latter will expand <code> first.
We don't want \draw to be expanded because TikZ should deal with it alone; but we want \c, \x, \y, \xx and \yy expanded because the hold the actual values. If we wouldn't expand these macros they would first be expanded when we use \mydraws inside the \tikzpicture but now they all have the value of the last row, and we would only get one box (but drawn five times) …

## Code

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{datatool}
\usepackage{etoolbox}
\begin{filecontents}{data.dat}
x,y,xx,yy,gg
0,0,5,4,0
0,4,5,9,1
0,9,5,13,0
0,13,5,16,1
5,0,9,5,2
\end{filecontents}
keys={x,y,xx,yy,gg}
]{data}{data.dat}
\begin{document}
\def\mydraws{}% \def is a TeX primitive, one could have said \newcommand*\mydraws{}, too.
\DTLforeach*{data}{\x=x,\y=y,\xx=xx,\yy=yy,\gg=gg}{
%   \PackageWarning{}{x=\x, y=\y, xx=\xx, yy=\yy, gg=\gg} % DEBUG
\def\c{blue}
\ifnum\gg=0\relax
\def\c{blue}
\else\ifnum\gg=1\relax
\def\c{red}
\else
\def\c{red}
\fi\fi
\appto\mydraws{\draw}% \draw will not be expanded
\eappto\mydraws{ [black, fill=\c, thick] (axis cs:\x,\y) rectangle (axis cs:\xx,\yy);}% \c, \x, \y, \xx and \yy will be expanded to their actual value.
}
\begin{tikzpicture}
\begin{axis}[
width=8cm,
height=8cm,
scale only axis,
xmin=-1, xmax=20,
ymin=-1, ymax=20,
xlabel={i},
ylabel={j},
title={Hi}]
\mydraws
\end{axis}
\end{tikzpicture}
\end{document}


## Ouput

-
Thanks for answering me.Both solutions look exactly what I want. But when I try to compile them, they give errors that I don't understand (As I am a newbie on Latex) @Qrrbrbirlbel Solution the error is Runaway argument? \@nil \let \@dtl@line =\@dtl@trmstr \ifx \@dtl@line \@longempty \@dtl@condition true \ETC. ! File ended while scanning use of \@dtl@trim. <inserted text> \par <*> solution_file.tex @percusse solution ! Missing number, treated as zero. <to be read again> \mycolor l.30 ]{drawer}{\mytable} Thanks –  TaniaMalik Nov 13 '12 at 17:54
The OP of the other question ran in the same problem. Do you maybe have a blank line in your data.dat? Otherwise try to remove (or comment out) lines from the file and test again. –  Qrrbrbirlbel Nov 13 '12 at 19:41
Thank you so much. It solve the problem :) –  TaniaMalik Nov 14 '12 at 10:49

Also a pgfplotstable solution with a custom column cretaed from existing ones.

\documentclass{article}
\usepackage{pgfplots,pgfplotstable,filecontents}
\pgfplotsset{compat=1.7}
\begin{filecontents*}{data.dat}
x   y   xx  yy  gg
0   0   5   4   0
0   4   5   9   1
0   9   5   13  0
0   13  5   16  1
5   0   9   5   2
\end{filecontents*}

\begin{document}
\pgfplotstablecreatecol[
create col/assign/.code={%
\getthisrow{x}\pointaa
\getthisrow{y}\pointab
\getthisrow{xx}\pointba
\getthisrow{yy}\pointbb
\getthisrow{gg}\mycolor
\pgfmathparse{\mycolor<2?(\mycolor<1?"blue":"red"):"green"}
\edef\temp{%
\noexpand\draw[black,fill=\pgfmathresult]
(axis cs:\pointaa,\pointab) rectangle (axis cs:\pointba,\pointbb);}
\pgfkeyslet{/pgfplots/table/create col/next content}\temp
}
]{drawer}{\mytable}

\begin{tikzpicture}
\begin{axis}[
width=8cm,
height=8cm,
scale only axis,
xmin=-1, xmax=20,
ymin=-1, ymax=20,
xlabel={i},
ylabel={j},
title={Hi},
]
\pgfplotstableforeachcolumnelement{drawer}\of\mytable\as\cell{\cell}
\end{axis}
\end{tikzpicture}
\end{document}


-
Thanks for answering me.Both solutions look exactly what I want. But when I try to compile them, they give errors that I don't understand (As I am a newbie on Latex) @Qrrbrbirlbel Solution the error is Runaway argument? \@nil \let \@dtl@line =\@dtl@trmstr \ifx \@dtl@line \@longempty \@dtl@condition true \ETC. ! File ended while scanning use of \@dtl@trim. <inserted text> \par <*> solution_file.tex @percusse solution ! Missing number, treated as zero. <to be read again> \mycolor l.30 ]{drawer}{\mytable} Thanks –  TaniaMalik Nov 13 '12 at 17:54