使 \footnote 在 leftbar 环境中工作

使 \footnote 在 leftbar 环境中工作

抱歉,又是我。Gonzalo 向我介绍了 framed 包中非常好的 leftbar 环境,我现在用它来处理定义、定理和类似的环境。现在我遇到了一个新问题:leftbar 环境内的脚注 (\footnote) 未显示!标签在那里,但没有带有相应编号的脚注。

根据这里和类似来源,可以使用 \footnotemark 和 \footnotetext 来修复此问题。问题是:我正在使用 SWP,并希望使用 \footnote 命令来添加脚注。我想到了类似这样的方法:

\renewcommand{\footnote}[1]{\footnotemark \footnotetext{#1}}

但是 \footnotemark 和 \footnotetext 需要标签。所以我最想要的是类似

\renewcommand{\footnote}[1]{\footnotemark[some arbitrary string "x" that cannot collide with an "x" from a different footnotemark] \footnotetext["x"]{#1}}

我尝试将 #1 本身用作“x”,但这会产生错误(这并不奇怪,因为它是未经净化的 LaTeX)。这里有技巧吗,还是我的语法错误?

编辑:我的更新命令无论如何都很愚蠢。诀窍是让 \footnotetext 出现结束后leftbar 环境。有没有办法通过 renewcommand 来获取它?

答案1

稍长的代码,允许您仅键入\footnote。这个想法是让每个脚注附加\footnotetext{#1}到一个标记列表。然后将此标记列表排版到正确的位置。需要一些技巧才能正确嵌套以及hyperref支持。通常,这可以用于其他(非逐字)环境。

代码中的注释。

\documentclass{article}
\usepackage{etoolbox} % for "\pretocmd".
\makeatletter
%
% User command, see the end of the preamble for use.
% We redefine the environment by adding "\savefoot@begin" at 
% the beginning, and "\savefoot@end" at the end.
%
\newcommand{\DeclareSavefootEnvironment}[1]{%
  \expandafter\pretocmd\csname #1\endcsname
              {\savefoot@begin}{}{\savefoot@patch@error{#1}}%
  \expandafter\pretocmd\csname end#1\endcsname
              {\savefoot@end}{}{\savefoot@patch@error{#1}}%
}
\newcommand{\savefoot@patch@error}[1]{%
  \PackageError{savefoot}{etoolbox failed to patch the environment #1, sorry}{}}
%
% Register allocation.
%
\newcount\c@savefoot@depth
\newtoks\savefoot@toks
%
% "\savefoot@begin" starts an environment which saves footnotes.
% We only want to output footnotes outside any such environment,
% so we count how deep we are in this kind of environment using
% the count "\c@savefoot@depth", which pretends to be a LaTeX
% counter. 
%
% We will store all the "\footnotetext" into the toks 
% "\savefoot@toks". This is initialized _locally_ at the start
% of each "savefoot" environment.
% 
\newcommand{\savefoot@begin}{%
  \advance\c@savefoot@depth by 1\relax  
  \savefoot@toks={}}
%
% When ending a savefoot environment, we check if it was the 
% outermost one. In that case, "\@secondoftwo", we output the
% "\footnotetext" that we saved, after the end of the group.
% The "\endgroup" must be explicitly there; this is the case
% because of how "\end{...}" works in LaTeX2e.
% 
% Since the register "\savefoot@toks" is restored at the end 
% of the group, we need to expand it inside the group, then 
% send "\savefoot@append{<expanded \savefoot@toks>}" after the
% "\endgroup", hence the "\expandafter"s. Same thing when we 
% need to typeset.
% 
\newcommand{\savefoot@end}{%
  \ifnum\c@savefoot@depth>\@ne
  \expandafter\@firstoftwo
  \else
  \expandafter\@secondoftwo
  \fi
  {% Nested, don't output, save to the level above.
    \expandafter\do@after@explicit@endgroup\expandafter{%
      \expandafter\savefoot@append\expandafter{\the\savefoot@toks}}%
  }%
  {% Outside, output.
    \expandafter\do@after@explicit@endgroup\expandafter{%
      \the\savefoot@toks}%
  }%
}
%
% Appending to a toks is standard.
%
\newcommand{\savefoot@append}[1]{%
  \savefoot@toks\expandafter{\the\savefoot@toks#1}}%
%
% To put after "\endgroup", we need delimited parameters,
% hence the "\def". We first check that the command is new,
% just in case.
% 
\newcommand{\do@after@explicit@endgroup}{}
\def\do@after@explicit@endgroup#1#2\endgroup{#2\endgroup#1}
%
% Now the footnote command. We save the old definition, 
% used in the case where we are outside any savefoot
% environment. If we are inside a savefoot environment,
% place the "\footnotemark", and append 
% 
%    \savefoot@setcounter{<explicit number>}\footnotetext{<text>}
% 
% to our register. This will be typeset at the end. We cannot
% just do "\footnotetext[<explicit number>]{<text>}", because
% the hyperref package does not like that.
% 
\let\savefoot@old@footnote\footnote
\renewcommand{\footnote}[1]{%
  \ifnum\c@savefoot@depth>\z@
  \expandafter\@firstoftwo
  \else
  \expandafter\@secondoftwo
  \fi
  {% Inside a leftbar, save the text in the relevant toks.
    \footnotemark
    \savefoot@toks=\expandafter{%
      \the\expandafter\savefoot@toks
      \expandafter\savefoot@setcounter\expandafter{\the\c@footnote}%
      \footnotetext{#1}%
    }%
  }%
  {% Don't save, just typeset.
    \savefoot@old@footnote{#1}%
  }%
}
%
% When not using hyperref, just give the counter "footnote" 
% the right value (in that case, "\savefoot@hhyperref@support{#1}"
% will be defined to do nothing).
% 
\newcommand{\savefoot@setcounter}[1]{%
  \setcounter{footnote}{#1}%
  \savefoot@hyperref@support{#1}%
}
% 
% Getting "\footnotetext" and "\footnotemark" to work with hyperref
% is non-trivial, and I probably did something wrong. But at first
% sight it seems ok.
%
\newcommand{\savefoot@hyperref@support}[1]{%
  \setcounter{Hfootnote}{#1}%
  \global \let \Hy@saved@currentHlabel \@currentHlabel 
  \global \let \Hy@saved@currentHref \@currentHref 
  \hyper@makecurrent {Hfootnote}%
  \global \let \Hy@footnote@currentHlabel \@currentHlabel 
  \global \let \Hy@footnote@currentHref \@currentHref 
  \global \let \@currentHlabel \Hy@saved@currentHlabel 
  \global \let \@currentHref \Hy@saved@currentHref
}
%
% If hyperref is not loaded, we disable the hyperref support.
% We check at the "\begin{document}".
%   
\AtBeginDocument{%
  \@ifpackageloaded{hyperref}{}{\let\savefoot@hyperref@support\@gobble}%
}
\makeatother


% ============= Example =====================

\usepackage{framed}
\usepackage{hyperref}
\DeclareSavefootEnvironment{leftbar}

\begin{document}

Some text,\footnote{With a normal footnote.} in which 
footnotes are preserved.\footnote{Even when you put several 
  in a row.}\footnote{Like this.}

\begin{leftbar}
  Also true for the leftbar environment.\footnote{The relevant 
    commands are placed after the end of the environment.} 
  \begin{leftbar}
    And we check that nesting works.\footnote{That was the tricky part\ldots}
  \end{leftbar}
  Maybe it breaks after nesting?\footnote{Let's see.}
\end{leftbar} 

More footnotes.\footnote{Like this.}\footnote{And that.}

\begin{leftbar}
  And another leftbar environment to be sure.\footnote{Does it 
    work? I'll see soon, when I compile.}
\end{leftbar}

\end{document}

答案2

您可以使用曼尼福特包;一个小例子:

\documentclass{article}
\usepackage{manyfoot}
\usepackage{framed}

\DeclareNewFootnote{B}
\begin{document}
text text\footnote{a standard footnote.}
\begin{leftbar}
text text\FootnoteB{*}{text}
\end{leftbar} 
\FootnotetextB{*}{a footnote with a manual mark.}
\end{document}

包文档解释了包提供给您的自定义脚注的所有选项。

相关内容