[Tex/LaTex] Putting two texts side-by-side


I am currently translating a book and typesetting the translation.

For reviewing purposes, I would find it very useful to display the original and translated texts side-by-side, without having to duplicate the text I have for the typesetted translation. My translation is stored in a chapters/ directory, one file per chapter, beginning with a \chapter command, and containing several \section* commands each.

My idea would be to have a chapters_orig/ directory with the chapters in the original language, and then automatically generate a document that contains the two languages side-by-side, paragraph by paragraph.
I have read about the parallel package but I don't see how I could use it to automatically generate this document without altering my current document.


Although I've answered my question with a bash script, I'm still interested in a TeX-based solution, and I would be happy to make it a package.

Best Answer

I think parallel is the best way to go, but doing it the way I want to is a bit tricky in pure TeX. After much thinking, I ended up writing a bash script to do it. It's not the most elegant solution (you can call it ugly if that makes you feel better), but it does the trick.

Here is my script:



# Split all files
for l in fr en; do
   mkdir -p $SPLITDIR/$l
   for c in $CHAPDIR/$l/*.tex; do
      f=$(basename $c)
      csplit --quiet --prefix $SPLITDIR/$l/${num}_                         --suffix-format=%02d.tex                          --elide-empty-files                  $c /^$/+1 {*}

# Generate document
for c in $CHAPDIR/fr/*.tex; do
   f=$(basename $c)

   # Make chapter title
   #echo "\input{$SPLITDIR/en/${chnum}_00}" > $sfile
   #sed -i 's@\\chapter@\\chapstyle\\chapheadstyle@' $SPLITDIR/fr/${chnum}_00.tex
   #echo "\input{$SPLITDIR/fr/${chnum}_00}" >> $sfile
   enchap=$(sed -e 's@\\chapter{\(.*\)}@\1@' $SPLITDIR/en/${chnum}_00.tex)
   frchap=$(sed -e 's@\\chapter{\(.*\)}@\1@' $SPLITDIR/fr/${chnum}_00.tex)
   echo "\\chapter{$enchap \\\\$frchap}" > $sfile

   echo '\normalfont' >> $sfile

   echo '\begin{Parallel}{2in}{2in}' >> $sfile
   for pfr in $SPLITDIR/fr/${chnum}_*.tex; do
        secname=$(basename $pfr .tex)
        [[ "x$secnum" = "x00" ]] && continue


        # Replace chapter with \Huge, section with \Large
        sed -i 's@\\section\*@\\secstyle@' $pfr

        # Remove lettrines
        sed -i 's@\\chlettrine{\(.*\)}{\(.*\)}@\1\2@' $pfr

        if [ ! -f "$pen" ]; then
           echo "E: Missing file $pen"
           exit 1

        echo '\ParallelPar' >> $sfile
        echo "\ParallelLText{\selectlanguage{english}\input{$pen}}" >> $sfile
        echo "\ParallelRText{\selectlanguage{french}\input{$pfr}}" >> $sfile
   echo '\end{Parallel}' >> $sfile

I've moved my translated text to chapters/fr/ and added original chapters in chapters/en/. The script creates a splits/fr/ and splits/en/ directories (for the note, splits/ has got an 's' because I've got a rule called "split" in my Makefile), splits the chapters in chapters/* by paragraph (using csplit) and reassembles the chapters in splits/, using the parallel package.

The master document then contains the call to load the parallel package, as well as the \input calls:

% All chapters

Running ./split.sh followed by pdflatex masterdoc.tex generates the document with split chapters.

And here is how it renders:

split view with line numbers