# Interweave .tex files to create single PDF

I have three .tex files.

1. contains all the verses in a document in this format: \subsection*{वृद्धिरादैच् 1.1.1} \label{1.1.1}
2. contains explanation 1
3. contains explanation 2

Is there a way to merge these three documents to create a single PDF?

To create a single PDF file:

What sort of delimiters do I need to put in document 2 & 3 to show the explanations 1 & 2 from these documents under corresponding verses in document 1?

In document 1 some marker (i.e. rule or exp1 exp2) shall be shown at the start of each explanation.

It would be ideal if I could use two different font sizes.

-
Please provide more information on how the files need to be merged. Will they be merged sequentially (first verse.tex, then expl-.tex, and finally expl-2.tex), or do the explanations pertaining to a given verse somehow have to be inserted following the corresponding verse? –  Mico Feb 16 '12 at 13:41
Mico- I added an image to my question to show the desired output. The verse appears at the center as I have shown in the image. I have a working tex file upto that point. I need help to display the explanations under the verses as shown in the image. –  Aku Feb 16 '12 at 13:51
Thanks, @Aku. Is each verse is in a LaTeX environment, and are the two groups of explanations (two per verse) also in their own separate environments, say expli and explii? 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 the verses, expli, and explii 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:08
@Mico - No, the explanations are not in any environment as of now, So far they are in plain text format. I want to know how to put them in an environment. Yes each verse can start on a new page. Verse are already defined its own layout but there is none for expli and explii –  Aku Feb 16 '12 at 14:15
Please consider adding a MWE. Incidentally, do the verses and the two types of explanations have to reside in three separate files, or is that just a matter of convenience? Could they conceivably reside in 3N files separate tex files, named (say) verse-i.tex, explone-i.tex, and expltwo-i.tex, where N is the total number of verses and i is an index that runs from 1 to N? If you had 3N separate files, numbered sequentially in the form suggested above, constructing a for loop to iterate over all files would become quite simple to do. –  Mico Feb 16 '12 at 14:44

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}%
\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}

\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.

-
+1 for mentioning that this is the wrong way to do it :) –  Ryan Reich Jul 8 '12 at 0:35