6

I would like to include the following data which is now in a separate doses.data file

0   1
10  1
11  1
12  1
0   2
1   2
4.8 2

into my TikZ drawing. Right now I'm importing the file, but I want the actual data inside my tikzpicture environment:

\documentclass{minimal}
\usepackage{tikz}

%%%<
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
%%%>


\begin{document}
\begin{tikzpicture}[y=.2cm, x=.7cm,font=\rmfamily]
    %axis
    \draw (0,0) -- coordinate (x axis mid) (21,0);
        %ticks
        \foreach \x in {0,...,21}
            \draw (\x,0pt) -- (\x,-3pt)
            node[anchor=north] {\x};
    %labels      
    \node[below=0.8cm] at (x axis mid) {Dose [Gy]};

    \draw plot[mark=*, only marks, mark size=2] 
        file {doses.data};

\end{tikzpicture}
\end{document}

enter image description here

To clarify, I don't want to include file {doses.data};, but the contents of doses.data instead. What is the correct syntax to do this?

Edit: If there's a simpler solution to this with pgfplots, I'm in, but I'd prefer TikZ since I would like to add things later on.

Eekhoorn
  • 3,567
  • You can always use TikZ “to add things later on” with the axis cs coordinate system. (→ pgfplots manual, subsection 4.16.1 “Accessing Axis Coordinates in Graphical Elements”, pp. 255f.) – Qrrbrbirlbel Jan 20 '13 at 17:18
  • I'm not sure if I got your question correctly but this might help. It also uses tikzpicture environment. – Pouya Jan 20 '13 at 17:35
  • To clarify, I don't want to write file {doses.data};, but the contents of doses.data instead. – Eekhoorn Jan 20 '13 at 17:47

1 Answers1

5

You can embed like this:

\draw plot[mark=*, only marks, mark size=2] coordinates {(0,1) (10,1) (11,1) (12,1) (0,2) (1,2) (4.8,2)};

OR:

A better way, in which no modification of coordinates is needed:

\documentclass{standalone}
\usepackage{tikz}
\usepackage[english]{babel}
\usepackage{filecontents}
\begin{filecontents}{doses.data}
    # Header goes here
    0   1
    10  1
    11  1
    12  1
    0   2
    1   2
    4.8 2
\end{filecontents}
\begin{document}
\begin{tikzpicture}[y=.2cm, x=.7cm,font=\rmfamily]
    \draw (0,0) -- coordinate (x axis mid) (21,0);
        \foreach \x in {0,...,21}
            \draw (\x,0pt) -- (\x,-3pt)
            node[anchor=north] {\x};
    \node[below=0.8cm] at (x axis mid) {Dose [Gy]};
    \draw plot[mark=*, only marks, mark size=2] file {doses.data};
\end{tikzpicture}
\end{document}

But this technique still use file. File created by the LaTeX itself on the first run. Standard version of filecontents environment did not update files upon modification of appropriate content, it would be better to use package filecontents which does.

m0nhawk
  • 9,664