# Is there a way to automatically change kerning between commas/full stops and footnote marks?

This question led to a new package:
fnpct

Following up on my question on the kerning of footnote marks after punctuation marks, I'd like to know if there is a way to achieve the kerning of footnote marks over a comma or full stop automatically or with a better macro than the one I provided there. I'm going to repeat my result and the macro I suggested.

Here the footnote marks are moved to the left by a little less than a third of the width of the punctuation marks and the normal footnote marks (that is, those that come after any other symbol or letter) are moved right by the same amount, as inspired by http://www.read.seas.harvard.edu/~kohler/latex.html and shown in my comment:

This is achieved with the following code:

\documentclass{article}
% URW Classico is a font that makes the issue apparent
\renewcommand{\sfdefault}{uop}
\renewcommand{\familydefault}{\sfdefault}
% define punctuation-aware footnote macro
\let\origfootnote\footnote
\renewcommand{\footnote}[1]{\kern.06em\origfootnote{#1}}
\newcommand{\punctfootnote}[1]{\kern-.06em\origfootnote{#1}}
% define a very small
\setlength{\textwidth}{150pt}
\setlength{\textheight}{5\baselineskip}
\begin{document}
\noindent The three little pigs built their houses out of
straw,\punctfootnote{not to be confused with hay}
sticks\footnote{or lumber according to some sources} and
bricks.\punctfootnote{probably fired clay bricks}
\end{document}


Using LuaLaTeX is an option for me. I'm also aware of the existing question regarding automatic kerning but I know too little to adapt the LuaLaTeX code there to my problem.

-

Edit - using the new package:

With the package fnpct now available just use \usepackage{fnpct} and make sure the punctuation marks follow the footnotes:

\documentclass{article}
% for demonstration purposes only: make the page small!
\usepackage[
paperwidth=.5\textwidth,
paperheight=15\baselineskip,
margin=5pt,
bottom=1.5cm]{geometry}

\usepackage{fnpct}

\begin{document}
\noindent The three little pigs built their houses
out of straw\footnote{not to be confused with hay},
sticks\footnote{or lumber according to some sources}
and bricks\footnote{probably fired clay bricks}.

% without fnpct' for comparison:
\setfnpct{dont-mess-around}\setcounter{footnote}{0}
\noindent The three little pigs built their houses
out of straw\footnote{not to be confused with hay},
sticks\footnote{or lumber according to some sources}
and bricks\footnote{probably fired clay bricks}.
\end{document}


The following code is an attempt using expl3. It uses the values of your answer to your related question: skip back .06em if a dot or comma follows else insert .06em.

\documentclass{article}

\renewcommand\familydefault{\sfdefault}
\usepackage{xparse}

\ExplSyntaxOn
% save definition of \footnote
\cs_new_eq:NN \fnpct_orig_footnote:w \footnote

% redefine \footnote
\RenewDocumentCommand \footnote { om }
{ \fnpct_dot_or_comma:nn { #1 } { #2 } }

% internal footnote function:
\cs_new:Npn \fnpct_dot_or_comma:nn #1#2
{
% if a dot follows remove it, insert dot, skip back
% and then insert footnote
\peek_meaning_remove:NTF .
{
. \skip_horizontal:n { -.06em }
\IfNoValueTF { #1 }
{ \fnpct_orig_footnote:w { #2 } }
{ \fnpct_orig_footnote:w [ #1 ] { #2 } }
}
{
% else do the same with comma
\peek_meaning_remove:NTF ,
{
, \skip_horizontal:n { -.06em }
\IfNoValueTF { #1 }
{ \fnpct_orig_footnote:w { #2 } }
{ \fnpct_orig_footnote:w [ #1 ] { #2 } }
}
{
% else insert space and then footnote
\skip_horizontal:n { .06em }
\IfNoValueTF { #1 }
{ \fnpct_orig_footnote:w { #2 } }
{ \fnpct_orig_footnote:w [ #1 ] { #2 } }
}
}
}
\ExplSyntaxOff

\setlength\textwidth{150pt}
\setlength\textheight{5\baselineskip}

\begin{document}

\noindent The three little pigs built their houses
out of straw\footnote{not to be confused with hay},
sticks\footnote{or lumber according to some sources} and
bricks\footnote{probably fired clay bricks}.

\end{document}

-
Wow, that's exactly the kind of magic I was searching for. Apparently parsing backwards is harder still but this will do nicely. Thanks a lot! :) –  Christian May 17 '12 at 12:25
Should be made into a package BTW. fixfoot or something. –  Christian May 17 '12 at 12:25
@Christian I am not so sure about that. A package should ensure compatibility with other packages that change footnote behaviour and also treat \footcite and similar citing commands the same way... as it is the code is not package material IMHO. –  cgnieder May 17 '12 at 13:17
Yes, treating endnotes and other footnote-style stuff the same would be the main problem. –  Christian May 17 '12 at 13:24
This is amazing work! I almost can't keep up with looking at the changes. I would feel bad for having caused all this work for you if it wasn't for such an awesome result. Let us hope many people stumble upon this package and subtly improve the typesetting of their footnotes! This might also be the one footnote package. To configure them all and in the \setfnpct bind them. To show some gratitude, let me award you a post-hoc bounty :) –  Christian May 25 '12 at 7:10

To achieve your goal, it seems that you have to hack into the \footnotecommand to check the previous character. I am not able to show you workable tex-code, but in the package footmisc Robin Fairbairns has coded an option called multiple. That option add a comma in between multiple footnote marks, in case you have multiple footnote at the same place. This implies that you have to know that the previous character is footnote mark. Maybe you can adapt the code for your purpose.

The code is commented under section 6.1 in the manual, and it is advanced ... I'm not a TeX-guru, and unable to decode the code.

Your link http://tex.stackexchange.com/a/10457/9632 gives answers to the question: Is it possibility to kern two character pairs in LaTeX. LuaLaTeX and fontspec seems to be the easiest solution. And in this answer; http://tex.stackexchange.com/a/53594/9632 ; you will find some luacode` to adjust kerning on the fly.

On the other hand, maybe it is less work (but less fun) to do it manually, when you do the very last proofreading.

-
I don't understand the code either but it doesn't necessarily have to parse backwards – parsing forwards would do just as well. Your second link is one that refers to the first one and I can't figure out how to adapt this for my problem either :/ –  Christian May 17 '12 at 12:17
@Christian Sorry, this was the best I could come up with. My programming skills are very limited (i.e. nil). I tried, at least. =:-) –  Sveinung May 17 '12 at 20:25
All ideas are welcome. You never know which ones work in advance. Oh and TeX is a nightmare of a language. I wouldn't judge my own programming skills to be nil but most advanced TeX packages look little better than /dev/random to me I must say. –  Christian May 18 '12 at 10:14