Hallo liebe LaTeX-Freunde,

ich würde gerne eine bestimmte Zeile einer externen .dat-Datei einlesen um diesen String dann in einem tikz Bild einzufügen. Hier mein Minimalbsp.:

Open in Online-Editor
\documentclass[margin=2mm, tikz]{standalone}
\usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}[xmin=0,xmax=2,ymin=0,ymax=2]
    \begin{axis}
    \node at (axis cs:1,1){***};
    \end{axis}
    \end{tikzpicture}
 \end{document}

Wobei *** hier Zeile 4 aus meiner externen Datei dumm.dat ist. Ist dies so einfach möglich bzw. gibt es hierfür ein Paket?

Dieser Frage ist "Community Wiki" markiert.

gefragt 12 Mai '15, 18:54

Ross's gravatar image

Ross
37571522
Akzeptiert-Rate: 100%

bearbeitet 12 Mai '15, 22:11

esdd's gravatar image

esdd
17.7k254256


expl3

Mit dem l3file-Modul aus expl3 (geladen von xparse im MWE) lässt sich eine Datei leicht Zeilenweise einlesen. Man sollte dabei bedenken, dass der Inhalt der Zeilen von TeX expandiert wird, also wenn z.B. irgendwo % steht wird der Rest der Zeile ignoriert.

Open in Online-Editor
\documentclass[tikz]{standalone}
\usepackage{pgfplots,xparse}
\begin{filecontents*}{\jobname.dat}
aaa
bbb
ccc
***
ddd
\end{filecontents*}

\ExplSyntaxOn

\int_new:N \l_ross_linecount_int
\ior_new:N \l_ross_infile_stream

