Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. –  karlkoeller Jan 25 at 10:27

1 Answer 1

up vote 9 down vote accepted

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} 
share|improve this answer
    
Thanks! It worked perfectly. –  Alex Roberts Jan 25 at 11:01
    
@AlexRoberts You're welcome. Happy to help :-) –  karlkoeller Jan 25 at 11:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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