# Could TeX typeset note text between two lines of the body?

How to typsetting the note text like

There is body blahblah blahblah I need a note here and then blah blah
blahblah blahblah blahblah blahblah blahblah blahblah blahblah blah
where the note is inserted in, and it could be break
blahblah blahblah blahblah blahblah blahblah blahblah blahblah blah


?

See picture below for more detail.

-
I have no answer to your question but a suggestion: Don't do that. It makes reading of the main text hard to impossible (depending on the amount of notes). Use footnotes or (even better) sidenotes. So if no explanation needed one is not forced to interrupt the reading and to look for the start of the new line. –  schmendrich Aug 3 '12 at 8:43
@TEXprimer What is the context here? This looks like it might be linguistic analysis, for which there are specialist LaTeX packages. Is that the case? –  Joseph Wright Aug 3 '12 at 8:44
@schmendrich Thanks your advice. The note can be formatted (such as decreasing note font size) to avoid confusion. Indeed, the style I mentioned is a typestyle in Traditional Chinese. –  TEXprimer Aug 3 '12 at 9:47
it sounds to me like you're talking about "ruby" annotations. one place to look for information is in the packages that support chinese. if you can read chinese, and your tex installation is based on tex live, texdoc ctex-faq for the chinese-based faq. you might also search ctan for "chinese" or "cjk" packages. (most of the "ruby" search returns appear to be relevant to the scripting language, though one item does have the caption "Ruby annotations in ConTeXt".) –  barbara beeton Aug 3 '12 at 11:58
'Interlinear' glosses were also common in early printed books (and manuscripts before that), most notably in glossed versions of the Bible. I would be interested in seeing this kind of functionality being implemented just for the purpose of being able to replicate the layout of such texts. (This might be something @Raphink has looked into....) On the other hand, would a solution for the Latin-based alphabets work for Chinese, and vice-versa..? –  jon Aug 3 '12 at 16:30

I'm sure the final answer will be using tikzmarks ;-)

For what it's worth, here's a simplistic pure TeX solution based on shuffling around some boxes.

Edit: Now with pagebreak enabled.

\documentclass{article}

\usepackage{color}

\newdimen\charwd
\charwd=1pt

\makeatletter
\newbox\@tempboxb
\newbox\@tempboxc
\newbox\@tempboxd
\newskip\@tempskipc

\newcommand\textwithnotes[1]
{%
\setbox\@tempboxb=\hbox{}%
\setbox\@tempboxa=\hbox\bgroup#1\egroup
\setbox\@tempboxa=
\vbox
{%
\hsize\linewidth
\unhbox\@tempboxa
}%
\setbox\@tempboxb=
\vbox
{%
\hsize\linewidth
\unhbox\@tempboxb
}%
\setbox\@tempboxa=
\vbox
{%
\offinterlineskip
\unvbox\@tempboxa
\mixlines
}%
\unvbox\@tempboxa
}

\newcommand\mixlines
{%
\setbox\@tempboxa=\lastbox
\ifvoid\@tempboxa
\else
\@tempskipa=\lastskip
\unskip
\unpenalty
\global\setbox\@tempboxb=
\vbox
{%
\unvbox\@tempboxb
\global\setbox\@tempboxc=\lastbox
\unskip
\unpenalty
}%
\setbox\@tempboxd=\box\@tempboxc
\begingroup
\mixlines
\endgroup
\vskip\@tempskipa
\vbox
{%
\offinterlineskip
{\set@color\box\@tempboxa}%
\vskip4\p@
\box\@tempboxd
}%
\fi
}

