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

AI Thread Summary
The discussion revolves around a coding issue in a LaTeX document where a loop intended to read and print English and Spanish translations is not functioning as expected. The user identifies that the loop in line 8 of body.tex only executes once, likely due to the condition in the \ifnum statement being satisfied prematurely. Work-arounds using \printNumRow{} are mentioned, but the user seeks a more effective solution. Additionally, it is noted that the code requires \epsilon-\TeX or \LaTeX for proper functionality, as it utilizes the \unless macro specific to \epsilon-\TeX. The user also shares a revised definition for the \printNum command and outlines methods for opening and closing input files. The conversation highlights the challenges of managing loops and file reading in LaTeX programming.
Eclair_de_XII
Messages
1,082
Reaction score
91
TL;DR Summary
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:
Back
Top