[Tex/LaTex] Sentence alignment for dual-line bilingual paragraph translation in LaTeX

languagesparagraphspositioning

I would like to make a translation of entire paragraphs at a time for a large document, with the start of the new sentences aligning. The following is almost what I want done, though it doesn't yet do the alignment for new sentences.

\documentclass{article}
\usepackage[margin=1in, paperwidth=8.5in, paperheight=11in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{url}
\usepackage{setspace}
\begin{document}

\def\english{In the heart of the forest lived three little pigs who were brothers. The wolf always was chasing them in order to eat them. In order to escape the wolf, the pigs decided to make a house each. The smallest made his from straw, to finish first and go out to play. The middle one constructed a cottage from wood. Seeing that his little brother had finished already, he hurried to go and play with him. The oldest worked on his house of brick. 'You'll soon see what the wolf does with your houses,' he scolded his brothers but they were having a great time.}

\def\spanish{En el corazón del bosque vivían tres cerditos que eran hermanos. El lobo siempre andaba persiguiéndoles para comérselos. Para escapar del lobo, los cerditos decidieron hacerse una casa. El pequeño la hizo de paja, para acabar antes y poder irse a jugar. El mediano construyó una casita de madera. Al ver que su hermano perqueño había terminado ya, se dio prisa para irse a jugar con él. El mayor trabajaba en su casa de ladrillo. - Ya veréis lo que hace el lobo con vuestras casas - riñó a sus hermanos mientras éstos se lo pasaban en grande.}

\begin{minipage}[t][0pt]{\linewidth}
    \setstretch{3}
    \english
\end{minipage}

\begin{minipage}[t]{\linewidth}
    \setstretch{3}
    \spanish
\end{minipage}

\end{document}

Sample of dual line translation

How can I automatically align new sentences? For bonus points, how can I modify this method to manually indicate when one sentence in english aligns to two in spanish? I would like the paragraph to flow naturally, I don't want to add a line break after each sentence or use a package that essentially does that.

Best Answer

This is a huge hack (and I can't wholeheartedly recommend using it as-is), but it is the only way I can think of doing it without mucking with TeX's linebreaking engine. You will need to latex this twice for it to work. This also assumes that the Spanish translations are not too much longer than the English.

\documentclass{article}
\usepackage[margin=1in, paperwidth=8.5in, paperheight=11in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{url}
\usepackage{setspace}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\newcount\numsentences\numsentences=0
\def\sentence#1#2{
  \expandafter\xdef\csname sentenceeng\the\numsentences\endcsname{#1}
  \expandafter\xdef\csname sentenceesp\the\numsentences\endcsname{#2}
  \advance\numsentences by 1
}

\sentence{In the heart of the forest lived three little pigs who were brothers.}
         {En el corazón del bosque vivían tres cerditos que eran hermanos.}
\sentence{The wolf always was chasing them in order to eat them.}
         {El lobo siempre andaba persiguiéndoles para comérselos.}
\sentence{In order to escape the wolf, the pigs decided to make a house each.}
         {Para escapar del lobo, los cerditos decidieron hacerse una casa.}
\sentence{The smallest made his from straw, to finish first and go out to play.}
         {El pequeño la hizo de paja, para acabar antes y poder irse a jugar.}
\sentence{The middle one constructed a cottage from wood.}
         {El mediano construyó una casita de madera.}
\sentence{Seeing that his little brother had finished already, he hurried to go and play with him.}
         {Al ver que su hermano perqueño había terminado ya, se dio prisa para irse a jugar con él.}
\sentence{The oldest worked on his house of brick.}
         {El mayor trabajaba en su casa de ladrillo.}
\sentence{``You'll soon see what the wolf does with your houses,'' he scolded his brothers but they were having a great time.}
         {- Ya veréis lo que hace el lobo con vuestras casas - riñó a sus hermanos mientras éstos se lo pasaban en grande.}

\begin{minipage}[t][0pt]{\linewidth}
  \setstretch{3}
  \count0=-1
  \loop
    \advance\count0 by 1
    \ifnum\count0<\numsentences
      \tikz[remember picture]\coordinate(s\the\count0 begin);\csname sentenceeng\the\count0\endcsname\tikz[remember picture]\coordinate(s\the\count0 end);\hskip0.5in
  \repeat
\end{minipage}

\begin{tikzpicture}[remember picture,overlay]
  \count0=-1
  \loop
    \advance\count0 by 1
    \ifnum\count0<\numsentences
      \path let \p1=(s\the\count0 begin), \p2=(s\the\count0 end) in (s\the\count0 begin) node {\xdef\xpos{\x1}\xdef\ypos{\y1}\xdef\xxpos{\x2}\xdef\yypos{\y2}};
      \ifdim\ypos=\yypos
        \node[below=12pt,left=1mm,anchor=west] at (s\the\count0 begin) {\dimen0=\xxpos\advance\dimen0 by -\xpos\hbox to \dimen0{\csname sentenceesp\the\count0\endcsname\hfill}};
      \else
        \path (current page.west) -- +(1.2in+\oddsidemargin,0) node[coordinate] (leftanchor) {};
        \path let \p1=(leftanchor) in (s\the\count0 begin) node {\xdef\leftxpos{\x1}};
        \path (leftanchor) |- node[below=12pt,left=1mm,anchor=west] {\begin{minipage}[t][0pt]{\linewidth}\setstretch{3}\dimen0=\xpos\advance\dimen0 by -\leftxpos\hspace*{\dimen0}\csname sentenceesp\the\count0\endcsname\end{minipage}} (s\the\count0 begin);
      \fi
  \repeat
\end{tikzpicture}

\end{document}

This is how the huge hack looks!

Related Question