How to import ASCII coordinates to TikZ

I have a long file of Brazil coordinates like:

(-3.43,-0.56) -- (-3.44,-0.56) -- (-3.44,-0.55) -- (-3.50,-0.55) -- (-3.52,-0.55) ...


and I want to import then into a tikzpicture environment to use with the \draw command. Some thing like this:

\documentclass{minimal}
\usepackage{tikz}
\begin{document}
\pagestyle{empty}

\begin{tikzpicture}
\draw 'myfile'
\end{tikzpicture}

\end{document}

-
What about using a shell script to transform the source file into TikZ code? –  Matthew Leingang Apr 9 '12 at 15:57

You can read the content of a file in an expandable way using TeX's \input, which however is renamed to \@@input by LaTeX in order to provide a higher level \input. The \draw command (actually \path) of TikZ will expand the following tokens first before looking for the coordinates.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\makeatletter
\draw\@@input coordfile ;
\makeatother
\end{tikzpicture}
\end{document}


Here I assume the coordinates are in coordfile.tex or coordfile (no extension). You should add a trailing space after the filename and avoid filenames with special, i.e. non-English characters and of course spaces.

If you need this more than once you should declare a different name for \@@input in the preamble and use this in the body instead:

\documentclass{article}
\usepackage{tikz}
\makeatletter
\let\expinput\@@input
\makeatother
\begin{document}
\begin{tikzpicture}
\draw\expinput coordfile ;
\end{tikzpicture}
\end{document}


or, more LaTeX'ish:

\documentclass{article}
\usepackage{tikz}
\makeatletter
\newcommand*\expinput[1]{\@@input #1 }
\makeatother
\begin{document}
\begin{tikzpicture}
\draw\expinput{coordfile};
\end{tikzpicture}
\end{document}

-

You can read from files in TeX. Presume you have a file foo with content (0,0) -- (2,0) -- (2,2) Then you can read that line and pass it to a draw command as follows:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\openin\bar=foo
\tikz\draw\res;
\end{document}


And that will draw the corresponding lines. If you have multiple lines in a file then \read\bar to \res a second time will read the second line from the file. After you are done with it, you should \closein\bar. There is also an \ifeof which you can use to loop over the file until it contains no more lines.

To go through the entire file, you can simply write a macro that loops as long as there are things to read. For this simple setup to work, the first command in the loop has to be the read of the file.

\documentclass{article}
\usepackage{tikz}
\def\myloop#1#2#3\repeat{\def\filenm{#2}\def\body{#1#2#3}\iterate}
\def\iterate{\body\ifeof\filenm\let\next\relax\else\let\next\iterate\fi\next}
\begin{document}

This will add every line of the file to the same tikzpicture.
If there are multiple lines, the lines will not share the same coordinates as each is draw in its own \tikzpicture. –  Peter Grill Apr 9 '12 at 14:40
This can be simplified by using the plainTeX version of \input which is fully expandable. See my answer. Of course then all lines will be plotted at once. –  Martin Scharrer Apr 9 '12 at 15:29
If the OP uses this exact method for multiple lines as well, then no. He can of course do the reads inside of a single tikzpicture environment depending on the results he wants. I'll add an example where it reads the entire file. –  Roelof Spijker Apr 9 '12 at 15:35