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?

  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – karlkoeller Jan 25 '14 at 10:27
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} 
  • @AlexRoberts You're welcome. Happy to help :-) – karlkoeller Jan 25 '14 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.