\newcommand\note[2]
{%
\egroup
\@tempdimb\wd\@tempboxb
\setbox\@tempboxb=%
\hbox
{%
\unhbox\@tempboxb\fillskip{\dimexpr\wd\@tempboxa-\@tempdimb\relax}%
\textcolor{red}{#2}%
}%
\setbox\@tempboxa=\hbox\bgroup\unhbox\@tempboxa#1%
}

\newcommand\fillskip[1]
{%
\@tempdima#1%
\loop
\ifdim\@tempdima>\z@
\vrule width \charwd height\z@\hskip\z@
\repeat
}

\begin{document}

\raggedright

\textwithnotes{foo foo \note{bar}{baz baz baz} foofoo foo
\note{bar}{baz baz baz} foofoo foo \note{bar}{baz} foofoo foo
\note{bar}{baz baz baz} foofoo foo \note{bar}{baz baz baz baz}
foofoo foo  foofoo foo \note{bar}{baz baz baz} foofoo foo
\note{bar}{baz} foofoo foo \note{bar}{baz baz baz} foofoo foo
\note{bar}{baz} foofoo foo \note{bar}{baz baz baz} foofoo foo
\note{bar}{baz} foofoo foo \note{bar}{baz baz baz} foo}

\vspace*{15cm}

\textwithnotes{foo foo \note{bar}{baz baz baz} foofoo foo
\note{bar}{baz baz baz} foofoo foo \note{bar}{baz} foofoo foo
\note{bar}{baz baz baz} foofoo foo \note{bar}{baz baz baz baz}
foofoo foo  foofoo foo \note{bar}{baz baz baz} foofoo foo
\note{bar}{baz} foofoo foo \note{bar}{baz baz baz} foofoo foo
\note{bar}{baz} foofoo foo \note{bar}{baz baz baz} foofoo foo
\note{bar}{baz} foofoo foo \note{bar}{baz baz baz} foo}

\end{document}


will give

Note this is mainly an answer to the question "could TeX do this"?

So, before it's really applicable, probably some adjustments have to be made.

Don't worry about the slight horizontal misalignment in the second line. For chinese text, where character widths and line breaks are completely fixed, this won't occur.

There, \charwd should be set to the width of one character.

-
+1 for this answer too. If I had more time I would experiment with this answer and that of @walkmanyi.... –  jon Aug 3 '12 at 23:06
@Stephan Thanks. More further, could you describe how to do with the pagebreak of text within \textwithnotes? –  TEXprimer Aug 4 '12 at 0:02
@TEXprimer Correct, there was one box too many... Try the new version :-) –  Stephan Lehmke Aug 4 '12 at 7:30

If you just want to write between the lines of text, you probably want to use combination of \raisebox and \rlap. \raisebox basically typesets the argument in a box raised, or lowered over the baseline by its given arguments. You however want TeX to make the box of virtually zero width and zero height. This is done using \rlap and arguments to \raisebox respectively. Check the documentation here and also e.g., here.

Now drawing the line from the point you mark and the note is a different matter, I made an attempt with TeX's \rule, but perhaps tikz could help better.

\documentclass{article}
\usepackage{color}

%%% Writes a simple small note above the point of use
\def\note#1{\rlap{\raisebox{.5\baselineskip}[0pt][0pt]{{\footnotesize\color{red}#1}}}}

%%% An attempt to also draw a rectangular line from the anchor to the note
\def\linenote#1{{\color{red}\rlap{\rule{0.5pt}{0.75\baselineskip}\rule[0.75\baselineskip]{5pt}{.5pt}\raisebox{.5\baselineskip}[0pt][0pt]{{\footnotesize #1}}}}}

\begin{document}

Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum.
Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem\note{Some note.} ipsum.
Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem ipsum. Lorem
ipsum. Lorem ipsum. Lorem ipsum.\linenote{A note with an anchor line.}
Lorem ipsum.
\end{document}


Resulting in the following snippet:

To explain the \note command, it basically writes its argument into a box raised by half the \baselineskip, the spacing between the lines of body text, instructs TeX to treat it as if it was zero height above the box anchor and zero height below the box anchor (the arguments to \raisebox), as well as zero width (the use of \rlap). The latter also outputs the note to the right of the anchor point. If you want to use other alignment, check \clap, or \llap. So far to the position, the text size and color are controled by the standard way.

Finally, you might want to take a look at the package todonotes. It implements something very similar to what you need, except puts the note into the margin, instead between the lines.

-
Very elegant solution, and in fact close to what I very imperfectly described above. One way to improve it would be to connect the notes and text in the manner of footnotes (historically, this would be alphabetic rather than Roman or Arabic numerals). I think that through a judicious use of \rlap and \llap versions of the \note command, and putting the interlinear text in \parboxes, it would be pretty easy to emulate these glossed texts (although this would be but one of many issues). –  jon Aug 3 '12 at 23:05