9

In reference to this question, I would like to ask whether there could be a command that produces the output X when issued in the body of a text, but the output Y when issued in any sort of footnote.

Example:

This is a nice sentence says Mister \funnycommand.\footnote{This is a nice footnote says Mister \funnycommand.}

Output:

This is a nice sentence says Mister X.*1

==============

*1 This is a nice footnote says Mister Y.

  • 2
    Hi! Can I ask what exactly do you need it for? – yo' Dec 16 '13 at 15:50
  • 1
    @tohecz : Of course. I often use my own command \BookTitle to quote booktitles. In the footnote, then, I usually quote the title abbreviated using \BookTitleAbb. I now recognised that I sometimes did not use the abbreviated form in the footnotes and thought that if there could be one single command that determines on its own whether to use abbreviated or not, I could maintain the coherence of my document easier. And since there is, e.g., biblatex 's smartcite-command, which behaves differently depending on whether it is in the main body or in the footnotes, I thought it might be easy. – ClintEastwood Dec 16 '13 at 18:19

2 Answers2

11

Assuming you don't use packages that change footnote management:

\documentclass{article}
%\documentclass{scrartcl} % works also with Koma-Script classes
\usepackage{etoolbox}

\newif\ifinfootnote
\makeatletter
\@ifundefined{scr@saved@footnotetext}
  {\patchcmd{\@footnotetext}}
  {\patchcmd{\scr@saved@footnotetext}}
  {\reset@font}
  {\reset@font\infootnotetrue}
  {}{}
\patchcmd{\@mpfootnotetext}
  {\reset@font}
  {\reset@font\infootnotetrue}
  {}{}
\makeatletter

\newcommand{\funnycommand}{\ifinfootnote Y\else X\fi}

\textheight=4cm % just for the example

\begin{document}
This is a nice sentence says
Mister \funnycommand.\footnote{This is a nice footnote
says Mister \funnycommand.} Again, \funnycommand

\fbox{\begin{minipage}{3cm}
\funnycommand\footnote{\funnycommand}
\end{minipage}}

\end{document}

enter image description here

NOTE Added compatibility with Koma-Script classes

egreg
  • 1,121,712
  • 1
    nice way! Of course, now I have to admit that I indeed use scrbook/scrartcl and hyperref to which your solution does not seem to be compatible. I also use \usepackage[bottom]{footmisc}\raggedbottom and csquotes but they seem to be fine with your solution. Two questions: 1) any way to adjust this to KOMA and hyperref? 2) Any way to use something of the \smartcite-command from biblatex? I am asking because this works well with all packages I am loading or is it too difficult to dig u the code? So that we do not have to reinvent the wheel... – ClintEastwood Dec 16 '13 at 19:04
  • @ClintEastwood Sorry, but a solution to a not clearly stated problem is impossible. I added support for Koma-Script – egreg Dec 16 '13 at 20:49
  • i underestimated the issue. I thought it would be easier and had less implications on other packages. I will have a look at how hyperref redefines footnotes. – ClintEastwood Dec 17 '13 at 04:23
4

A hack ---

\documentclass[10pt]{article}

\let\realfootnote=\footnote
\newif\ifinfootnote\infootnotefalse
\renewcommand{\footnote}[1]{{\infootnotetrue\realfootnote{#1}}} %double grouping!
\newcommand{\funnycommand}{\ifinfootnote Y\else X\fi}
\begin{document}
  This is a nice sentence says 
  Mister \funnycommand.\footnote{This is a nice footnote 
  says Mister \funnycommand.} Again, \funnycommand
\end{document}

you have to modify it to take into account the optional parameter of \footnote if you need it.

Rmano
  • 40,848
  • 3
  • 64
  • 125
  • 2
    +1, just two remarks: Putting \infootnotetrue inside \footnote{...} would prevent the necessity of grouping. And if you group, it's in general preferred to use \begingroup...\endgroup instead of {...} of \bgroup...\egroup. – yo' Dec 16 '13 at 18:56