0

I am currently trying to redefine some aspect of the footnote, footnotemark and footnotetext command to allow the use of these command in diverses situation (like in float) with things like hyperlink and backref. And it now work well in a large set of situation.

But, i am stuck at redefining a specific command. It seems like a problem of expansion in the command.

\documentclass[a4paper,12pt,oneside,final, DIV=12, listof=totoc, bibliography=totoc, toc=bibliography, open=right, chapterprefix=true]{scrbook}
\usepackage{lmodern} 
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pdfpages}
\usepackage{xspace}
\usepackage[final]{hyperref}
\usepackage{footnotehyper}
\usepackage{footnotebackref}
\usepackage{tablefootnote}
\usepackage{bookmark}
\usepackage{perpage} 
\MakePerPage{footnote} 

%
% Lot of other package not usefull
%

%%%%%%%% redefinition of "footnote" to allow cool backref (work well) %%%%%%
\let\oldfootnote\footnote

%a new counter to create an unambiguous label-tag
\newcounter{myHyperFootnoteCounter}

\makeatletter

% branch between the footnote with/without opt. argument
\renewcommand{\footnote}{\@ifnextchar[\my@OptHyperFootnote\my@HyperFootnote}

%define an new footnote without optional argument
\newcommand{\my@HyperFootnote}[1]{%
    \refstepcounter{myHyperFootnoteCounter}%
    \def\myFootnoteTag{hfn:\themyHyperFootnoteCounter}%
    \label{\myFootnoteTag}%
    \oldfootnote{\hyperref[\myFootnoteTag]{$\uparrow$}~#1}%
}

%define an new footnote with optional argument
\newcommand{\my@OptHyperFootnote}[2][plop]{%
    \refstepcounter{myHyperFootnoteCounter}%
    \def\myFootnoteTag{hfn:\themyHyperFootnoteCounter}%
    \label{\myFootnoteTag}%
    % put the optional argument to the original `footnote`
    \oldfootnote[#1]{\hyperref[\myFootnoteTag]{$\uparrow$}~#2}% 
}

\makeatother


%%%%%%%%%%%%redefinition of myHyperFootnoteCounterMARK : work partially %%%%%%
\newcounter{myHyperFootnoteCounterMARK}
\let\oldfootnotetext\footnotetext
\let\oldfootnotemark\footnotemark
%
\makeatletter
\renewcommand{\footnotetext}{\@ifnextchar[\my@OptHyperFootnotetext\my@HyperFootnotetext}
%
\newcommand{\my@HyperFootnotetext}[1]{%
\def\myFootnoteTagtext{hfu:\themyHyperFootnoteCounterMARK}%
\oldfootnotetext{\hyperref[\myFootnoteTagtext]{$\uparrow$}~#1}
}
\newcommand{\my@OptHyperFootnotetext}[2][plop]{%
\def\myFootnoteTagtextARGU{hfoi:#1}%
\def\mysave{saved@Href@A}% In theory : saved@Href@#1
\let\Hy@footnote@currentHref\mysave
\oldfootnotetext{\hyperref[\myFootnoteTagtextARGU]{$\uparrow$}~#2}
}
%
\renewcommand{\footnotemark}{\@ifnextchar[\my@OptHyperFootnotemark\my@HyperFootnotemark}
%
\newcommand{\my@HyperFootnotemark}{%
\refstepcounter{myHyperFootnoteCounterMARK}%
\def\myFootnoteTagtext{hfu:\themyHyperFootnoteCounterMARK}%
\label{\myFootnoteTagtext}%
\oldfootnotemark%
}
\newcommand{\my@OptHyperFootnotemark}[1][plop]{%
\refstepcounter{myHyperFootnoteCounterMARK}
\def\myFootnoteTagtextargu{hfoi:#1}
\def\mysave{saved@Href@A}% In theory : saved@Href@#1
\label{\myFootnoteTagtextargu}%
\oldfootnotemark \global\let\mysave\Hy@footnote@currentHref%
}
%
\makeatother

The error is in the end of the command. I use an Hyperref tricks by redefining manually \global\let\mysave\Hy@footnote@currentHref after footnotemark and \let\Hy@footnote@currentHref\mysave before footnotetext. It allow the use of footnotemark and footnotetext out of order by redefining on which footnotetext point footnotemark without using the [] variant that are not supported by hypperef.

The tricks work well in text, but i would like to generalise it. So i have redefined the [] variant of footnotemark and footnotetext to use the classic version of footnotemark and footnotetext (i have no use of the [] variant) but to use the optionnal argument as an anchor for hyperref, which allow me to define manually label of hyppereference with footnotemark and footnotetext.

The command work well if i write inside the command an ad-hoc argument to test it (like \global\let\saved@Href@AHy@footnote@currentHref) (obviously, they don't point on a good target but it works) but when i tried to generalise that with :

\def\mysave{saved@Href@#1}%
\oldfootnotemark \global\let\mysave\Hy@footnote@currentHref%

it throw multiple error when i try to use the [] variant of footnotemark and footnotetext. (Undifined control sequence).

I have tested the replacement by a simple anchor like :

\def\mysave{saved@Href@A}%
\oldfootnotemark \global\let\mysave\Hy@footnote@currentHref%

And it doesn't work neither. So i guessed it was an axpansion priority problem and i tried the classical :

\def\mysave{saved@Href@A}%
\oldfootnotemark% 
\expandafter\global\expandafter\let\mysave\Hy@footnote@currentHref%

But it surprinsigly doesn't work ... and throw multiple " missing control sequence inserted " . I have looked on several solution through the web that look like the one i have used so i don't understand why it doesn't work.

Any help will be appreciated ;)

6
  • Welcome to TeX.SX! As you defined, \mysave is just a bunch of characters, not a control sequence, so you can't \let it to anything. Try \global\expandafter\let\csname\mysave\endcsname\Hy@footnote@currentHref Commented Mar 18, 2020 at 21:00
  • Hi and thanks for your answer. It's true, \mysave is just a bunch of character, but saved@Href@A is also a bunch of character so why " \global\let\saved@Href@A\Hy@footnote@currentHref " works perfectly when i write it like that ?? You can see an example of the tricks on the manual of hyperref for example (mirrors.ircam.fr/pub/CTAN/macros/latex/contrib/hyperref/doc/…) page 51 Commented Mar 18, 2020 at 22:56
  • 1
    To our eyes, yes, (1) saved@Href@A and (2) \saved@Href@A are both a bunch of characters. However to TeX's eyes, the \ in front of example 2 makes it be one single (so-called) token, so for TeX example 1 is a group of 12 character tokens, while the second example is one single control sequence token, and that makes all the difference. The syntax of \let is \let<token>=<token> (the = is optional), so you need two tokens, and your \mysave contains several. Then \csname<characters>\endcsname turns <characters> into \<characters>, which is what you need here. Commented Mar 18, 2020 at 23:07
  • Ok thanks ! It works ;) Commented Mar 19, 2020 at 2:31
  • Good to know! Would you please post what you did to fix your code as an answer, so future users can benefit from it, please? Commented Mar 19, 2020 at 3:17

1 Answer 1

1

Based on the comment of Phelype Oleinik, heres the code that work :


%%%%%%%% redefinition of "footnote" to allow cool backref (work well) %%%%%%
\let\oldfootnote\footnote

%a new counter to create an unambiguous label-tag
\newcounter{myHyperFootnoteCounter}

\makeatletter

% branch between the footnote with/without opt. argument
\renewcommand{\footnote}{\@ifnextchar[\my@OptHyperFootnote\my@HyperFootnote}

%define an new footnote without optional argument
\newcommand{\my@HyperFootnote}[1]{%
    \refstepcounter{myHyperFootnoteCounter}%
    \def\myFootnoteTag{hfn:\themyHyperFootnoteCounter}%
    \label{\myFootnoteTag}%
    \oldfootnote{\hyperref[\myFootnoteTag]{$\uparrow$}~#1}%
}

%define an new footnote with optional argument
\newcommand{\my@OptHyperFootnote}[2][plop]{%
    \refstepcounter{myHyperFootnoteCounter}%
    \def\myFootnoteTag{hfn:\themyHyperFootnoteCounter}%
    \label{\myFootnoteTag}%
    % put the optional argument to the original `footnote`
    \oldfootnote[#1]{\hyperref[\myFootnoteTag]{$\uparrow$}~#2}% 
}

\makeatother


%%%%%%%%%%%%redefinition of myHyperFootnoteCounterMARK : work partially %%%%%%
\newcounter{myHyperFootnoteCounterMARK}
\let\oldfootnotetext\footnotetext
\let\oldfootnotemark\footnotemark
%
\makeatletter
\renewcommand{\footnotetext}{\@ifnextchar[\my@OptHyperFootnotetext\my@HyperFootnotetext}
%
\newcommand{\my@HyperFootnotetext}[1]{%
\def\myFootnoteTagtext{hfu:\themyHyperFootnoteCounterMARK}%
\oldfootnotetext{\hyperref[\myFootnoteTagtext]{$\uparrow$}~#1}
}
\newcommand{\my@OptHyperFootnotetext}[2][plop]{%
\def\myFootnoteTagtextARGU{hfoi:#1}%
\def\mysavee{saved@Href@#1}% 
\expandafter\let\expandafter\Hy@footnote@currentHref\csname\mysavee\endcsname%
\oldfootnotetext{\hyperref[\myFootnoteTagtextARGU]{$\uparrow$}~#2}
}
%
\renewcommand{\footnotemark}{\@ifnextchar[\my@OptHyperFootnotemark\my@HyperFootnotemark}
%
\newcommand{\my@HyperFootnotemark}{%
\refstepcounter{myHyperFootnoteCounterMARK}%
\def\myFootnoteTagtext{hfu:\themyHyperFootnoteCounterMARK}%
\label{\myFootnoteTagtext}%
\oldfootnotemark%
}
\newcommand{\my@OptHyperFootnotemark}[1][plop]{%
\refstepcounter{myHyperFootnoteCounterMARK}
\def\myFootnoteTagtextargu{hfoi:#1}
\def\mysave{saved@Href:#1}%
%
\label{\myFootnoteTagtextargu}%
\oldfootnotemark \global\expandafter\let\csname\mysave\endcsname\Hy@footnote@currentHref%
}
%
\makeatother

with "\expandafter\let\expandafter\Hy@footnote@currentHref\csname\mysavee\endcsname%" for footnotetext and "\oldfootnotemark \global\expandafter\let\csname\mysave\endcsname" for footnotemark.

The code works (backreference and reference works, even in a figure) but is still dependant of some "stepcounter" in text, i am trying to automatise that and will return when the full solution will work ;)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .