dbosk / didactic

Pedagogical extensions for Beamer
LaTeX Project Public License v1.3c
2 stars 0 forks source link

Fix citation so that it reuses footnotes #19

Open dbosk opened 3 months ago

dbosk commented 3 months ago

customcite.pdf

\documentclass{article}
\usepackage{minted}
\usepackage{xparse}
\usepackage{ifoddpage}
\usepackage[style=verbose,
            citestyle=verbose,
            singletitle=false,
            maxbibnames=99,
            isbn=false,doi=false,url=false,
            autocite=footnote]{biblatex}

\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@book{companion,
  author = {Goossens, Michel and Mittelbach, Frank and Samarin, Alexander},
  title = {The LaTeX Companion},
  edition = {1},
  publisher = {Addison-Wesley},
  location = {Reading, Mass.},
  date = {1994},
}
@book{taocp,
  author = {Knuth, Donald E.},
  title = {The Art of Computer Programming},
  volumes = {3},
  publisher = {Addison-Wesley},
  location = {Reading, Mass.},
  date = {1997},
}
@book{texbook,
  author = {Knuth, Donald E.},
  title = {The \TeX book},
  publisher = {Addison-Wesley},
  location = {Reading, Mass.},
  date = {1984},
}
@article{ErrorsOfTeX,
  author = {Knuth, Donald E.},
  title = {Errors of \TeX},
  journal = {Software---Practice and Experience},
  volume = {19},
  number = {7},
  date = {1989},
  pages = {607--685},
}
\end{filecontents}
\addbibresource{\jobname.bib}

\makeatletter
\def\@defkeyasused#1{%
  \typeout{defkeyasused set \thekey{} to page \arabic{page} note \thefootnote}%
  \expandafter\xdef\csname repeatcite@#1\endcsname{\thefootnote}%
  \expandafter\xdef\csname citepage@#1\endcsname{\arabic{page}}%
}%
\def\@resetkey#1{%
  \typeout{resetkey reset #1:
    \arabic{page}
    and \oddpage@page{}
    and \csname citepage@#1\endcsname}%
  \global\expandafter\let\csname repeatcite@#1\endcsname=\relax%
}
\def\@resetkeyifnewpage#1{%
  \@ifundefined{citepage@#1}
    {\ifciteseen
      {\@resetkey{#1}}
      {\relax}}%
    {\expandafter\ifnum\csname citepage@#1\endcsname<\oddpage@page\relax
      \@resetkey{#1}%
     \fi}%
}
\def\footnoteormark#1{%
  \edef\thekey{\thefield{entrykey}}%
  \typeout{footnoteormark \thekey}%
  \@resetkeyifnewpage{\thekey}%
  \@ifundefined{repeatcite@\thekey}%
    {\mkbibfootnote{#1}\@defkeyasused{\thekey}}%
    {\footnotemark[\csname repeatcite@\thekey\endcsname]%
      \typeout{footnoteormark reused \thekey}}%
}
\def\ifnonotes#1#2{%
  \ifboolexpr{test {\iffieldundef{prenote}} and test {\iffieldundef{postnote}}}
    {#1}
    {#2}%
}
\def\noisymkbibfootnote#1{%
  \typeout{noisymkbibfootnote}%
  \mkbibfootnote{#1}%
}
\let\@mkbibfootnoteifnotes=\noisymkbibfootnote
\NewDocumentCommand\mkbibfootnoteifnotes{m}{%
  \typeout{mkbibfootnoteifnotes \meaning\@mkbibfootnoteifnotes}%
  \@mkbibfootnoteifnotes{#1}%
}
%\DeclareCiteCommand{\footcite}%
%  [\expandafter\mkbibfootnoteifnotes]
%  {\ifnonotes
%    {\global\let\@mkbibfootnoteifnotes=\relax\typeout{set relax}}
%    {\global\let\@mkbibfootnoteifnotes=\noisymkbibfootnote\typeout{set footnote}}%
%   \usebibmacro{prenote}}
%  {\usebibmacro{citeindex}%
%   \typeout{footcite \thefield{entrykey}}%
%   \ifnonotes
%    {\footnoteormark{\usebibmacro{cite}}}
%    {\usebibmacro{cite}}}
%  {\ifnonotes
%    {\textsuperscript{,}}
%    {\multicitedelim}}
%  {\usebibmacro{postnote}}
\DeclareCiteCommand{\footcite}%
  [\unspace]
  {\relax}
  {\usebibmacro{citeindex}%
    \typeout{footcite \thefield{entrykey}}%
    \ifnonotes{\footnoteormark}{\mkbibfootnote}
      {\usebibmacro{prenote}\usebibmacro{cite}\usebibmacro{postnote}}}
  {\textsuperscript{,}}
  {\relax}
\DeclareMultiCiteCommand{\footcites}{\footcite}{\textsuperscript{,}}
\makeatother

\DeclareAutoCiteCommand{footnote}[f]{\footcite}{\footcites}

\begin{document}
\Textcite{texbook} wrote \citetitle{texbook} early on.
\Textcite{companion} wrote \citetitle{companion} later.
The algorithms are efficient \autocite{taocp}.
One should be careful when analysing one's algorithms \autocite{taocp}.

\Textcite{texbook,taocp} has in fact written both \citetitle{texbook} and 
\citetitle{taocp}.

\clearpage

\Textcite{texbook} wrote \citetitle{texbook} early on.
\Textcite{companion} wrote \citetitle{companion} later.
The algorithms are efficient \autocite{taocp}.
One should be careful when analysing one's algorithms \autocite{taocp}.

\Textcite{texbook,taocp}
has written many books, and he has in fact written both \citetitle{texbook} and 
\citetitle{taocp}.
But still, one must be careful when programming \TeX{} \autocite{texbook}.

And again, \textcite[See for example][]{texbook,taocp}
has written many books, and he has in fact written both \citetitle{texbook} and 
\citetitle{taocp}.
But still, one must be careful when programming \TeX{} \autocite{texbook}.

If we want to refer to several, we can use that version of the 
command\autocites{texbook}{taocp}.
Or even with notes\autocites[See for example][]{texbook}[Or see][but this is 
not a light read]{taocp}.

However, if we want both refs in the same footnote to discuss them together, 
then we need to use a proper footnote.\footnote{%
  See for instance \cite{texbook} or \cite{taocp}.
  But if we want to say that programming is an art, which it is 
  \autocite{taocp}, then we use the normal citation command.
  However, we can use additional sources to support that claim \autocite[such 
  as][]{ErrorsOfTeX}.
  But if we refer to the same source again \autocite{ErrorsOfTeX}, we see that 
  it uses a shorter version, but still inline in parentheses.
  But if we use the previous one \autocite[but now with a note]{taocp}, we see 
  that it doesn't reuse the footnote but rather inline with parentheses.
}

\clearpage
The code that generated this document follows.
\inputminted{latex}{\jobname.tex}
\end{document}