How can I fix the following overlap happening in the margin? I want the numbers to follow each other with a comma in between them.

Edit: Clarification of the question. The text needs to be flowing from one line to the next with the section numbering in the margin. However when there are two or more numbers in the same line on the margin they should be separated by a comma or placed one (slightly) above the other.


  \mbox{\strut#1}\vadjust{\vbox to 0pt{
    \llap{\box0 }

\marginsec{Babel} and hyphenation patterns for english, dumylang, nohyphenation,
\marginsec{CATALAN}, pinyin, coptic, croatian, czech, danish, dutch, ukenglish,
usenglishmax, esperanto, estonian

MWE output

Edit 4: Cleaner explanation:

This is the way the verses work in the Bible. Since the copies of manuscripts we have from the Bible has all the text written basically with all the letters back-to-back in the original languages the chapter and verse method has been applied to make it more readable and easier to reference a certain point in the text.

You have a 66 books, broken into multiple chapters each. Every chapter is then broken into many verses.

The way I need my verses numbered is by using the chapter number as the number for the very first verse. See the two screenshots below of Genesis chapter 1 and chapter 2. The numbers of the verses then increments onward until you get to the next chapter.


Turning to page 3 where the next chapter starts it should look like this:


BUT based on Marc's excellent solution below the verse numbers are all still being displayed as well as the chapter number, resulting in the following:


And also:


Here is an example to reproduce where I'm at at the moment. However you might not have the embedded image file so just remove that code in the wrapfigure tags.

Edit 5: Here's my file. Demo File

  • What do the numbers represent? It looks to me is something akin to verses? Jan 20, 2012 at 10:59
  • If you put #1 in a \mbox the word won't be hyphenated. With such narrow columns, you should increase the tolerance or set \emergencystretch.
    – egreg
    Jan 20, 2012 at 11:01
  • @YiannisLazarides The numbers are verses in the Bible indeed.
    – McGafter
    Jan 20, 2012 at 12:28
  • @McGafter I posted an answer, if you add some clarifications, I will try and provide a full solution. Jan 20, 2012 at 13:23
  • 1
    Regardless of whether you accept my solution, you may want to consider using pattern matching for the \marginsec command because its argument is always followed by a space: \def\marginsec#1 {<put body of current \marginsec here>}.
    – user10274
    Jan 23, 2012 at 4:33

2 Answers 2


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).

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


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

% Option parsing                                                
         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},            

% 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.               
    \immediate\openout\msec@numbers@output \msec@nmbr@file%     
    \openout\msec@ypos@output \msec@ypos@file%                  

% Write the position to file.
% #1 is the page number      
% #2 is the chapter number   
% #3 is the verse number     

% Read in the input files.                                      


    % keep changes and definitions local
        % Evaluate lines read from input files.                 
        % Define next page, chapter, and verse.                 
        % Define next y-position.                               
        % Process next two input lines.                         
            \advance\msec@reads by 1\relax%                     
            \xdef\csname msec@label@\msec@READS\endcsname{%     
        % end of local macro definitions                        
        \def\msec@curr@py{0-0}% page-ypos                       
        \openin\msec@numbers@input \msec@nmbr@file%             
        \openin\msec@ypos@input    \msec@ypos@file%             
            \read\msec@numbers@input to \msec@tmp@a%            
            \read\msec@ypos@input    to \msec@tmp@b%            




  \advance\msec@calls by 1\relax%
  \advance\msec@verse by 1\relax%
      \edef\msec@tmp{\csname msec@label@\the\msec@calls\endcsname}%
  \vadjust{\vbox to 0pt{%
    \llap{\box0 }



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

\documentclass[pagesize=pdftex, 9pt]{scrbook}


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


% sets font to use
% package = xltxtra
% Don't have Cardo                    

[you have to fix this]%             
{THIS TOO}%                         

% Paragraph formatting

% sets the marks to be used (section and subsection)


% the ratio of the embedded PDF to the text on the page

    pdftitle={The Holy Bible}



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


\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.                          
\includegraphics[width=\ratiowidth, page=2, trim=70 50 85 30, clip]{torahCropped.pdf}    
\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                                                 
\includegraphics[width=\ratiowidth, page=3, trim=70 50 85 30, clip]{torahCropped.pdf}    
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.



  • This looks like the solution. I can't quite figure out which counter to reset to 1 for resetting my verses after a chapter so that they can start from 1 again.
    – McGafter
    Jan 23, 2012 at 9:23
  • 1
    @McGafter You didn't ask for a reset feature. This may break the solution if the top-level units are sections because there may be several sections on a page. However, for chapters it should work. I'll add a reset feature, which you have to call it at the start of each chapter. I'll put in another note when the feature has been added.
    – user10274
    Jan 23, 2012 at 9:37
  • 1
    @MarcvanDongen Shouldn't it be \ProvidesPackage{mvdverse} instead of \ProvidesClass?
    – ienissei
    Jan 23, 2012 at 14:27
  • 1
    @McGafter It doesn't look much more difficult, but it's still not clear. For example, what should happen if there's a new chapter and two or more \marginsec commands for the first output line? How should that be typeset? For example, should we get \textbf{<chapter number>} \textrm{<number>} and \textbf{<chapter number> \textrm<first number>--\textrm{last number>}? Can you make a link available to torahCropped.pdf?
    – user10274
    Jan 23, 2012 at 15:13
  • 1
    @McGafter Got it. It will probably take until tomorrow morning (Irish time) because I suspect you would like to reset the chapter counter values to specific numbers. BTW: I looked at the output of your input file and it doesn't look nice near the starts of the sections. There's too much space. I suspect this is because you don't pass the first word as an argument to \marginsec. Passing the first word to \marginsec is the intended API.
    – user10274
    Jan 23, 2012 at 15:44

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.

 \g@addto@macro{\collect}{,\inc }\footnotemark
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 

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

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.

  • 1
    I dont't think the original poster was asking for a solution like this. The labels should be collected on the same lines that starts them. Not at the end of the paragraphs they're in.
    – user10274
    Jan 22, 2012 at 17:50
  • @Yiannis Lazarides: Marc van Dongen is right. However I'll upvote you because your solution can be helpful in my future projects and comes close to what I want. Have a look at Marc's answer below, he's got it figured out, it seems.
    – McGafter
    Jan 23, 2012 at 10:49
  • @McGafter I see if I can modify as an alternate solution, is not all that difficult to split line by line, just a bit busy over the next day or two. Jan 23, 2012 at 11:11
  • @YiannisLazarides I'd be interested to see your solution that splits line by line. (I never studied TeX properly. Later this time, I'll take some more time to do this.)
    – user10274
    Jan 23, 2012 at 12:45
  • 1
    @MarcvanDongen Essentially you put everything in a box and you vsplit at the baselines iteratively. Have a look at vsplit and the lineno package, if you want to find a bit more about this sort of method. Your solution is good also. Jan 23, 2012 at 12:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .