5

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.

  • 2
    Possible duplicate: tex.stackexchange.com/questions/116078/… – Ethan Bolker Dec 26 '13 at 13:05
  • thanks Ethan... in the question you mention the all reading and expanding is done in a Loop at one time, this is very easy to accomplish; but the command in my question when encountred read the content of the actual line in the external file and put the content of this line. the needed operation is the opposite of indexing where content is writing to the .idx file... hope that is clear. – moyoumos Dec 28 '13 at 8:48
  • Please edit the question to provide a sample showing what the text file looks like (after python has done its job), what you've tried or imagine for the TeX file, and what the output should look like. – Ethan Bolker Dec 28 '13 at 15:38
5

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.

  • i think that procedures to open the file must go to an \atBeginDocument and those to close it to \atEndDocument with some verifications (\iffileexists, \ifeof,...)... [a paralel question: is it necessary to close auxiliary files???]... thanks,Ethan. – moyoumos Dec 30 '13 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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