How to loop through reading .txt files while conditioning on line no.?

Click For Summary
SUMMARY

This discussion focuses on implementing a loop in LaTeX to read and conditionally process lines from two text files, "english.txt" and "spanish.txt", based on a specified line number from "line-no.txt". The original loop encountered issues due to the \ifnum condition being satisfied prematurely, leading to only a single iteration. The user sought a more effective solution than the work-arounds provided, emphasizing the need for compatibility with \epsilon-\TeX or \LaTeX for proper functionality.

PREREQUISITES
  • Familiarity with LaTeX programming and syntax
  • Understanding of file input/output operations in LaTeX
  • Knowledge of conditionals and loops in LaTeX
  • Experience with \epsilon-\TeX extensions
NEXT STEPS
  • Explore advanced LaTeX programming techniques for file handling
  • Learn about the differences between \LaTeX and \epsilon-\TeX
  • Investigate debugging strategies for LaTeX code
  • Study the use of macros in LaTeX for more complex data processing
USEFUL FOR

LaTeX developers, technical writers, and anyone involved in document preparation requiring conditional data processing from external text files.

Eclair_de_XII
Messages
1,082
Reaction score
91
TL;DR
I have two files with the same line count. I want to read both of them in tandem and pass each line of said files as arguments to some two-parameter macro. I also want to print something on the line numbers specified by a different file I'm reading. How would I accomplish this?
[CODE title="style.sty"]\newcommand{\printNum}[2]{%
#1 #2%
}

\newread\readEng
\newread\readEsp

\newread\readLineNo

\newcount\myLineCount
\myLineCount=0

\newcommand{\printNumRow}{\loop\global\advance\myLineCount by 1\read\readEng to \Eng \read\readEsp to \Esp \printNum{\Eng}{\Esp}\ifnum\myLineCount=\linenum \read\readLineNo to \linenum \par The next line number to stop at is: \linenum\par\repeat}[/CODE]

[CODE title="main.tex"]\documentclass{minimal}

\usepackage{style}

\begin{document}

\input{body}

\end{document}[/CODE]

[CODE title="body.tex" highlight="8"]\openin\readLineNo=line-no.txt

\openin\readEng=english.txt
\openin\readEsp=spanish.txt

\read\readLineNo to \linenum

%\loop\advance\myLineCount by 1 \ifnum\myLineCount=\linenum \read\readLineNo to \linenum \par The next line number to stop at is: \linenum\par\else \read\readEng to \Eng \read\readEsp to \Esp \printNum{\Eng}{\Esp}\repeat

\printNumRow{}
\printNumRow{}
\printNumRow{}

\closein\readEng
\closein\readEsp

\closein\readLineNo[/CODE]

For some reason, the loop in line 8 of body.tex only runs the one time. I'm guessing the loop terminated because the \ifnum condition was satisfied. The \printNumRow{} calls were work-arounds to the problem. Can anyone think of a better one?
 

Attachments

Physics news on Phys.org
If anyone's interested, I fixed my code. It doesn't actually work, though, unless you typeset this using \epsilon-\TeX or \LaTeX, since I'm calling the \unless macro from \epsilon-\TeX.

[CODE title="style.tex"]\def\printNum{
\read\readEng to \Eng
\read\readEsp to \Esp
\Eng\ \Esp\par}

\newread\readEng
\newread\readEsp
\newread\readLineNo

\def\openAll{
\openin\readEng=english.txt
\openin\readEsp=spanish.txt
\openin\readLineNo=line-no.txt
}

\def\closeAll{
\closein\readEng
\closein\readEsp
\closein\readLineNo
}

\newcount\myLineCount

\def\printEngEspDict{
\loop
\read\readLineNo to \linenum
\unless\ifeof\readLineNo

{\loop
\ifnum\myLineCount=\linenum
\relax\number\myLineCount
\else
\printNum
\global\advance\myLineCount by 1
\repeat}

\repeat
}[/CODE]

[CODE title="body.tex"]\input{style}

\openAll

\printEngEspDict

\closeAll[/CODE]
 
Last edited: