1

I use with very big love this code

Footnotes: Keep same footnote number for same footnotetext (during all document)

from koleygr.

But it works not with \footnote{\url{info@test.com}}} Is there a possibility to use the \url command?

\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.com}}} % Does work
%{\color{red}test1 \footnote{\protect\url{test.com}}} % Does not work
\lipsum[1]
{\color{red}test2 same as 1\footnote{          {test.com}}}  % Does work
%{\color{red}test2 same as 1\footnote{\protect\url{test.com}}}  % Does not work
\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}
  • Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – samcarter_is_at_topanswers.xyz Jan 16 '18 at 17:12
  • The example is the same as in the link. Only the line with the error is added. – user152720 Jan 16 '18 at 17:15
3

With the original definition of \footnote (in the original question), you need to \protect the \url macro inside of a footnote.

\documentclass{article}
\usepackage{lipsum,url,xcolor}
\begin{document}
\lipsum[1]
{\color{red}test1\footnote{\protect\url{test.com}}}  % \url command makes error. Withotu this line it works
\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}

enter image description here

For the edited question of the OP, including a redefined \footnote, use \noexpand, instead of \protect:

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

\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.com}}} % Does work
{\color{red}test1 \footnote{\noexpand\url{xtest.com}}} % Does not work
\lipsum[1]
%{\color{red}test2 same as 1\footnote{          {test.com}}}  % Does work
{\color{red}test2 same as 1\footnote{\noexpand\url{test.com}}}  % Does not work
\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}
  • @user152720 I provided my answer, apparently, before you specified a redefinition of \footnote. I will have a further look. – Steven B. Segletes Jan 16 '18 at 18:08
  • @user152720 See my edit. With the modified \footnote definition, using \noexpand instead of \protect fixes it. – Steven B. Segletes Jan 16 '18 at 18:18
  • @user152720 If this answer completely and sufficiently addresses your question, you may consider clicking the check mark to the left of the answer to indicate to other readers that you "accept" the answer as the best (if there were several answers provided). – Steven B. Segletes Jan 16 '18 at 18:38
  • @Steven... No other anawers here.... So, no accept needed... :P... kidding... +1 for the fix. – koleygr Jan 16 '18 at 20:58

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.