You can use some do some arithmetic to calculate the number of lines needs to indent for the shorter (by height) text, then use \hangindent and \hangafter to typeset the longer text, and add the shorter one in a box:
\documentclass[12pt]{book}
\usepackage{lipsum}
\usepackage[
paperwidth=160mm, paperheight=240mm,
textheight=18cm,
textwidth=10.5cm,
inner=2cm,
showframe]
{geometry}
\def\divtrancate(#1/#2){%
\ifnum\numexpr #1 - #2*\numexpr #1/#2\relax<0
\the\numexpr #1/#2\relax\else\the\numexpr #1/#2+1\relax\fi
}
\newbox\mishna
\newbox\tosafot
\def\talmudlayout#1 #2\par#3\par{
\setbox\mishna\vbox{\hsize=7.5cm\relax\noindent\textbf{#1}.\hskip 1em\relax #2}
\setbox\tosafot\vbox{\hsize=2.5cm\relax\scriptsize\noindent #3\par}
\ifdim\ht\mishna>\ht\tosafot
\hangafter=-\divtrancate(\dimexpr\ht\tosafot\relax/\dimexpr\baselineskip\relax)
\hangindent=-3cm
\noindent
\vadjust{\moveright 8cm\vbox to 0pt{\vskip-.5\baselineskip\box\tosafot\vss}}%
\textbf{#1}.\hskip 1em\relax #2\par
\else
\hangafter=-\divtrancate(\dimexpr\ht\mishna\relax/\dimexpr 9.5pt\relax)
\hangindent=8cm
\noindent
\vadjust{\vbox to 0pt{\vskip-.5\baselineskip\box\mishna\vss}}%
{\scriptsize #3\par}
\fi
}
\begin{document}
\talmudlayout First
This is a long text, way longer than the comment at its margin.
So there is no need to keep the paragraph width as is but it can
be expanded by 3 cm. \lipsum[1]
This is a small margin paragraph, way smaller than the text it comments.
\newpage
\talmudlayout Second
This is a short text, way shorter than the comment at its margin.
So there is no need to keep the comment width as is but it can
be expanded by 8 cm.
This is a long margin paragraph, way longer than the text it comments.
\lipsum[1]
\end{document}
If you want to change the side of the comment depending on the page number, just like \marginnote, simply add another test in the macro:
\documentclass[12pt]{book}
\usepackage{lipsum}
\usepackage[
paperwidth=160mm, paperheight=240mm,
textheight=18cm,
textwidth=10.5cm,
inner=2cm,
showframe]
{geometry}
\makeatletter
\newcommand*\writetalmudpage{%
\write@auxout{%
\string\expandafter\gdef\string\csname\space talmudlayout@\thetalmudpage\string\endcsname{\thepage}}}
\makeatother
\newcommand*\defloop[1]{%
\expandafter\def\csname talmudlayout@#1\endcsname{0}
\ifnum#1<100
\expandafter\defloop
\expandafter{\number\numexpr#1+1\expandafter}%
\fi}
\defloop{1}
\def\divtrancate(#1/#2){%
\ifnum\numexpr #1 - #2*\numexpr #1/#2\relax<0
\the\numexpr #1/#2\relax\else\the\numexpr #1/#2+1\relax\fi
}
\newcounter{talmudpage}
\newbox\mishna
\newbox\tosafot
\def\talmudlayout#1 #2\par#3\par{
\stepcounter{talmudpage}
\setbox\mishna\vbox{\hsize=7.5cm\relax\noindent\textbf{#1}.\hskip 1em\relax #2}
\setbox\tosafot\vbox{\hsize=2.5cm\relax\scriptsize\noindent #3\par}
\ifdim\ht\mishna>\ht\tosafot
\hangafter=-\divtrancate(\dimexpr\ht\tosafot\relax/\dimexpr\baselineskip\relax)
\hangindent=\ifodd\csname talmudlayout@\thetalmudpage\endcsname -3cm\else 3cm\fi
\noindent\writetalmudpage
\vadjust{\ifodd\csname talmudlayout@\thetalmudpage\endcsname \moveright 8cm\fi\vbox to 0pt{\vskip-.5\baselineskip\box\tosafot\vss}}%
\textbf{#1}.\hskip 1em\relax #2\par
\else
\hangafter=-\divtrancate(\dimexpr\ht\mishna\relax/\dimexpr 9.5pt\relax)
\ifodd\csname talmudlayout@\thetalmudpage\endcsname \hangindent=8cm\else \hangindent=-8cm\fi
\noindent\writetalmudpage
\vadjust{\ifodd\csname talmudlayout@\thetalmudpage\endcsname\else \moveright 3cm\fi\vbox to 0pt{\vskip-.5\baselineskip\box\mishna\vss}}%
{\scriptsize #3\par}
\fi
}
\begin{document}
\sloppy
\talmudlayout First
This is a long text, way longer than the comment at its margin.
So there is no need to keep the paragraph width as is but it can
be expanded by 3 cm. \lipsum[1]
This is a small margin paragraph, way smaller than the text it comments.
\newpage
\talmudlayout Second
This is a long text, way longer than the comment at its margin.
So there is no need to keep the paragraph width as is but it can
be expanded by 3 cm. \lipsum[1]
This is a small margin paragraph, way smaller than the text it comments.
\newpage
\talmudlayout Third
This is a short text, way shorter than the comment at its margin.
So there is no need to keep the comment width as is but it can
be expanded by 8 cm.
This is a long margin paragraph, way longer than the text it comments.
\lipsum[1]
\newpage
\talmudlayout Fourth
This is a short text, way shorter than the comment at its margin.
So there is no need to keep the comment width as is but it can
be expanded by 8 cm.
This is a long margin paragraph, way longer than the text it comments.
\lipsum[1]
\end{document}
Edit
The second block of code had some issues, so I fixed it. It now need two compilations to get the correct results.
As for the questions in the comments, I think making a package would be the easiest thing to do here.
Put the following code in a file called talmudlayout.sty:
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{talmudlayout}
\RequirePackage{xkeyval}
\RequirePackage{footnote}
\newlength\talmudlayout@mishna@width
\setlength\talmudlayout@mishna@width{.7\textwidth}
\newlength\talmudlayout@tosafot@width
\setlength\talmudlayout@tosafot@width{.25\textwidth}
\newlength\talmudlayout@mishnahead@hsep
\setlength\talmudlayout@mishnahead@hsep{.05\textwidth}
\newcommand\talmudlayout@tosafot@size{scriptsize}
\newcommand\talmudlayout@mishna@ext{0}
\newcommand\talmudlayout@tosafot@ext{0}
\newcommand\talmudlayout@mishnahead@ext{0}
\newcommand*\talmudlayout@max{30}
\DeclareOptionX{tosafot}{\renewcommand\talmudlayout@tosafot@ext{#1}}
\DeclareOptionX{mishna}{\renewcommand\talmudlayout@mishna@ext{#1}}
\DeclareOptionX{head}{\renewcommand\talmudlayout@mishnahead@ext{#1}}
\DeclareOptionX{max}{\talmudlayout@max}
\DeclareOptionX{headsep}{\setlength\talmudlayout@mishnahead@hsep{#1}}
\DeclareOptionX{mishnawidth}{\setlength\talmudlayout@mishna@width{#1}}
\DeclareOptionX{tosafotwidth}{\setlength\talmudlayout@tosafot@width{#1}}
\DeclareOptionX{tosafotsize}{\renewcommand\talmudlayout@tosafot@size{#1}}
\ProcessOptionsX\relax
\newif\if@talmudlyaout@headmax
\newcommand\talmudlayout@savecounters{%
\begingroup
\def@elt##1{%
\global\csname c@##1\endcsname\the\csname c@##1\endcsname}%
\xdef@gtempa{%
\cl@@ckpt
\let\noexpand\talmudlayout@restorecounters\noexpand@empty
}%
\endgroup
\let\talmudlayout@restorecounters@gtempa
}
\newcommand\talmudlayout@restorecounters{}
\let\talmudlayout@restorecounters@empty
\newcommand*\talmudlayout@skip{}
{\csname \talmudlayout@tosafot@size\endcsname\xdef\talmudlayout@skip{\the\baselineskip}}
\newcommand*\write@talmudlayout@page{%
\write@auxout{%
\string\expandafter\gdef\string\csname\space talmudlayout@\thetalmudlayout@page\string\endcsname{\thepage}}}
\newcommand*\talmudlayout@defloop[1]{%
\expandafter\def\csname talmudlayout@#1\endcsname{0}
\ifnum#1<\talmudlayout@max
\expandafter\talmudlayout@defloop
\expandafter{\number\numexpr#1+1\expandafter}%
\fi}
\talmudlayout@defloop{1}
\newcommand*\talmudlayout@exploop[3]{% #1 = start, #2 = max, #3 = min
\ifnum #1<\numexpr #3+1\relax
\ifodd\csname talmudlayout@\thetalmudlayout@page\endcsname
\dimexpr \wd\talmudlayout@mishnaheadh + \talmudlayout@mishnahead@hsep\relax
\dimexpr \talmudlayout@mishna@width - \wd\talmudlayout@mishnaheadh-\talmudlayout@mishnahead@hsep\relax
\else
\dimexpr \textwidth-\talmudlayout@mishna@width \relax
\dimexpr \talmudlayout@mishna@width - \wd\talmudlayout@mishnaheadh-\talmudlayout@mishnahead@hsep\relax
\fi
\else
\if@talmudlyaout@headmax
\ifodd\csname talmudlayout@\thetalmudlayout@page\endcsname
\dimexpr \wd\talmudlayout@mishnaheadh+\talmudlayout@mishnahead@hsep\relax
\dimexpr \textwidth-\wd\talmudlayout@mishnaheadh-\talmudlayout@mishnahead@hsep\relax
\else
\z@ \dimexpr\textwidth-\wd\talmudlayout@mishnaheadh-\talmudlayout@mishnahead@hsep\relax
\fi
\else
\ifodd\csname talmudlayout@\thetalmudlayout@page\endcsname
\z@ \dimexpr\talmudlayout@mishna@width\relax
\else
\dimexpr \textwidth-\talmudlayout@mishna@width \relax \talmudlayout@mishna@width
\fi
\fi
\fi
\ifnum#1<#2
\expandafter\talmudlayout@exploop
\expandafter{\the\numexpr#1+1\expandafter}%
\expandafter{\the\numexpr#2\expandafter}%
\expandafter{\the\numexpr#3\expandafter}%
\fi}
\newcommand\talmudlayout@divtrancate[2]{%
\ifnum\numexpr #1 - #2\numexpr #1/#2\relax<0
\the\numexpr #1/#2\relax\else\the\numexpr #1/#2+1\relax\fi
}
\newcommand*\talmudlayout@parshape[2]{
\ifnum #1 < #2
@talmudlyaout@headmaxtrue
\parshape \numexpr #2+1\relax
\talmudlayout@exploop{1}{#2}{#1}
\else
@talmudlyaout@headmaxfalse
\parshape \numexpr #1+1\relax
\talmudlayout@exploop{1}{#1}{#2}
\fi
\z@ \textwidth
}
\def\talmudlayout@mishna@box#1#2#3{\vbox{\hsize=\talmudlayout@mishna@width
\hangafter=-\numexpr\talmudlayout@divtrancate{\dimexpr\ht\talmudlayout@mishnaheadv\relax}{\dimexpr \baselineskip\relax}+\talmudlayout@mishnahead@ext\relax
\ifodd\csname talmudlayout@\thetalmudlayout@page\endcsname
\hangindent=\dimexpr \wd\talmudlayout@mishnaheadh + \talmudlayout@mishnahead@hsep\relax
\else
\hangindent=-\dimexpr \wd\talmudlayout@mishnaheadh + \talmudlayout@mishnahead@hsep\relax
\fi
\noindent
\vadjust{\ifodd\csname talmudlayout@\thetalmudlayout@page\endcsname\else \moveright \dimexpr \talmudlayout@mishna@width - \wd\talmudlayout@mishnaheadh\relax\fi
\vbox to 0pt{\vskip-.75\baselineskip\bfseries\fontsize{46pt}{46pt}\selectfont\noindent #1\vss}}%
\strut\textbf{#2}.\hskip 1em\relax #3}}
\newbox\talmudlayout@mishnaheadh
\newbox\talmudlayout@mishnaheadv
\newbox\talmudlayout@mishna
\newbox\talmudlayout@tosafot
\newcounter{talmudlayout@page}
\newcommand*\talmudlayout{}
\long\protected\def\talmudlayout#1 #2 #3\par#4\par{
\stepcounter{talmudlayout@page}
\talmudlayout@savecounters
\setbox\talmudlayout@mishnaheadh\hbox{\bfseries\fontsize{46pt}{46pt}\selectfont\noindent #1}
\talmudlayout@restorecounters\talmudlayout@savecounters
\setbox\talmudlayout@mishnaheadv\vbox{\bfseries\fontsize{46pt}{46pt}\selectfont\noindent #1}
\talmudlayout@restorecounters\talmudlayout@savecounters
\setbox\talmudlayout@mishna\talmudlayout@mishna@box{#1}{#2}{#3}
\talmudlayout@restorecounters\talmudlayout@savecounters
\setbox\talmudlayout@tosafot\vbox{\hsize=\talmudlayout@tosafot@width\csname \talmudlayout@tosafot@size\endcsname\noindent\strut #4\par}
\talmudlayout@restorecounters\savenotes
\ifdim\ht\talmudlayout@mishna>\ht\talmudlayout@tosafot
\talmudlayout@parshape{\numexpr\talmudlayout@divtrancate{\dimexpr\ht\talmudlayout@tosafot\relax}{\dimexpr \baselineskip\relax}+\talmudlayout@mishna@ext\relax}%
{\numexpr\talmudlayout@divtrancate{\dimexpr\ht\talmudlayout@mishnaheadv\relax}{\dimexpr \baselineskip\relax}+\talmudlayout@mishnahead@ext\relax}
\noindent\write@talmudlayout@page
\vadjust{\ifodd\csname talmudlayout@\thetalmudlayout@page\endcsname \moveright \dimexpr\textwidth-\talmudlayout@tosafot@width\relax\fi
\vbox to 0pt{\vskip-.75\baselineskip\vbox{\hsize=\talmudlayout@tosafot@width\csname \talmudlayout@tosafot@size\endcsname\noindent\strut #4\par}\vss}}%
\vadjust{\ifodd\csname talmudlayout@\thetalmudlayout@page\endcsname\else \moveright \dimexpr\textwidth-\wd\talmudlayout@mishnaheadh\relax\fi
\vbox to 0pt{\vskip-.75\baselineskip\bfseries\fontsize{46pt}{46pt}\selectfont\noindent #1\vss}}%
\strut\textbf{#2}.\hskip 1em\relax #3\par
\else
\hangafter=-\numexpr\talmudlayout@divtrancate{\dimexpr\ht\talmudlayout@mishna\relax}{\dimexpr \talmudlayout@skip\relax}+\talmudlayout@tosafot@ext\relax
\ifodd\csname talmudlayout@\thetalmudlayout@page\endcsname
\hangindent=\dimexpr\textwidth-\talmudlayout@tosafot@width
\else
\hangindent=-\dimexpr\textwidth-\talmudlayout@tosafot@width
\fi
\noindent\write@talmudlayout@page
\vadjust{\ifodd\csname talmudlayout@\thetalmudlayout@page\endcsname\else \moveright \dimexpr\textwidth-\talmudlayout@mishna@width\relax\fi
\vbox to 0pt{\vskip-\talmudlayout@skip\talmudlayout@mishna@box{#1}{#2}{#3}\vss}}%
{\csname \talmudlayout@tosafot@size\endcsname\strut #4\par}
\fi
\par
\spewnotes
}
\endinput
Now I think you want something similar to the following document
\documentclass[12pt]{book}
\usepackage{lipsum}
\usepackage[
paperwidth=160mm, paperheight=240mm,
textheight=18cm,
textwidth=10.5cm,
inner=2cm]
{geometry}
\usepackage[
tosafot=1,
mishna=1,
head=1,
headsep=1cm,
mishnawidth=7.5cm,
tosafotwidth=2.5cm]
{talmudlayout}
\begin{document}
\sloppy
\talmudlayout Big First
This is a long text, way longer than the comment at its margin.
So there is no need to keep the paragraph width as is but it can
be expanded by 3 cm. \lipsum[1]
This is a small margin paragraph, way smaller than the text it comments.
\newpage
\talmudlayout Big Second
This is a long text, way longer than the comment at its margin.
So there is no need to keep the paragraph width as is but it can
be expanded by 3 cm. \lipsum[1]
This is a small margin paragraph, way smaller than the text it comments.
\newpage
\talmudlayout Big Third
This is a short text, way shorter than the comment at its margin.
So there is no need to keep the comment width as is but it can
be expanded by 8 cm.
This is a long margin paragraph, way longer than the text it comments.
\lipsum[1]
\newpage
\talmudlayout Big Fourth
This is a short text, way shorter than the comment at its margin.
So there is no need to keep the comment width as is but it can
be expanded by 8 cm.
This is a long margin paragraph, way longer than the text it comments.
\lipsum[1]
\end{document}
The package defines a macro called \talmudlayout which accept 4 arguments.
- The first is the head of the main text, and is delimited by space after it.
- The second is the first word of the main text and is delimited by a space.
- The third is the main text, and it is delimited by
\par so if you will want several paragraphs in this argument, you will have to use \endgraf, or change the delimiters of the arguments.
- The fourth is the margin text and it is also delimited by \par, same remarks apply to him.
Feel free to change the delimiters if you find the syntax inconvenient.
The package has 8 options.
tosafot, head and mishna adds extra lines of space below the specified part (the default is 0).
mishnawidth and tosafotwidth controls the width of the main and "margin" text (at least when they overlap, defaults are .7\textwidth and .25\textwidth).
headsep is the horizontal space between the head and the main text (default is .05\textwidth).
max is the maximum allowed of layouts in a document (default is 30)
tosafotsize is a name of a command size to control the size of the margin text (default is scriptsize).
This code will need two compilations as well. Feel free to change the default values of the options, I did not put much thought about them.
For a better alignment of the first lines you will need to tweak some of the length I used in the code, probably tweaking the length of the \vskip will eventually get you what you want.
The example file presents the four possible situation of the layout.
- The main text is longer then the margin text, and the page number is odd

- The main text is longer then the margin text, and the page number is even

- The main text is shorter then the margin text, and the page number is odd

- The main text is shorter then the margin text, and the page number is even

marginparwidth=in yourgeometryoptions. But what do you mean by towards the inner part? Seen from the margin, the inner part is where the text area is. Expanding in this direction has to shrink the text area, as long as you don't decrease theinneroption or themarginparsep. – lukeflo Aug 23 '23 at 07:27showframeoption withgeometry, so you can see the borders of your margin etc. – lukeflo Aug 23 '23 at 07:31