1

Yes, TeX can read a line in a file and use it as input. The example working code below now implements the accepted answer from Willie Wong. For the new user: "\inputL" and "\inputR" are not TeX commands. Mr. Wong's answer states that they are macros. A TeX macro is something like a variable that is declared when the macro name is first used, and a string value is read into it. It is something like a function because when the macro name is used a second time, when it will return the string value. Source files, desired output, and example code follow.

Please assume source files with these contents.

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.

Desired output document should have a left page and a right page.

Left page should have read and input one paragraph:

This is paragraph 1 from LeftPageParagraphs.tex file.

Right page should have read and input one paragraph:

This is paragraph 1 from RightPageParagraphs.tex file.

An annotated minimum working example of the solution:

\documentclass[openany]{book}

\usepackage{paracol} % Parallel columns package

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

\begin{document}

%  Open *.tex files in input streams 0 and 1).
\openin0 = {LeftPageParagraphs}
\openin1 = {RightPageParagraphs}

\begin{paracol}[1]*{2}     % Open parallel columns "environment".
    
    \read0 to \inputL  % <-- Reads first line from first file.
    \read1 to \inputR  % <-- Reads first line from second file. 
        
    \chunks            % <-- Pass: Adds line from each file as paragraph.
        {\inputL}  
        {\inputR}  

\end{paracol}

% Close files (input streams 0 and 1).
\closein0
\closein1

\end{document} 
22
  • Oh, you're using primitive TeX commands? Where are you finding the documentation? TeXbook? If not, it's not "the documentation". (although TeX by Topic is also pretty good)
    – user202729
    Jan 22, 2022 at 2:32
  • 1
    Do you happen to have shell-escaped enabled or LuaTeX? Programming in Lua is way more convenient.
    – user202729
    Jan 22, 2022 at 2:33
  • latexref.xyz/_005cread.html suggests you would want first \read0 to \inputL followed by \read1 to \inputR and then call \chunks{\inputL}{\inputR}. (I haven't tested it since there were no Chapter01Left and ...Right files provided for testing.) Jan 22, 2022 at 2:36
  • 2
    reading a file is easy. The tricky part is if the file you're reading isn't actually tex input. What does your file look like?
    – Teepeemm
    Jan 22, 2022 at 3:02
  • \read0 to \input would redefine the command \input making it impossible to \input{myfile} also don't use stream numbers directly, always use \newread to allocate a number for your use. Jan 22, 2022 at 11:07

1 Answer 1

1

(Converted from comment)

The command \read<stream> to<macro> stores the next line of input from <stream> and stores it as <macro>.

So if the file you are reading starts

Hello World!
Hi There.

Then

\read0 to\InPutWorld
\read0 to\InPutThere

will have the same effect as

\def\InPutWorld{Hello World!}
\def\InPutThere{Hi There.}

Hence, to accomplish what you want, you probably should:

\read0 to \inputL
\read1 to \inputR 
\chunks{\inputL}{\inputR}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .