[Tex/LaTex] Another body text in the margin

margins

I saw a book that besides its main content had the text from another book squeezed in the bottom margin. I'm wonder whether this is possible to do with LaTeX? More specifically I'm wondering whether it's possible to have two separate text and produce output such that one text goes in the main content area of the page and the other in the margin? Note that both texts may not fit on one page but needs to run over several pages.

Best Answer

A very rough approach, collecting the material for the other book in a vertical box, and using \vsplit to split the top part of that box and put it on the left of the main text. Admittedly, the result is not good, but it might be possible to polish it.

\documentclass{article}
\textwidth=200pt\relax %??
\usepackage{xgalley,xparse}
\usepackage{lipsum}

\ExplSyntaxOn\makeatletter
\box_new:N \l_otext_tmpa_box
\box_new:N \g_otext_box
\dim_new:N \g_otext_prevdepth_dim
\skip_new:N \l_otext_sep_skip
\skip_set:Nn \l_otext_sep_skip { 20 pt }
\dim_new:N \g_otext_side_width_dim
\dim_gset:Nn \g_otext_side_width_dim { 100 pt }
\NewDocumentEnvironment{otext}{}
  {
    \vbox_gset:Nw \g_otext_box
      \color_group_begin:
        \vbox_unpack_clear:N \g_otext_box
        \dim_gset_eq:NN \tex_prevdepth:D \g_otext_prevdepth_dim
        \dim_set_eq:NN \l_galley_width_dim \g_otext_side_width_dim
        \galley_level:
        \bool_gset_false:N \g_galley_omit_next_indent_bool
        \raggedleft
  }
  {
      \color_group_end:
      \dim_gset_eq:NN \g_otext_prevdepth_dim \tex_prevdepth:D
    \vbox_set_end:
  }
% Patch \output.
\tl_set:Nx \l_tmpa_tl { \the\output }
\tl_put_left:Nn \l_tmpa_tl
  {
    \vbox_set_split_to_ht:NNn \l_otext_tmpa_box \g_otext_box
      { \box_ht:N \@cclv + \box_dp:N \@cclv }
    \vbox_set:Nn \@cclv
      {
        \hbox:n
          {
            \box_use_drop:N \l_otext_tmpa_box
            \skip_horizontal:N \l_otext_sep_skip
            \box_use_drop:N \@cclv
          }
      }
  }
\exp_args:No \output \l_tmpa_tl
\makeatother\ExplSyntaxOff

\begin{document}
\raggedright

\begin{otext}
  \section{Paragraphs 1 and 2}
  \lipsum[1-2]
\end{otext}

\begin{otext}
  \section{Paragraphs 3 and 4}
  \lipsum[3-4]
\end{otext}

\section{Paragraphs 11 to 13}
\lipsum[11-13]

\begin{otext}
  \section{Paragraphs 5 to 7}
  \lipsum[5-7]
\end{otext}

\section{Paragraphs 14 to 27}
\lipsum[14-27]

\end{document}
Related Question