Überarbeitungsverlauf[Zurück]
Klicke auf Einblenden/Ausblenden von Überarbeitungen 5

12 Mai '15, 23:17

Henri's gravatar image

Henri
15.7k133943

# 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. \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][1] # LuaTeX Mit dem `io`-Modul von Lua kann man auch Dateien einlesen. \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. \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`. \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 `pgfplotstable`ist zu Beachten, dass `table/header=false` gesetzt ist, sonst wird die erste Zeile als Spaltenname interpretiert. \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} [1]: http://texwelt.de/wissen/upfiles/u_179.png
Klicke auf Einblenden/Ausblenden von Überarbeitungen 4

12 Mai '15, 23:01

Henri's gravatar image

Henri
15.7k133943

# 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. \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][1] # LuaTeX Mit dem `io`-Modul von Lua kann man auch Dateien einlesen. \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. \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`. \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} [1]: http://texwelt.de/wissen/upfiles/u_179.png http://texwelt.de/wissen/upfiles/u_179.png
Klicke auf Einblenden/Ausblenden von Überarbeitungen 3

12 Mai '15, 22:55

Henri's gravatar image

Henri
15.7k133943

Klicke auf Einblenden/Ausblenden von Überarbeitungen 2

12 Mai '15, 22:44

Henri's gravatar image

Henri
15.7k133943

Klicke auf Einblenden/Ausblenden von Überarbeitungen 1

12 Mai '15, 21:36

Henri's gravatar image

Henri
15.7k133943

Willkommen, erstes Mal hier? Schau mal unter FAQ!

×