Interleaving of contents from three different .tex
files is not easy. If you had control over the contents and can generate it in a systematic way, could could use something like datatool
to "import the databases" and then parse them sequentially (interleaved) to obtain your output. Alternatively, you could store the contents of "Explanation 1" and "Explanation 2" in a separate macro and then write them out while you're reading in "Verse". It all depends on the amount of control you have in generating the three files.
Here is a small attempt at obtaining the output, using the datatool
package. However, heed the following warnings from the datatool
documentation:
Whilst TEX is an excellent typesetting language, it is not designed as a
database management system, and attempting to use it as such is like
trying to fasten a screw with a knife instead of a screwdriver: it can be done, but
requires great care and is more time consuming. [...] large databases and complex
operations will still slow the time taken to process your document. Therefore, if
you can, it is better to do the complex operations using whatever system created
the data in the first place.

\documentclass{article}
\usepackage{filecontents}% http://ctan.org/pkg/filecontents
\usepackage{datatool}% http://ctan.org/pkg/datatool
\begin{filecontents*}{verses.tex}
Name,Label
First,first
Second,second
Last,last
\end{filecontents*}
\begin{filecontents*}{expl1.tex}
Label,Explanation
second,This is Explanation~1 for the second verse
first,This is Explanation~1 for the first verse
last,This is Explanation~1 for the last verse
\end{filecontents*}
\begin{filecontents*}{expl2.tex}
Label,Explanation
last,This is Explanation~2 for the last verse
first,This is Explanation~2 for the first verse
second,This is Explanation~2 for the second verse
\end{filecontents*}
\newcounter{myverse}
\newenvironment{myverse}[1]
{% \begin{myverse}
\refstepcounter{myverse}%
\addvspace{2\baselineskip}\noindent
\hfill {\Large\bfseries Verse~--~\themyverse~--~#1} \hfill\null\par\nobreak
\noindent\hrulefill\par\nobreak
}{% \end{myverse}
}
\newenvironment{firstexpl}
{% \begin{firstexpl}
\par\nobreak{\large\bfseries Explanation~--~1:}\par\nobreak
}{% \end{firstexpl}
\par\hfill\ldots ends here.\par
}
\newenvironment{secondexpl}
{% \begin{secondexpl}
\par\nobreak{\large\bfseries Explanation~--~2:}\par\nobreak
}{% \end{secondexpl}
\par\hfill\ldots ends here.\par
}
\begin{document}
\DTLloaddb{verses}{verses.tex}% Load verses.tex
\DTLloaddb{expl1}{expl1.tex}% Load expl1.tex
\DTLloaddb{expl2}{expl2.tex}% Load expl2.tex
\DTLforeach*{verses}
{\CurrentVerse=Name,\CurrentLabel=Label}{%
\begin{myverse}{\CurrentVerse}\label{\CurrentLabel}
\DTLgetvalueforkey{\explanation}{Explanation}{expl1}{Label}{\CurrentLabel}%
\begin{firstexpl}\explanation\end{firstexpl}
\DTLgetvalueforkey{\explanation}{Explanation}{expl2}{Label}{\CurrentLabel}%
\begin{secondexpl}\explanation\end{secondexpl}
\end{myverse}
}
\end{document}
Within the myverse
environment, datatool
searches for the appropriate explanation associated with that specific verse Name
. This allows you to have a different order of each explanation (both expl1.tex
and expl2.tex
). However, if you maintain the same order as verses.tex
, you can speed up your compilation and don't need to search for the correct key/entry.
For the sake of completeness, I've added a Label
for each verse that you can use directly as \label{\CurrentLabel}
to label the verse.
One limitation of using datatool
directly, is that it only works with ASCII text files. However, if you need other language support (as suggested by your post), you can store the contents of every verse in a file that you can \input
based on the conditions in (say) verses.tex
.
The filecontents
package is loaded to make the example above portable, and may therefore not be needed in your application.
Other options also exist, but would require (most likely) external applications to interleave the "databases" into a more manageable document.
expli
andexplii
? I believe we still need to get more information from you in order to be able to provide suggestions. E.g., does each verse have to start on a new page, or can it start anywhere on a page? Do theverses
,expli
, andexplii
environments define their own layouts -- and what you need is a method for interleaving the environments -- or do you still need to define the layouts? Please provide an MWE. – Mico Feb 16 '12 at 14:08verse
can start on a new page. Verse are already defined its own layout but there is none forexpli
andexplii
– Aku Feb 16 '12 at 14:15N
files separate tex files, named (say) verse-i.tex, explone-i.tex, and expltwo-i.tex, whereN
is the total number of verses andi
is an index that runs from 1 toN
? If you had 3N
separate files, numbered sequentially in the form suggested above, constructing afor
loop to iterate over all files would become quite simple to do. – Mico Feb 16 '12 at 14:44