如何在循环条件下检测两个源文件的 EOF?

如何在循环条件下检测两个源文件的 EOF?

对于两种语言的批判版本,我的Main.tex文件有一个循环:

  1. 从第一个源文件读取一行
  2. 从第二个源文件读取一行
  3. \chunks使用命令输出行
  4. 通过检测第一个源文件的EOF来\ifeof0控制\loop

更好的方法\loop是检查两个源文件的 EOF。但是,TeXstudio 不会编译\ifeof0\ifeof1处于循环状态。有什么建议吗?


下面是源文件和示例代码。

请假设源文件包含这些内容。

LeftPageParagraphs.tex

This is paragraph 1 from LeftPageParagraphs.tex file.

This is paragraph 2 from LeftPageParagraphs.tex file.

RightPageParagraphs.tex:

This is paragraph 1 from RightPageParagraphs.tex file.

This is paragraph 2 from RightPageParagraphs.tex file.

带注释的问题最小工作示例:

% Test: read lines from files.

\documentclass[openany]{book}

\usepackage{paracol}  % Parallel columns package
\usepackage{forloop}
\usepackage{ifthen}

% The \chunks command outputs two parallel paragraphs 
% to left and right columns on facing pages.
\newcommand\chunks[2]{%
    \begin{leftcolumn*}
        {#1}%
    \end{leftcolumn*}%
    \begin{rightcolumn}
        {#2}%
    \end{rightcolumn}%
    }

\begin{document}

    % Algorithm:
    % Read a line (a paragraph) from input stream 0.
    % Read a line (a paragraph) from input stream 1.
    % Output line to left column on left page.
    % Output line to right column on right page.    
    % Detect end-of-file in input stream 0.

    % PASS with condition: \ifeof0
    % FAIL with condition: { \( \ifeof0 \and \ifeof1 \) }
    % ERROR: Incomplete \ifeof; all text was ignored after line 49.
    
    % Open files in input streams 0 and 1.
    \openin0 = LeftPageParagraphs
    \openin1 = RightPageParagraphs
    
    \begin{paracol}[1]*{2}  
        
        \loop 
        { 
            \read0 to \inputL  
            \read1 to \inputR
  
            \chunks
                {\inputL}
                {\inputR}
        }       
        \( \ifeof0 \and \ifeof1 \) % <-- ERROR
        \repeat

    \end{paracol}
    
    % Close files in input streams 0 and 1.
    \closein0
    \closein1
    
\end{document}

答案1

\( \ifeof0 \and \ifeof1 \) 

\(\) 命令排版数学,不用于评估逻辑表达式

\and\maketitle是用于区分作者的命令

\ifeof0访问流 0,该流被 LaTeX 保留为\IFFileExistsfilecontents其他文件相关结构的一部分。

用两个已分配的流替换流 0 和 1

\newread\streamL
\newread\streamR

并结合测试,例如\if..在循环中使用新命令

\newif\ifcontinue

\loop
...
\continuetrue
\ifeof\streamL
   \ifeof\streamR
      \continuefalse
   \fi
\fi
\ifcontinue
\repeat

循环将继续,直到两个流都到达 eof。

相关内容