[Tex/LaTex] way to automatically change kerning between commas/full stops and footnote marks

footnoteskerningpunctuationtypography

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:

package default compensation

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.

Best Answer

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}

enter image description here


The original answer:

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}