2

Is it possible to tell LaTeX, that \footnote{text} use the same number, if the text is equal?

  • 1
    Do you mean if the (same) footnotes are in the same page? Or you mean to keep the same number all pages (because usually the footnote number restarts in every page). – koleygr Oct 22 '17 at 12:32
  • Also, you can use just \footnotemark[5] to refer to the same footnote created with \footnote[5]{my text} and you don't have to create a new footnote with the same text. You have just use a counter if you want to keep the numbers on all pages. Please be more specific about what you want by giving an example. – koleygr Oct 22 '17 at 13:11
  • I do want a automatic counter and I want to keep the numbers on all pages, but if at the same page the text in \footnote{text} is the same I want that LaTeX only use on and the same number. – user146304 Oct 22 '17 at 13:35
1

Here is the solution. (It has many checks because accepts custom numbers too and updates the old one if it was already given the specific footnotetext):

\documentclass{article}
\usepackage{lipsum}
\usepackage{pgffor}
\usepackage{xcolor}

\newcounter{myfootnote}

\let\oldfootnote=\footnote

\makeatletter
\def\footnote{\let\curfootnum\undefined\@ifnextchar[%
{\@ReadOptional}{\@ReadMandatory}}
\def\@ReadOptional[#1]{\def\curfootnum{#1}\@ReadMandatory}%
\def\@ReadMandatory#1{\xdef\curfoottext{#1}%
\ifnum\themyfootnote>0%
  \xdef\Found{0}%
  \foreach \n in {1,...,\themyfootnote}%
    {\expandafter\ifx\csname FootText\n\endcsname\curfoottext%
       \xdef\temppage{\csname FootLastPage\n\endcsname}%
       \xdef\tempnum{\n}%\ifx\curfootnum\undefined\n\else\curfootnum\fi
       \xdef\Found{1}%
     \fi%
     }%
  \ifnum\Found=0%
    \let\nextavail\undefined%
    \foreach \f in {1,...,\themyfootnote}%
    {\xdef\FoundNum{0}%
    \foreach \l in {1,...,\themyfootnote}%
    {\expandafter\ifnum\csname FootLastNumber\l\endcsname=\f%
    \xdef\FoundNum{1}%
    \breakforeach%
    \fi}%
    \ifnum\FoundNum=0%
    \xdef\nextavail{\f}%
    \fi%
    }%
    \refstepcounter{myfootnote}%
    \ifx\nextavail\undefined%
    \xdef\nextavail{\themyfootnote}%
    \fi%
    \xdef\tempn{\ifx\curfootnum\undefined\nextavail\else\curfootnum\fi}%
    \global\expandafter\let\csname FootLastNumber\themyfootnote\endcsname\tempn%
    \xdef\myp{\thepage}%
    \global\expandafter\let\csname FootLastPage\themyfootnote\endcsname\myp%
    \global\expandafter\let\csname FootText\themyfootnote\endcsname\curfoottext
    \oldfootnote[\csname FootLastNumber\themyfootnote\endcsname]{\curfoottext}%
  \else%
     \ifnum\thepage=\temppage%
         \ifx\curfootnum\undefined%
         \footnotemark[\csname FootLastNumber\tempnum\endcsname]%
         \else%
         \xdef\myp{\thepage}%
         \global\expandafter\let\csname FootLastPage\tempnum\endcsname\myp%
         \global\expandafter\let\csname FootLastNumber\tempnum\endcsname\curfootnum%
         \refstepcounter{myfootnote}%
         \global\expandafter\let\csname FootLastNumber\themyfootnote\endcsname\tempnum%
         \oldfootnote[\csname FootLastNumber\tempnum\endcsname]{\curfoottext}%
         \fi%
     \else%
         \ifx\curfootnum\undefined%
         \oldfootnote[\csname FootLastNumber\tempnum\endcsname]{\curfoottext}%
         \else%
         \global\expandafter\let\csname FootLastNumber\tempnum\endcsname\curfootnum%
         \oldfootnote[\csname FootLastNumber\tempnum\endcsname]{\curfoottext}%
         \refstepcounter{myfootnote}%
         \global\expandafter\let\csname FootLastNumber\themyfootnote\endcsname\tempnum%
         \fi%
         \xdef\myp{\thepage}%
         \global\expandafter\let\csname FootLastPage\tempnum\endcsname\myp%
     \fi%
  \fi%
\else%
  \refstepcounter{myfootnote}\global\expandafter\let\csname FootText\themyfootnote\endcsname\curfoottext%
  \xdef\temp{\thepage}%
  \global\expandafter\let\csname FootLastPage\themyfootnote\endcsname\temp%
  \xdef\temp{\ifx\curfootnum\undefined\themyfootnote\else\curfootnum\fi}%
  \global\expandafter\let\csname FootLastNumber\themyfootnote\endcsname\temp%
  \oldfootnote[\csname FootLastNumber\themyfootnote\endcsname]{\curfoottext}%
\fi}%
\makeatother


\begin{document}
\lipsum[1]
{\color{red}test1\footnote{test}}
\lipsum[1]
{\color{red}test2 same as 1\footnote{test}}
\lipsum[2]
{\color{red}test3 different\footnote{test2}}
\lipsum[1-3]
{\color{red}test4 same as 1 different page\footnote{test}}
\lipsum[1-2]
\lipsum[1-3]
{\color{red}test5 same as 2 different page\footnote{test2}}
\lipsum[1-3]
{\color{red}custom numbered\footnote[5]{custom numbered 5}}
\lipsum[1]
{\color{red} repeat custon numbered same page\footnote{custom numbered 5}}
\lipsum[1-5]
{\color{red} repeat custon numbered other page\footnote{custom numbered 5}}
\lipsum[1-3]
{\color{red}Again second\footnote{test2}}
\lipsum[1]{\color{red}A new one\footnote{test new}}
{\color{red}Another one double\footnote{test new two}\footnote{test new two}}
{\color{red}This has to be 6\footnote{test new three}}
{\color{red}From now 6 will forced to be 8\footnote[8]{test new three}}
\lipsum[1-2]
{\color{red} This footnote has to be 7\footnote{new text}}
\lipsum[1-2]
{\color{red} Repeating 8 to see if last number kept\footnote{test new three}}
\lipsum[1-2]
{\color{red} Custom updating footnote 2\footnote[2]{test new last}}\lipsum[3]
{\color{red} Repeating 2 to see if text "test new last" kept\footnote{test new last}}
\lipsum[3]
{\color{red} And numbers continues from 9\footnote{test new 9}}


\end{document}

The idea is to keep track of the text, the -last- number (footenotemark) and the last page of every footnote.

If you find a footnote with defined text, the old number will be kept except if you renew it by \footnote[<new number>]{old text}. In this last case the given number will be kept for the following same footnotes but the old number will not bee freed.

If you find two same footnotes in the same page, only one will be printed at the footer.

If you define a footnote with custom number \footnote[<custom number>]{some text} then this number will be not available for next footnotes, except if you change it.

Output:

Page 1:

enter image description here

Page 2:

enter image description here

Page 4:

enter image description here

Page 6:

enter image description here

Page 7:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.