![脚注布尔值:如何检查当前是否在脚注中](https://linux22.com/image/283107/%E8%84%9A%E6%B3%A8%E5%B8%83%E5%B0%94%E5%80%BC%EF%BC%9A%E5%A6%82%E4%BD%95%E6%A3%80%E6%9F%A5%E5%BD%93%E5%89%8D%E6%98%AF%E5%90%A6%E5%9C%A8%E8%84%9A%E6%B3%A8%E4%B8%AD.png)
有没有办法检查我当前是否在脚注中?
(这个问题已经问在 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}