Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
What about using a shell script to transform the source file into TikZ code? –  Matthew Leingang Apr 9 '12 at 15:57

2 Answers 2

up vote 7 down vote accepted

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}
share|improve this answer

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}
  \newread\bar
  \openin\bar=foo
  \read\bar to\res
  \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}
\newread\bar
\openin\bar=foo
\begin{tikzpicture}
  \myloop\read\bar to\res\draw\res;\repeat
\end{tikzpicture}
\end{document}

This will add every line of the file to the same tikzpicture.

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.