[Tex/LaTex] Paragraph short footnotes following multiline footnotes; prevent footnotes being paragraphed if they go onto a second line

bigfootfootmiscfootnotes

Is it possible to have footnotes which are paragraphed according to the following rule (common in my discipline, history)?

Footnotes always begin on a new line, unless there is sufficient whitespace following the previous footnote to fit the entire footnote.

Thus: a) short footnotes can be paragraphed following a multi-line footnote, provided there is sufficient blank space; and b) footnotes which do not begin at the left margin (i.e. which are paragraphed following another footnote) can never run onto the following line.

Here is an example of effect (a): short footnotes paragraphed after multi-line footnotes:

Example of effect (a): short footnotes paragraphed after multi-line footnotes

Effect (b) is harder to illustrate (as it is proving a negative), but as far as I can tell, in historical monographs published by (for example) Oxford University Press or Cambridge University Press, footnotes are never set in the position of footnotes 3 or 5 in the image above if they would then run onto the following line—even if setting them that way would save on the total vertical space the footnote block requires.

As far as I can tell, the bigfoot package paragraphs footnotes according to two rules: 1. Footnotes which are more than one line long are not paragraphed with anything, preceding or following. 2. That condition satisfied, set the footnotes in whatever way will minimize the vertical space required.
(This may be wrong; it is just based on observed effects.)

So, for example, this:

\documentclass{article}
\usepackage[para]{bigfoot}
\DeclareNewFootnote[para]{default}
\begin{document}
The first sentence.\footnote{This is a long footnote, to show that short footnotes are not paragraphed following multiline footnotes.}
The second sentence.\footnote{A short footnote.}
The third sentence.\footnote{This is a somewhat longer footnote, to show that it is allowed to overrun the line.}
The third sentence.\footnote{This is another short footnote.}
\end{document}

Renders as this, for me:

Example of bigfoot footnotes not following the rules proposed.

I would want 'Footnote 2' to be in the whitespace following 'Footnote 1'. And if 'Footnote 2' were placed as it is, then 'Footnote 3' would not be paragraphed following it, as it runs into the next line.

The second (b) effect which I want can be done manually with 'post-processing' by changing any footnote which runs over like that to be \footnote+{Example text.}, which forces it onto its own line. But that has to be done as the very last thing, as it changes pagination and cross-references. footmisc will achieve effect (a), but only by running all footnotes together as a single paragraph, which is not the goal.

Finally, in an ideal solution, any footnotes placed together on a line would be spread as far apart as possible, or at least have substantial whitespace left (as in the first picture above).

Best Answer

I started with your picture, i.e. footnotes begin at the left margin or in the middle.

I can show how this can be done in plain TeX. If you are using different macro package then you can inspire by this code and re-implement it to your favorite macro package.

The code (you can try it by the command tex test.tex):

\newcount\specfootnum % for global counting the footnotes
\newcount\fnotenum    % for footnote marks
\newif\ifrepeat

\tracingpages=1

