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

parallel

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.

Note:

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:

#!/bin/bash


CHAPDIR="chapters"
SPLITDIR="splits"

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


# Generate document
for c in $CHAPDIR/fr/*.tex; do
   f=$(basename $c)
   chnum=${f%%_*}
   sfile="$SPLITDIR/${chnum}.tex"

   # 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)
        secnum=${secname##*_}
        [[ "x$secnum" = "x00" ]] && continue

        pen=${pfr/fr/en}

        # 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
        fi

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

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
\pagewiselinenumbers
\input{splits/01}
\input{splits/02}
\input{splits/03}
\input{splits/04}

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

Related Question