Customised section overlapping in margin

The following does the trick (I've fixed a problem since the first submission). The solution requires two runs. In the first it'll save the label numbers and in the second it will collect them and turn them into text in the margin.

In the solution, which requires the recent pdftex extensions \pdfsavepos and pdflastypos, I use the current page number and the y position to group the numbers that should serve as labels. Since the numbers are all contiguous I decided to typeset the numbers as <start>--<end> in case there was a range of numbers. If you really want a comma-separated list then this should be easy to fix.

The implementation requires two temporary files because one had to store counters, which required an immediate \write and the other file had to store the \pdflastypos values, which are only known at page shipout time. It took a while before I got the implications:-)

The following is the package. The input is based on McGafter's input. I didn't have Caslon and I had to redefine the page headings (I wasn't going to bother defining them).

\NeedsTeXFormat{LaTeX2e}       
\ProvidesPackage{mvd-verse}[2012/01/23 Marc van Dongen's verse numbering style]

\makeatletter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \msec@chapt is the number of the current chapter              
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcount\msec@chapt                                            
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \msec@verse is the number of the current verse                
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcount\msec@verse                                            
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \msec@calls is the number of calls to \marginsec              
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcount\msec@calls                                            
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \msec@reads is the number of lines read from input files      
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcount\msec@reads                                            
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Option parsing                                                
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\RequirePackage{pgfopts}                                        
\pgfkeys{/mvdsec@package/initial/.style={/mvdsec/.cd,chapter=#1},
         /mvdsec/.cd,                                            
         set verse/.code={\global\msec@verse=#1\relax},          
         set chapter/.code={\global\msec@chapt=#1\relax},        
         reset verse/.style={set verse=0},                       
         chapter/.style={set chapter=#1,reset verse},            
         step chapter/.code={\global\advance\msec@chapt by 1\relax},
         next chapter/.style={step chapter,reset verse},            
         /mvdsec@package/initial=1}                                 
\ProcessPgfPackageOptions{/mvdsec@package}                          
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Saving the y positions.                                       
% This code is partially based on V{\'\i}t Z{\'y}ka's article in
% TUGboat, Volume 30 (2009), No. 1, pp.~123--124.               
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\AtBeginDocument{%                                              
    \makeatletter                                               
    \newwrite\msec@numbers@output%                              
    \newwrite\msec@ypos@output%                                 
    \immediate\openout\msec@numbers@output \msec@nmbr@file%     
    \openout\msec@ypos@output \msec@ypos@file%                  
    \makeatother                                                
}                                                               
\AtEndDocument{%                                                
    \immediate\closeout\msec@numbers@output%                    
    \closeout\msec@ypos@output%                                 
}                                                               

% Write the position to file.
% #1 is the page number      
% #2 is the chapter number   
% #3 is the verse number     
\def\msec@write@lines#1#2#3{%
    \pdfsavepos%             
    \immediate%              
    \write\msec@numbers@output{%
        \string\msec@def@numbers%
        \string{#1\string}%      
        \string{#2\string}%      
        \string{#3\string}%      
    }%                           
    \write\msec@ypos@output{%    
        \string\msec@def@ypos\string{\the\pdflastypos\string}%
    }%                                                        
}                                                             

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Read in the input files.                                      
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\msec@ypos@file{\jobname.mpy}
\def\msec@nmbr@file{\jobname.mpn}

\def\msec@read@input@files{%
    % keep changes and definitions local
    \begingroup                         
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        \def\msec@text{-}                                       
        \def\msec@first{-}                                      
        \def\msec@chapter{-}                                    
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % Evaluate lines read from input files.                 
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        \def\msec@evaluate@lines{%                              
            \msec@tmp@a%                                        
            \msec@tmp@b%                                        
        }                                                       
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % Define next page, chapter, and verse.                 
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        \def\msec@def@numbers##1##2##3{%                        
            \def\msec@page{##1}%                                
            \def\msec@chap{##2}%                                
            \def\msec@vrse{##3}%                                
        }                                                       
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % Define next y-position.                               
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        \def\msec@def@ypos##1{%                                 
            \def\msec@ypos{##1}%                                
            \edef\msec@next@py{\msec@page-##1}%                 
        }                                                       
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % Process next two input lines.                         
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        \def\msec@read@lines{%                                  
            \msec@evaluate@lines%                               
            \advance\msec@reads by 1\relax%                     
            \ifx\msec@next@py\msec@curr@py%                     
                \edef\msec@text{\msec@first--\msec@vrse}%       
            \else%                                              
                \edef\msec@READS{\the\msec@reads}               
                \ifx\msec@chap\msec@chapter%                    
                    \edef\msec@text{\msec@vrse}%                
                    \edef\msec@first{\msec@vrse}                
                \else%                                          
                    \edef\msec@chapter{\msec@chap}%             
                    \edef\msec@text{[\msec@chap}%               
                    \edef\msec@first{[\msec@chap}               
                \fi%                                            
                \edef\msec@curr@py{\msec@next@py}%              
            \fi%                                                
            \expandafter%                                       
            \xdef\csname msec@label@\msec@READS\endcsname{%     
                \msec@text%                                     
            }%                                                  
        }                                                       
        % end of local macro definitions                        
        \def\msec@curr@py{0-0}% page-ypos                       
        \msec@verse=0\relax%                                    
        \newread\msec@numbers@input                             
        \newread\msec@ypos@input                                
        \openin\msec@numbers@input \msec@nmbr@file%             
        \openin\msec@ypos@input    \msec@ypos@file%             
        \loop%                                                  
            \read\msec@numbers@input to \msec@tmp@a%            
            \read\msec@ypos@input    to \msec@tmp@b%            
            \unless\ifeof\msec@numbers@input                    
            \msec@read@lines                                    
        \repeat                                                 
        \closein\msec@numbers@input%                            
        \closein\msec@ypos@input%                               
        \global\msec@reads=\msec@reads\relax%                   
    \endgroup                                                   
}                                                               

\IfFileExists{\msec@ypos@file}
             {\msec@read@input@files}
             {}                      

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\msec@set@box{%
    \@ifnextchar[% 
            \msec@set@chapter@box%
            \msec@set@section@box%
}
\def\msec@set@chapter@box[#1\msec@set@box{%
    \msec@set@chapter@box@#1--\msec@set@box%
}
\def\msec@set@chapter@box@#1--#2\msec@set@box{%
    \textbf{#1}%
}
\def\msec@set@section@box#1\msec@set@box{%
    #1%
}

\newcommand{\marginsec}[2][]{%
  \strut\nobreak#2%
  \pgfkeys{/mvdsec/.cd,#1}%
  \advance\msec@calls by 1\relax%
  \advance\msec@verse by 1\relax%
  \ifnum\msec@reads<\msec@calls%
      \def\msec@tmp{}%
  \else%
      \edef\msec@tmp{\csname msec@label@\the\msec@calls\endcsname}%
  \fi%
  \msec@write@lines{\thepage}{\the\msec@chapt}{\the\msec@verse}%
  \vadjust{\vbox to 0pt{%
    \sbox0{\expandafter\msec@set@box\msec@tmp\msec@set@box\quad}
    \kern-\ht0
    \kern-\dp\strutbox
    \llap{\box0 }
    \vfill
    }
  }%
}

\newcommand{\getChapter}{\the\msec@chapt}
\newcommand{\getVerse}{\the\msec@verse}

\makeatother

The following is the sample input. It's based on McGafter's input.

\documentclass[pagesize=pdftex, 9pt]{scrbook}
\usepackage[pagestyles]{titlesec}            
\usepackage{geometry}                        
\usepackage{xltxtra}                         

\usepackage{graphicx}
\usepackage{wrapfig} 

% \usepackage[initial=4]{mvd-verse} starts with chapter 4.
\usepackage{mvd-verse}                                    

\geometry{
paperwidth=15.3cm,
paperheight=23.2cm,
top=1.5cm,         
bottom=.8cm,       
left=1.3cm,        
right=1.3cm        
}                  

% sets font to use
% package = xltxtra
\defaultfontfeatures{Mapping=tex-text}
% Don't have Cardo                    
%\setmainfont{Cardo}                  

\newpagestyle{Biblestyle}{
  \setheadrule{.02pt}     
  \sethead[\thepage][\chaptertitle]%
[you have to fix this]%             
{THIS TOO}%                         
{\chaptertitle}{\thepage}           
}                                   

% Paragraph formatting
\setlength{\parindent}{0pt}

% sets the marks to be used (section and subsection)
\setmarks{section}{subsection}                      

\newlength\howwide
\newcommand\defhowwide{
    \ifnumless{\value{section}}{99}{\ifnumgreater{\value{section}}{8}{\setlength{\howwide}{2.5em}}{\setlength{\howwide}{1.2em}}}{\ifnumgreater{\value{section}}{98}{\setlength{\howwide}{3.5em}}{\setlength{\howwide}{2.5em}}}                                                     
}                                                                                        

% the ratio of the embedded PDF to the text on the page
\newlength\ratiowidth                                  
\setlength\ratiowidth{.62\textwidth}                   

\usepackage{hyperref}
\hypersetup{         
    bookmarksnumbered=true,
    bookmarksopen=true,    
    bookmarksopenlevel=0,  
    bookmarksdepth=1,      
    unicode=true,          
    colorlinks=true,       
    citecolor={black},     
    linkcolor={black},     
    urlcolor={black},      
    pdffitwindow=true,     
    pdftitle={The Holy Bible}
}                            

\begin{document}

\mainmatter
\pagestyle{Biblestyle}

\begin{wrapfigure}{r}{\ratiowidth}
  \begin{center}                  
\includegraphics[width=\ratiowidth, page=1, trim=70 50 85 30, clip]{torahCropped.pdf}
\end{center}                                                                         
\end{wrapfigure}                                                                     

\phantomsection
\addcontentsline{toc}{part}{Law}\noindent
\phantomsection                          
\addcontentsline{toc}{chapter}{Genesis}\noindent
\small{THE FIRST BOOK OF MOSES, CALLED\\        
\textbf{\LARGE{GENESIS}}\\                      
}                                               

\normalsize
\marginsec{In} the beginning God created the heaven and the earth.
\marginsec{And} the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters.                            
\marginsec{And} God said, Let there be light: and there was light.                       
\marginsec{And} God saw the light, that it was good: and God divided the light from the darkness.                                                                                     
\marginsec{And} God called the light Day, and the darkness he called Night. And the evening and the morning were the first day.                                                       
\marginsec{And} God said, Let there be a firmament in the midst of the waters, and let it divide the waters from the waters.                                                          
\marginsec{And} God made the firmament, and divided the waters which were under the firmament from the waters which were above the firmament: and it was so.                          
\marginsec{And} God called the firmament Heaven. And the evening and the morning were the second day.                                                                                 
\marginsec{And} God said, Let the waters under the heaven be gathered together unto one place, and let the dry land appear: and it was so.                                            
\marginsec{And} God called the dry land Earth; and the gathering together of the waters called he Seas: and God saw that it was good.                                                 
\marginsec{And} God said, Let the earth bring forth grass, the herb yielding seed, and the fruit tree yielding fruit after his kind, whose seed is in itself, upon the earth: and it was so.                                                                                       
\marginsec{And} the earth brought forth grass, and herb yielding seed after his kind, and the tree yielding fruit, whose seed was in itself, after his kind: and God saw that it was good.                                                                                         
\marginsec{And} the evening and the morning were the third day.                          
\clearpage                                                                               
\begin{wrapfigure}{r}{\ratiowidth}                                                       
  \begin{center}                                                                         
\includegraphics[width=\ratiowidth, page=2, trim=70 50 85 30, clip]{torahCropped.pdf}    
\end{center}                                                                             
\end{wrapfigure}                                                                         
\marginsec{And} God said, Let there be lights in the firmament of the heaven to divide the day from the night; and let them be for signs, and for seasons, and for days, and years:   
\marginsec{And} let them be for lights in the firmament of the heaven to give light upon the earth: and it was so.                                                                    
\marginsec{And} God made two great lights; the greater light to rule the day, and the lesser light to rule the night: he made the stars also.                                         
\marginsec{And} God set them in the firmament of the heaven to give light upon the earth,
\marginsec{And} to rule over the day and over the night, and to divide the light from the darkness: and God saw that it was good.                                                     
\marginsec{And} the evening and the morning were the fourth day.                         
\marginsec{And} God said, Let the waters bring forth abundantly the moving creature that hath life, and fowl that may fly above the earth in the open firmament of heaven.            
\marginsec{And} God created great whales, and every living creature that moveth, which the waters brought forth abundantly, after their kind, and every winged fowl after his kind: and God saw that it was good.                                                                  
\marginsec{And} God blessed them, saying, Be fruitful, and multiply, and fill the waters in the seas, and let fowl multiply in the earth.                                             
\marginsec{And} the evening and the morning were the fifth day.                          
\marginsec{And} God said, Let the earth bring forth the living creature after his kind, cattle, and creeping thing, and beast of the earth after his kind: and it was so.             
\marginsec{And} God made the beast of the earth after his kind, and cattle after their kind, and every thing that creepeth upon the earth after his kind: and God saw that it was good.                                                                                            
\marginsec{And} God said, Let us make man in our image, after our likeness: and let them have dominion over the fish of the sea, and over the fowl of the air, and over the cattle, and over all the earth, and over every creeping thing that creepeth upon the earth.            
\marginsec{So} God created man in his own image, in the image of God created he him; male and female created he them.                                                                 
\marginsec{And} God blessed them, and God said unto them, Be fruitful, and multiply, and replenish the earth, and subdue it: and have dominion over the fish of the sea, and over the fowl of the air, and over every living thing                                                 
\clearpage                                                                               
\begin{wrapfigure}{r}{\ratiowidth}                                                       
  \begin{center}                                                                         
\includegraphics[width=\ratiowidth, page=3, trim=70 50 85 30, clip]{torahCropped.pdf}    
\end{center}                                                                             
\end{wrapfigure}                                                                         
that moveth upon the earth.                                                              
\marginsec{And} God said, Behold, I have given you every herb bearing seed, which is upon the face of all the earth, and every tree, in the which is the fruit of a tree yielding seed; to you it shall be for meat.                                                               
\marginsec{And} to every beast of the earth, and to every fowl of the air, and to every thing that creepeth upon the earth, wherein there is life, I have given every green herb for meat: and it was so.                                                                          
\marginsec{And} God saw every thing that he had made, and, behold, it was very good. And the evening and the morning were the sixth day.                                              

\marginsec[next chapter]{Thus} the heavens and the earth were finished, and all the host of them.
\marginsec{And} on the seventh day God ended his work which he had made; and he rested on the seventh day from all his work which he had made.
\marginsec{And} God blessed the seventh day, and sanctified it: because that in it he had rested from all his work which God created and made.
\marginsec{These} are the generations of the heavens and of the earth when they were created, in the day that the LORD God made the earth and the heavens,
\marginsec{And} every plant of the field before it was in the earth, and every herb of the field before it grew: for the LORD God had not caused it to rain upon the earth, and there was not a man to till the ground.
\marginsec{But} there went up a mist from the earth, and watered the whole face of the ground.
\marginsec{And} the LORD God formed man of the dust of the ground, and breathed into his nostrils the breath of life; and man became a living soul.
\marginsec{And} the LORD God planted a garden eastward in Eden; and there he put the man whom he had formed.
\marginsec{And} a short TEST VERSE.
\marginsec{And} out of the ground made the LORD God to grow every tree that is pleasant to the sight, and good for food; the tree of life also in the midst of the garden, and the tree of knowledge of good and evil.
\marginsec{And} a river went out of Eden to water the garden; and from thence it was parted, and became into four heads.
\marginsec{The} name of the first is Pison: that is it which compasseth the whole land of Havilah, where

\marginsec[chapter=22]{And} we're almost there.

\marginsec[next chapter]{I} promise; I really do.
\marginsec{For} we cannot go on forever.
\marginsec{We} have to stop somewhere or we'll never stop.
\marginsec{Bye} now.

Finally, the current chapter and verse are \getChapter\ and \getVerse.

\end{document}

Output


I am not sure, if I am interpreting the question properly, but here is a MWE. If you willing to enclose the verse in environments, the macros can be modified to offer more complicated ways of typesetting.

\documentclass{article}
\usepackage{xspace,lineno}
\newcounter{ctr}
\setcounter{ctr}{0}
\makeatletter
\def\inc{\refstepcounter{ctr}\thectr}
\def\collect{}
\def\typeset{\marginpar{\collect}\let\collect\@empty\space}
\def\verse{%
\ifx\collect\@empty
  \g@addto@macro{\collect}{\inc}\footnotemark
\else
 \g@addto@macro{\collect}{,\inc }\footnotemark
\fi
}
\let\endverses\typeset
\begin{document}
When you decided to produce your publication you have accepted the will to learn enough of a system to do your own work, but this now involves more than the art of the typesetter. You need to invest time to learn the programming part of packages and LaTeX and programming needs artful and other skills.\verse 
\endverses

There are no-shortcuts and instant solutions\verse. No magic wands. You need to invest time in effortful study. \verse There are no-shortcuts and instant solutions. No magic wands.\verse  You need to invest time in effortful study.\verse
\endverses
\end{document}

The output

enter image description here

If one can enclose the paragraphs with a set of verses in environment, line numbers can be positioned at the actual lines, as this will enable vsplit to be implemented.