有没有办法检查我当前是否在脚注中?
(这个问题已经问在 tex.stackexchange.com 上,但接受的答案仅提供了针对用户特定问题的解决方案,但没有回答我现在提出的问题。)
我希望能够定义一个命令,我将称之为\mycommand
,它在脚注中的行为是一种方式,当它出现在脚注之外时则以另一种方式运行。这两种情况可能出现在同一个文档中。似乎我可以\mycommand
使用\ifthenelse
语句进行定义,但前提是我知道一个命令可以回答“我目前是否在脚注中”这个问题。
换句话说,X
在下面的基本示例中应该写什么?
\newcommand{\mycommand}[1]{\ifthenelse{\boolean{X}{...}{...}}
...
This is a paragraph \mycommand{...}.\footnote{This is a footnote.
Things should look different here \mycommand{...}.}
或者有更好的方法可以采取?
答案1
它可以轻松适应您的情况:
\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}
\ifthenelse
如果您更喜欢使用和的解决方案,\boolean
那么它是:
\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}