7

Is there a way to check whether I'm currently in a footnote?

(This question has already been asked on tex.stackexchange.com, but the accepted answer only provides a solution to the user's specific issue but does not answer the question I am now posing.)

I'd like to be able to define a command, which I'll call \mycommand, which behaves one way in footnotes and another way when it appears outside of a footnote. Both cases may occur in the same document. It seems like I could define \mycommand using an \ifthenelse statement, but only if I know a command which will answer the question "am I currently in a footnote."

In other words, what should write for X in the following barebones example?

\newcommand{\mycommand}[1]{\ifthenelse{\boolean{X}{...}{...}}
...
This is a paragraph \mycommand{...}.\footnote{This is a footnote.
Things should look different here \mycommand{...}.}

Or is there a better approach to take?

Alex Roberts
  • 1,379

1 Answers1

10

It can be easily adapted to your case:

\documentclass{article}

\newif\iffoot
\footfalse

\let\origfootnote\footnote
\renewcommand{\footnote}[1]{\foottrue\origfootnote{#1}\footfalse}

\newcommand{\mycommand}[1]{%
  \iffoot%
    (#1 is inside a footnote)%  here put what the command has to do when inside
  \else%
    (#1 is outside a footnote)%  here put what the command has to do when outside
  \fi%
}

\begin{document}
This is a paragraph \mycommand{hello}.\footnote{This is a footnote.
Things should look different here \mycommand{hello}.}
\end{document} 

enter image description here

If you prefer a solution with \ifthenelse and \boolean here it is:

\documentclass{article}
\usepackage{ifthen}

\newboolean{X}
\setboolean{X}{false}

\let\origfootnote\footnote
\renewcommand{\footnote}[1]{\setboolean{X}{true}\origfootnote{#1}\setboolean{X}{false}}

\newcommand{\mycommand}[1]{%
  \ifthenelse{\boolean{X}}%
  {(#1 is inside a footnote)}%  here put what the command has to do when inside
  {(#1 is outside a footnote)}%  here put what the command has to do when outside
  }

\begin{document}
This is a paragraph \mycommand{hello}.\footnote{This is a footnote.
Things should look different here \mycommand{hello}.}
\end{document} 
karlkoeller
  • 124,410