latex3 / latex2e

The LaTeX2e kernel
https://www.latex-project.org/
LaTeX Project Public License v1.3c
1.88k stars 262 forks source link

Provide a way to test whether a stuff is inside a footnote or not #789

Open dbitouze opened 2 years ago

dbitouze commented 2 years ago

Brief outline of the enhancement

Something I'm often missing is a way to test whether a stuff is currently in a footnote or not.

A typical use case is for acronyms which, thanks to styles, can be automatically displayed at their first occurrences as ⟨short⟩\footnote{⟨long⟩}. For the first occurrences that happen to appear:

This is illustrated by the following examples.

Minimal example showing the current behaviour

% \RequirePackage{latexbug}       % Commented on purpose
\documentclass{article}
\usepackage[shortcuts=ac]{glossaries-extra}
\setabbreviationstyle[acronym]{footnote}
\newacronym{tug}{TUG}{\TeX\ User Group}

\begin{document}
The following acronym, used for the first time, is displayed as
\verb|TUG\footnote{\TeX\ User Group}|: Did you subscribe to a \gls{tug}? So far,
so good.

Resetting the "first" times with \verb|\glsresetall|.
\glsresetall

The following acronym, used for the ``first'' time, is displayed inside
a footnote as \verb|TUG\footnote{\TeX\ User Group}|%
\footnote{Did you subscribe to a \gls{tug}?}\footnote{Bar.}. It makes a mess...
\end{document}

Minimal example showing the desired new behaviour

% \RequirePackage{latexbug}       % Commented on purpose
\documentclass{article}
\usepackage[shortcuts=ac]{glossaries-extra}
\setabbreviationstyle[acronym]{footnote}
\newacronym{tug}{TUG}{\TeX\ User Group}

% Ugly code that test whether we are inside a footnote or not.
\makeatletter
\newif\if@inside@footnote@

\let\@ori@footnote\footnote%
\renewcommand{\footnote}[1]{%
  \@inside@footnote@true%
  \@ori@footnote{#1}%
  \@inside@footnote@false%
}%

% Personal glossaries command that displays the acronym differently depending
% whether it is used inside a footnote or not.
\newcommand*\mygls[1]{%
  \if@inside@footnote@
  \acs{#1} (\acl{#1})%
  \else
  \gls{#1}%
  \fi
}%
\makeatother

\begin{document}
The following acronym, used for the first time, is displayed as
\verb|TUG\footnote{\TeX\ User Group}|: Did you subscribe to a \mygls{tug}? So far, so good.

Resetting the "first" times with \verb|\glsresetall|.
\glsresetall

The following acronym, used for the ``first'' time, is displayed inside
a footnote as \verb|TUG\footnote{\TeX\ User Group}|%
\footnote{Did you subscribe to a \mygls{tug}?}\footnote{Bar.}. It \emph{doesn't}
makes a mess...
\end{document}
stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity.

FrankMittelbach commented 2 years ago

I think the suggestion that commands and environments can query the context in which they are used is a good one, but the "context" is far wider than "am I in a footnote?". For this to make sense there should be a general mechanism to define context and also a general one to query it. That is something we have on our radar and it will eventually come. In short, I'm not that keen on introducing individual commands and tests for something like footnotes, because the same is relevant for many other places and such adhoc solutions would then conflict with a general mechanism and and would need to remain supported.

FrankMittelbach commented 2 years ago

With some restrictions you can have a solution already now (as long as your footnotes do contain stuff that does a \refstepcounter, after all within a footnote the \@currentcounter is footnote:

\makeatletter
\newcommand*\mygls[1]{%
  \def\@tempa{footnote}%
  \ifx\@tempa\@currentcounter
    \acs{#1} (\acl{#1})%
  \else
    \gls{#1}%
  \fi
}%
\makeatother