# parallel or other parallel text - paragraph alignment feeding from input files

I am scratching my head trying to sort out an issue with parallel text (in a bilingual document). So first off I have seen many examples on stackexchange regarding this problem but not quite the solution I am looking for.

In my document, I am supplying separate tex files for each chapter. So I have a set of files for one language, and then another set of files for the translation. I would like to keep it this way for various reasons, rather than write one big file with a ton of \ParallelPars every twenty lines or so to keep the paragraphs together. The different languages have significant differences in chapter length, about every page or so there is a difference of three lines so it adds up when it gets to the end (I could have an extra one or two pages of one language per chapter).

Is there any way to synchronize the chapters and paragraphs of the document fed via input files? How else might this be done? Another kind of parallel paragraph package maybe? One thing I am considering is playing with the textwidth so the language that is longer has a wider column (bringing the paragraph lengths closer together) but I don't think this will look very nice.

Also you will see I am useing pdfcolparallel, which helps with the use of colors in the document, which will become a PDF.

Example code:

\documentclass[10pt]{article}

\usepackage{yfonts,color,lettrine,pdfcolparallel}
\usepackage[top=.5in, bottom=1in, left=1in, right=1in]{geometry}

\begin{document}

\begin{Parallel}{.47\textwidth}{.47\textwidth}
\ParallelLText{\input{cv/doc1.tex}}
\ParallelRText{\input{dr/doc1.tex}}
\ParallelPar
\newpage
\ParallelLText{\input{cv/doc2.tex}}
\ParallelRText{\input{dr/doc2.tex}}
\ParallelPar
\newpage
\ParallelLText{\input{cv/doc3.tex}}
\ParallelRText{\input{dr/doc3.tex}}
\ParallelPar
\newpage
\ParallelLText{\input{cv/doc4.tex}}
\ParallelRText{\input{dr/doc4.tex}}
\ParallelPar
\newpage
\ParallelLText{\input{cv/doc5.tex}}
\ParallelRText{\input{dr/doc5.tex}}
\ParallelPar
\newpage
\end{Parallel}
\end{document}

• For future reference, please make your code self-contained by using lipsum, kantlipsum or blindtext for your dummy textual content. – Werner Jun 13 '14 at 21:45
• I don't know if its possible without further investigation but may be you can write every language in one independent document formated as one column and afterthat generate a merged document with pdfpages (or other merge utility). – Ignasi Jun 14 '14 at 8:41

Let me check if I understood the question. You want to have two separate files containing paragraphs separated by blank lines, as in standard latex, and you want those files to be paragraph-synced as if you wrote \ParallelLText{} or \ParallelRText{} around of them?

If so, the only solution which came to my mind is to use Luatex to read those files, split them at paragraphs, and insert the appropiate commands around.

This is my implementation:

## Lua code (parallelfiles.lua):

local function paragraphs(str)
local t = {}
local function helper(line) table.insert(t, line) return "" end
helper((str:gsub("(.-)\n\n+", helper)))
return t
end

function parallel_files(f1, f2)
local left_pars = paragraphs(left);
local right_pars = paragraphs(right);
if (#left_pars ~= #right_pars) then
tex.print("\\PackageWarning{parallelfiles}{Warning, the files do not have the same number of paragraphs}");
end
local i
local max_lines = #left_pars
if (#right_pars>max_lines) then
max_lines = #right_pars
end
for i=1,max_lines do
if i<#left_pars then
tex.print("\\ParallelLText{");
tex.print(left_pars[i]);
tex.print("}")
end;
if i<#right_pars then
tex.print("\\ParallelRText{");
tex.print(right_pars[i]);
tex.print("}");
end
tex.print("\\ParallelPar");
end
end


# Latex package (parallelfiles.sty)

This is only a wrap-around for the lua function:

\directlua{
dofile("parallelfiles.lua")
}

\newcommand{\parallelfiles}[2]{
\directlua{ parallel_files("#1", "#2") }
}


# Example of use:

\documentclass[10pt]{article}
\usepackage{yfonts,color,lettrine,pdfcolparallel}
\usepackage[top=.5in, bottom=1in, left=1in, right=1in]{geometry}
\usepackage{parallelfiles}

\begin{document}
\begin{Parallel}{.47\textwidth}{.47\textwidth}
\parallelfiles{lipsum.txt}{catsum.txt}
\end{Parallel}
\end{document}


And, assuming that lipsum.txt contains 12 paragraphs generated by lipsum web and catsum.txt contains 12 paragraphs generated by Kitty Ipsum web, this is the result:

• This is splendid. Would love to see it as a package on CTAN. – Andrew Dunning Jul 30 '16 at 11:29

This is the result of my first test. This is a poor-man solution and probably contains several pitfalls which I hope somebody else could help to solve.

As I explained in my comment it consits in building two different one column documents which will be later merged with pdfpages help.

The first document is:

%----184768b.tex-------%
\documentclass{article}
\pagestyle{empty}
\usepackage{kantlipsum}

\begin{document}
\thispagestyle{empty}
\kant
\end{document}


and the second

%------184768c.tex --------%
\documentclass{article}
\pagestyle{empty}
\usepackage{lipsum}

\begin{document}
\thispagestyle{empty}
\lipsum[1-30]
\end{document}


where I used half a4paper dimensions. The result looks like

The first document (kantlipsum) has 4 pages while the second one (lipsum) 8. Its possible to merge them inside a final document with

\documentclass[a4paper]{article}

\usepackage{pdfpages}
\usepackage{pgffor}

\begin{document}

\foreach \i in {1,2,...,4}{
\includepdfmerge[nup=2x1]{104768b, \i, 104768c, \i}}

\foreach \i in {5,6,...,8}{
\includepdf[nup=2x1, pages={{},\i}]{104768c}}

\end{document}


We need foreach (or any other loop utility) because

\includepdfmerge[nup=2x1]{104768b, {1-4}, 104768c, {1-4}}


would compound 4 pages from first file before 4 pages from the second one and

\foreach \i in {1,2,...,4}{
\includepdfmerge[nup=2x1]{104768b, \i, 104768c, \i}}


will place one from ecah one in every final page. While

\foreach \i in {5,6,...,8}{
\includepdf[nup=2x1, pages={{},\i}]{104768c}}


will place 4 last pages from second file on right columns while left columns are empty.

Some images from the final result: