从 koleygr 重新定义 \footnote:\footnote 中的 \url 命令出错。没有它也可以工作

从 koleygr 重新定义 \footnote:\footnote 中的 \url 命令出错。没有它也可以工作

我非常喜欢使用这个代码

脚注:对相同的脚注文本保留相同的脚注编号(在所有文档中)

科莱格尔

但它不起作用 是否有可能使用该命令?\footnote{\url{[email protected]}}}\url

\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}

答案1

使用(原始问题中的)原始定义\footnote,您需要在脚注里面的宏\protect\url

\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}

在此处输入图片描述

对于 OP 的编辑问题,包括重新定义的\footnote,使用\noexpand,而不是\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}

相关内容