\def\footnote{\global\advance\fnotenum by1 \fnmark\footnoteA}  
\def\footnoteA#1{\global\advance\specfootnum by1
   \edef\tmp{\indent\llap{\fnmark\kern2pt}}%   
   \expandafter\gdef\csname specfoot:\the\specfootnum
                    \expandafter\endcsname\expandafter{\tmp#1}%
   \setbox0=\hbox{\tmp#1}%
   \ifdim\wd0<.45\hsize \dimen0=.5\baselineskip
   \else \ifdim\wd0>\hsize \setbox0=\vbox{\tmp\strut#1\strut\par\kern0pt}\dimen0=\ht0
         \else\dimen0=\baselineskip 
   \fi\fi
   \insert\footins{\floatingpenalty=20000
                   \vbox to\dimen0{\vss\penalty\specfootnum}\penalty0}%
}
\def\fnmark{$^{\the\fnotenum}$}

\catcode`@=11
\def\pagecontents{\ifvoid\topins\else\unvbox\topins\fi
  \dimen@=\dp\@cclv \unvbox\@cclv % open up \box255
  \ifvoid\footins\else % footnote info is present
    \vskip\skip\footins \footnoterule \printspecfoot \fi
  \ifr@ggedbottom \kern-\dimen@ \vfil \fi
}
\catcode`@=12

\def\printspecfoot{\bgroup\def\tmp{}%
  \setbox0=\vbox{\repeattrue \unvbox\footins 
     \loop \unpenalty \setbox0=\lastbox
        \ifvoid0 \repeatfalse 
        \else \setbox0=\vbox{\unvbox0 \xdef\tmp{\the\lastpenalty,\tmp}}\fi
        \ifrepeat \repeat
                }%
    \dimen1=\hsize \rm
    \expandafter\printspecfootA\tmp,
}
\def\printspecfootA#1,{\ifx,#1,\egroup\else
   \ifdim\dimen1<.45\hsize 
      \setbox0=\hbox{\csname specfoot:#1\endcsname}%
      \ifdim\wd0<.5\hsize
          \vskip-\baselineskip \vskip-\parskip
          \noindent\hskip.5\hsize \hskip-.5\parindent \box0 \par
          \dimen1=\hsize
      \else
          \noindent\unhbox0 \newdimenone
      \fi
   \else \csname specfoot:#1\endcsname \newdimenone \fi
   \global\expandafter\let\csname specfoot:#1\endcsname=\relax
   \expandafter \printspecfootA\fi
}
\def\newdimenone{$$\global\dimen1=\predisplaysize
  \abovedisplayskip=0pt \belowdisplayskip=0pt
  \abovedisplayshortskip=0pt \belowdisplayshortskip=0pt
  $$\advance\dimen1 by-2em\vskip-\baselineskip
}

%%% the test:

\vsize=120pt
\raggedbottom

  Lorem ipsum dolor sit amet, consectetuer%
\footnote{First text.}
  adipiscing elit. Ut purus elit, vestibulum ut, placerat ac,   
  adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu%
\footnote{Second text.}
  libero, nonummy eget, consectetuer id, vulputate a, magna. Donec
  vehicula augue eu neque. Pellentesque habitant morbi tristique  
  senectus et netus et malesuada fames ac turpis egestas. Mauris ut  
  leo. Cras viverra metus rhoncus sem. Nulla et lectus vestibulum urna%
\footnote{Third text.}
  fringilla ultrices.  Phasellus eu tellus sit amet tortor gravida  
  placerat. Integer sapien est, iaculis in, pretium quis, viverra ac,
  nunc. Praesent eget sem vel leo ultrices bibendum. Aenean faucibus.  
  Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla.
  Curabitur auctor semper nulla. Donec varius orci eget risus. Duis%
\footnote{Next text.}
  nibh mi, congue eu, accumsan eleifend, sagittis quis, diam. Duis
  eget orci sit amet orci dignissim rutrum.

  Morbi auctor lorem non justo. Nam lacus%
\footnote{Nulla malesuada porttitor diam. Donec felis
erat, congue non, volutpat at, lobortis vitae, tincidunt tristique, libero.}
  libero, pretium at, lobortis vitae, ultricies et, tellus. Donec
  aliquet, tortor sed accumsan bibendum, erat ligula aliquet magna,%
\footnote{Short text.}
  vitae ornare odio metus a mi. Morbi ac orci et nisl hendrerit%
\footnote{Next short text.}
  mollis. Suspendisse ut massa. Cras nec ante. Pellentesque a nulla.%
\footnote{Intersting short text.}
  Cum sociis natoque penatibus et magnis dis parturient montes, 
  nascetur ridiculus mus. Aliquam tincidunt urna. Nulla ullamcorper
  vestibulum turpis. Pellentesque cursus luctus mauris.%
\footnote{The last footnote.}

\bye

The result:

footnotes

The notes to the implementation: Each footnote text is saved to the control sequence \specfoot:\the\specfootnum which is unique in the whole document. The raw calculation about the vertical space of such footnote is done when \footnote macro is processed. The result is \dimen0 which is inserted to the \insert as \vbox to\dimen0{\vss\penalty\specfootnum}. This means that the real text isn't saved to the \footins box. Only empty box with the "message" about the number of the footnote is icluded here.

What does \output routine? It creates the list of numbers of the footnotes at the current page, because the \penalties inside boxes are read. The list of current footnotes are set to the \tmp macro. Then the typesetting of these footnotes is processed using control sequences \specfoot:\the\specfootnum. Printing of each footnote is ended by measurement of the text in the last line using \newdimenone macro. The result is set to the \dimen1. If \dimen1<.5\hsize and the width of the next footnote is less than .5\hsize then this next footnote is started by \vskip-\baselineskip and \hskip.5\hsize.

Related Question