[Tex/LaTex] Put from external file

automationcitingcontent-replicationexternal filesindexing

I need to write a command that read a line from a text file and expand to the content of this line, the next command read the next line and so on.

...
\command{arg}
...
\command{arg}
...
\command{arg}
...

the macro argument can't be used as an index in the external file, and that the external file is produced by a python script.

Ethan:: the text file contain Arabic text generated in a line by line form by the python script… in a first pass of LateX (XeLateX) each \command write its argument in a fileIn.txt (Arabic text)… the python script take this file and produce fileOut.txt… in the second pass of Latex each \command will read its corresponding Line from fileOut.txt…
The two text files contain simple line by line Arabic text…

line one
line two
.
.
.
last line

here is a MnWE:

usepackage{}
...
...
\newcommand{\command}[1]{....#1...}
...
\begin{document}
...
...
\command{arg0}
...
...
\command{arg1}
...
...
...
\command{arg2}
...
...
\command{arg3}
...

\end{document}

in the first pass \command{..} is typically like \index{..} wich WRITE content in an external file… but in second pass i havn't any idea about the command in ()TeX wich PUTS a content from an external file… hope all is clear now.

Best Answer

This may meet your needs. I found it at File input and output; I'm reproducing it (somewhat simplified) here:

With this sample fileOut.txt:

first line, with a \TeX{} macro to expand
second line
third line

the code

\documentclass{article}

\newread\file
\openin\file=fileOut.txt

\newcommand{\getnextline}{%
 \read\file to\fileline % Reads a line of the file into \fileline
 \fileline % display it
}

\begin{document}

First line of fileOut.txt: 

\getnextline{}

More text here, then second line: \getnextline{}

Third line: \getnextline{}

Read past end of file? \getnextline{}
\closein\file

\end{document}

produces

enter image description here

I hope this works for you in XeTeX with Arabic text.

Edit to answer the OP's further questions:

Since the example here works, the open and close statements are where they belong. Good programming practice (in any language) would call for checks that the file exists and is readable. I didn't do that since this answer is just a proof of concept. If you are in complete control of your environment and know the file will always be where it's expected, you need not bother.

I don't know what happens if you don't close an open file. I do know that TeX limits the number of files that you can have open at the same time. Some applications aren't good about releasing file handles by themselves, so it's a good habit to close them yourself.

I was curious about what would happen if I read past the end of the file. The example shows that reading a line that's not there just returns an empty string. You need to decide whether that's acceptable in your use case. If not, test for eof and act accordingly.

Related Question