\cs_new_protected:Npn \ross_getline:nn #1#2
 {
  \int_set:Nn \l_ross_linecount_int { 1 }
  \ior_open:Nn \l_ross_infile_stream { #1 }
  \ior_map_inline:Nn \l_ross_infile_stream
   {
    \int_compare:nT { \l_ross_linecount_int == #2 } { ##1 }
    \int_incr:N \l_ross_linecount_int
   }
  \ior_close:N \l_ross_infile_stream
 }

\NewDocumentCommand \RowOfFile { m m }
 {
  \ross_getline:nn { #1 } { #2 }
 }

\ExplSyntaxOff

\begin{document}
\begin{tikzpicture}
  \begin{axis}[xmin=0,xmax=2,ymin=0,ymax=2]
    \node at (axis cs:1,1) {\RowOfFile{\jobname.dat}{4}};
  \end{axis}
\end{tikzpicture}
\end{document}

alt text

LuaTeX

Mit dem io-Modul von Lua kann man auch Dateien einlesen.

Open in Online-Editor
\documentclass[tikz]{standalone}
\usepackage{pgfplots,luacode}
\begin{filecontents*}{\jobname.dat}
aaa
bbb
ccc
***
ddd
\end{filecontents*}
\begin{luacode*}
function getline(filename, lineno)
   linecount = 0
   fp = io.open(filename)
   while true do
      linecount = linecount + 1
      line = fp:read()
      if line == nil then
         break
      end
      if lineno == linecount then
         return tex.sprint(line)
      end
   end
end
\end{luacode*}
\DeclareRobustCommand\RowOfFile[2]{\directlua{getline("#1",#2)}}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[xmin=0,xmax=2,ymin=0,ymax=2]
    \node at (axis cs:1,1) {\RowOfFile{\jobname.dat}{4}};
  \end{axis}
\end{tikzpicture}
\end{document}

plain TeX

Man kann eine Datei auch nur mit plain-TeX-Makros einlesen. Diese Definition von \RowOfFile ist auch LaTeX-kompatibel.

Open in Online-Editor
\input pgfplots.tex

% Allocate handles
\newwrite\outfile
\newread\infile
\newcount\lineno

% Write dummy data
\immediate\openout\outfile=\jobname.dat
\immediate\write\outfile{aaa}
\immediate\write\outfile{bbb}
\immediate\write\outfile{ccc}
\immediate\write\outfile{***}
\immediate\write\outfile{ddd}
\immediate\closeout\outfile

\def\RowOfFile#1#2{%
  \lineno=0
  \openin\infile=#1
  \loop\ifnum\lineno<#2
    \read\infile to \curline%
    \advance\lineno by 1
  \repeat%
  \closein\infile%
  \curline
}

\tikzpicture
  \axis[xmin=0,xmax=2,ymin=0,ymax=2]
    \node at (axis cs:1,1) {\RowOfFile{\jobname.dat}{4}};
  \endaxis
\endtikzpicture

\bye

LaTeX

Eigentlich ist das nur eine Kopie der obigen plain-TeX-Definition, wobei einige Makros durch dokumentierte LaTeX-Makros ersetzt wurden, z.B. \def -> \newcommand.

Open in Online-Editor
\documentclass[tikz]{standalone}
\usepackage{pgfplots,xparse}
\begin{filecontents*}{\jobname.dat}
aaa
bbb
ccc
***
ddd
\end{filecontents*}
\newread\infile
\newcounter{lineno}
\newcommand\RowOfFile[2]{%
  \setcounter{lineno}{0}
  \openin\infile=#1
  \loop\ifnum\arabic{lineno}<#2
    \read\infile to \curline%
    \stepcounter{lineno}
  \repeat%
  \closein\infile%
  \curline
}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[xmin=0,xmax=2,ymin=0,ymax=2]
    \node at (axis cs:1,1) {\RowOfFile{\jobname.dat}{4}};
  \end{axis}
\end{tikzpicture}
\end{document}

pgfplotstable

Bei der Benutzung von pgfplotstableist zu Beachten, dass table/header=false gesetzt ist, sonst wird die erste Zeile als Spaltenname interpretiert.

Open in Online-Editor
\documentclass[tikz]{standalone}
\usepackage{pgfplots,pgfplotstable,xparse}
\begin{filecontents*}{\jobname.dat}
aaa
bbb
ccc
***
ddd
\end{filecontents*}
\pgfplotsset{table/header=false}
\newcommand\RowOfFile[2]{
  \pgfplotstableread{#1}\loadedtable
  \pgfplotstablegetelem{\numexpr#2-1\relax}{[index]0}\of\loadedtable
  \pgfplotsretval
}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[xmin=0,xmax=2,ymin=0,ymax=2]
    \node at (axis cs:1,1) {\RowOfFile{\jobname.dat}{4}};
  \end{axis}
\end{tikzpicture}
\end{document}
Permanenter link

beantwortet 12 Mai '15, 21:36

Henri's gravatar image

Henri
15.7k133943
Akzeptiert-Rate: 46%

bearbeitet 12 Mai '15, 23:17

Alternativ könnte man auch das Paket datatool verwenden:

Open in Online-Editor
\begin{filecontents*}{dumm.dat}
Zeile 1
Zeile 2
Zeile 3
Zeile 4
Zeile 5
\end{filecontents*}
\documentclass[margin=2mm]{standalone}
\usepackage{pgfplots}
\usepackage{datatool}
\DTLloaddb[noheader]{mylist}{dumm.dat}
\newcommand\listvalue{}
\newcommand\printlist[1]{%
  \DTLgetvalue\listvalue{mylist}{#1}{1}%
  \listvalue%
}
\begin{document}
    \begin{tikzpicture}
    \begin{axis}[xmin=0,xmax=2,ymin=0,ymax=2]
    \node at (axis cs:1,1){\printlist{4}};
    \end{axis}
    \end{tikzpicture}
 \end{document}

alt text

Beachten muss man dabei allerdings, dass das Komma als Spaltentrenner voreingestellt ist. Gegebenenfalls muss man den Spaltentrenner mit \DTLsetseparator{<zeichen>} vor dem Laden der Datei in ein anderes Zeichen ändern.

Verwendet man statt \DTLloaddb den Befehl \DTLlaodrawdb dann kann in der Zeile zum Beispiel auch ein Prozentzeichen stehen, dass dann auch als Prozentzeichen ausgegeben wird. Das gilt aber nicht für den Backslash.

Permanenter link

beantwortet 12 Mai '15, 22:20

esdd's gravatar image

esdd
17.7k254256
Akzeptiert-Rate: 62%

Deine Antwort
Vorschau umschalten

Folgen dieser Frage

Per E-Mail:

Wenn sie sich anmelden, kommen Sie für alle Updates hier in Frage

Per RSS:

Antworten

Antworten und Kommentare

Markdown-Grundlagen

  • *kursiv* oder _kursiv_
  • **Fett** oder __Fett__
  • Link:[Text](http://url.com/ "Titel")
  • Bild?![alt Text](/path/img.jpg "Titel")
  • nummerierte Liste: 1. Foo 2. Bar
  • zum Hinzufügen ein Zeilenumbruchs fügen Sie einfach zwei Leerzeichen an die Stelle an der die neue Linie sein soll.
  • grundlegende HTML-Tags werden ebenfalls unterstützt

Frage-Themen:

×11
×4

gestellte Frage: 12 Mai '15, 18:54

Frage wurde gesehen: 9,965 Mal

zuletzt geändert: 12 Mai '15, 23:17