# Is it possible to declare that the footnote number is right aligned but uses a dynamic width?

Our document has footnotes ranging from 1 to more than 100. I would like to see the footnote numbers

1. right-aligned to the footnote text
2. the space that is used for the footnote number should be dynamically adjusted, depending on the number of digits of the footnotes that are present on a particular page.
3. indent the text and the paragraph of a footnote identically

Sounds complicated, right? So here is an example: in case I have only one-digit footnotes on a page it should look like the following:

some text^4 and more text^5
-----------------
4 Foo bar
5 Bar Baz


If one and two digits are used it should look like the following:

some text^9 and more text^10
-----------------
9 Foo bar
10 Bar Baz


and so on.

Currently I use the deffootnote option as follows:

\documentclass{scrartcl}

\deffootnote{1.85em}{1.85em}{\thefootnotemark\hspace{0.5em}}

\usepackage{blindtext}% dummy text

\usepackage{hyperref}
\begin{document}

\setcounter{footnote}{6}

a\footnote{\blindtext}b\footnote{\blindtext}

\newpage

a\footnote{\blindtext}b\footnote{\blindtext}

\newpage

\setcounter{footnote}{98}

a\footnote{\blindtext}b\footnote{\blindtext}

\end{document}


But then I have to use a fixed width for all footnote numbers, i.e. a width which is compatible for the three-digit footnote numbers.

This is interesting problem because of asynchronous processing footnotes and pages. But it has solution using \write. I show such solution in plain TeX using OPmac because I don't support LaTeX. My example shows that the problem is solvable. And you can be inspired by this and use these ideas in another TeX formats if it is needed.

\def\Xfnotei#1#2{\sdef{fnpg:#1}{#2}\sdef{fntpg:#2}{#1}}
\input opmac

\runningfnotes
\def\thefnote{$^{\the\fnotenum}$}
\def\fnotehook{\def\thefnote{\fnindent \kern.5em\llap{\the\fnotenum}\kern.5em
\expandafter\wref\expandafter\Xfnotei
\expandafter{\expandafter{\the\fnotenum}{\the\pageno}}}}

\def\fnmax{\csname fntpg:\csname fnpg:\the\fnotenum\endcsname\endcsname}
\def\fnindent{\isdefined{fnpg:\the\fnotenum}\iftrue
\ifnum\fnmax<10                    % one digit footnotes (or default)
\else \ifnum\fnmax<100  \kern.5em  % two digit footnotes
\else \ifnum\fnmax<1000 \kern1em   % three digit footnotes
\else                   \kern1.5em % four digit footnotes
\fi\fi\fi\fi
}
\let\textindent=\noindent

A test:

a\fnote{aaa} b\fnote{bbb} c\fnote{ccc} d\fnote{ddd}
e\fnote{eee} f\fnote{fff}

\vfil\break

g\fnote{ggg} h\fnote{hhh} i\fnote{iii} j\fnote{jjj}
k\fnote{kkk}.

\end


If you want to try this you can run tex example or pdftex example, no latex example. You must run TeX twice in order to reach the result.

Explanation: Each footnote has unique number \the\fnotenum which will be printed as a mark using \thefnote macro. This macro has two variants, default for in-text marks and redefined in \fnotehook for marks in footnotes. And this mark is defined as follows:

\fnindent \kern.5em \llap{\the\fnotenum}\kern.5em \write...


The \fnindent macro does nothing in the first TeX run, so all footnotes are typeset as one-digit foontnote with the number lapped left. But \write writes needed information into .ref file which is read by OPmac in the second (and more) TeX run. The information is in the form:

\Xfnotei{fnote-number}{page-number}


The macro \Xfnotei saves this information into the form:

• macro \fnpg:fnote-number includes page-number
• macro \fntpg:page-number includes the maximal fonte-number in this page.

Finally, the macro \fnindent uses such information for indentation.

• Impressive. Works! – LaRiFaRi Sep 4 '15 at 8:21
• This is indeed very impressive. Unfortunately I am not so much into TeX and LaTeX, that I feel I would be able to adapt it to my LaTeX document. I hoped for an existing LaTeX solution/package but perhaps someone with more LaTeX skills will pick your solution up at some time and adapt it. Still, thanks for your help. – taocp Sep 5 '15 at 16:21