# Customised section overlapping in margin

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.

MWE:

\documentclass{article}
\newcommand{\marginsec}[1]{%
\refstepcounter{marginsec}%
\kern-\ht0
\kern-\dp\strutbox
\llap{\box0 }
\vfill
}
}%
}
\newcounter{marginsec}

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


# 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? – Yiannis Lazarides Jan 20 '12 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 '12 at 11:01
@YiannisLazarides The numbers are verses in the Bible indeed. – McGafter Jan 20 '12 at 12:28
@McGafter I posted an answer, if you add some clarifications, I will try and provide a full solution. – Yiannis Lazarides Jan 20 '12 at 13:23
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>}. – Marc van Dongen Jan 23 '12 at 4:33

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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

% 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.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\msec@evaluate@lines%
\ifx\msec@next@py\msec@curr@py%
\edef\msec@text{\msec@first--\msec@vrse}%
\else%
\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%
\msec@text%
}%
}
% end of local macro definitions
\def\msec@curr@py{0-0}% page-ypos
\msec@verse=0\relax%
\openin\msec@numbers@input \msec@nmbr@file%
\openin\msec@ypos@input    \msec@ypos@file%
\loop%
\unless\ifeof\msec@numbers@input
\repeat
\closein\msec@numbers@input%
\closein\msec@ypos@input%
\endgroup
}

\IfFileExists{\msec@ypos@file}
{}

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

\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}%
\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}%
\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}{
[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,
citecolor={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
\phantomsection
\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}


.

-
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 '12 at 9:23
@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. – Marc van Dongen Jan 23 '12 at 9:37
@MarcvanDongen Shouldn't it be \ProvidesPackage{mvdverse} instead of \ProvidesClass? – ienissei Jan 23 '12 at 14:27
@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? – Marc van Dongen Jan 23 '12 at 15:13
@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. – Marc van Dongen Jan 23 '12 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.

\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
\else
\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

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.

-
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. – Marc van Dongen Jan 22 '12 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 '12 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. – Yiannis Lazarides Jan 23 '12 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.) – Marc van Dongen Jan 23 '12 at 12:45
@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. – Yiannis Lazarides Jan 23 '12 at 12:48