基于@moewe 提供的有益建议这里和这里,我想寻求帮助,以便获得更复杂的脚注引用命令,让用户也能包含页码。长话短说,我正在帮助一位同事完成一个使用 natbib 的旧项目。
最小工作示例如下,
\RequirePackage{filecontents}
\begin{filecontents}{\jobname.bib}
@book{A01,
author = {Author Name},
publisher = {Publisher Co.},
title = {Title of the Book},
year = {2022}}
\end{filecontents}
\listfiles
\documentclass{article}
\usepackage{natbib}
\begin{document}
\NewDocumentCommand{\footcite}{om}{\footnote{\IfNoValueTF{#1}{\cite{#2}}{\cite[#1]{#2}}}}
Test function of footnote citation without page number.\footcite{A01}
Test function of footnote citation with page number.\footcite[2]{A01}
\bibliographystyle{plainnat}
\bibliography{\jobname}
\end{document}
不幸的是,该命令\NewDocumentCommand{\footcite}{om}{\footnote{\IfNoValueTF{#1}{\cite{#2}}{\cite[#1]{#2}}}}
导致未定义的控制序列错误。
非常感谢任何指导或建议!
答案1
经过一番尝试,我终于搞明白了!请参阅现在具有所需输出的功能性 MWE。另请注意,我添加了一些样式选项。我希望这对有类似需求的未来用户有所帮助!
\RequirePackage{filecontents}
\begin{filecontents}{\jobname.bib}
@book{A01,
author = {Author Name},
publisher = {Publisher Co.},
title = {Title of the Book},
year = {2022}}
\end{filecontents}
\listfiles
\documentclass{article}
\usepackage{natbib}
\setcitestyle{authoryear,open={(},close={)}} %Citation-related command, replaces default brackets to parentheses
\setcitestyle{aysep={}} %Citation-related command, removes default comma between author & year
\usepackage{xparse}
\NewDocumentCommand\footcite{o m}
{%
\footnote{ %
\IfNoValueTF {#1}%
{\citet{#2}}%
{\citet[#1]{#2}}%
}
}
\begin{document}
Test function of footnote citation without page number.\footcite{A01}
Test function of footnote citation with page number.\footcite[2]{A01}
\bibliographystyle{plainnat}
\bibliography{\jobname}
\end{document}