将 \citet{CITEKEY} 全局更改为 {\renewcommand\&{and}\citet{CITEKEY}} ?

将 \citet{CITEKEY} 全局更改为 {\renewcommand\&{and}\citet{CITEKEY}} ?

正如标题所述,我想\citet{CITEKEY}在整个文档中使用,但在序言中将其全局包装为\renewcommand\&{and}\citet{CITEKEY}}。这样,我就可以打开/关闭文本引用中的“和”。

谢谢。

以下是 MWE:

\begin{filecontents*}{\jobname.bib}
@article{key,
author = {Authorone, One and Authortwo, Two},
title = {{Title goes here.}},
journal = {Journal of MWE},
year = {1991},
volume = {11},
number = {22},
pages = {303--340},
\end{filecontents*}

%========  ========%

\documentclass[authoryear,review,11pt]{elsarticle}
\usepackage[colorlinks=true]{hyperref}
\usepackage{verbatim}

\begin{document}
\section*{In text citations}

In text command \verb|\citet{key}| prints \citet{key}. Note the ``\&''.

This command \verb|{\renewcommand\&{and}\citet{key}}| prints {\renewcommand\&{and}\citet{key}}. Note the ``and''.

I want to define \verb|\citet| as \verb|{\renewcommand\&{and}\citet{key}}| globally, maybe in the preamble, so I don't have to write \verb|{\renewcommand\&{and}\citet{key}}| in the text in order to get ``and''.

\section*{References}
\bibliographystyle{apalike2}
\bibliography{\jobname}
\end{document}

答案1

这是一个基于 LuaLaTeX 的解决方案。代码处理 、 和 指令的“无星号”和“有星号”变体\cite\citet\citep在方括号中使用零个、一个或两个可选参数。

代码实际上并不修改\cite\citet\citep宏;相反,它在处理的早期阶段扫描输入文件,并将、和的所有实例\cite\citet\citep或不带“星号”、带或不带可选参数)封装在{\renewcommand\&{and} ... }“包装器”中。

为了使其在您的文件中工作,只需将代码块从到添加\usepackage{luacode}\end{luacode}您的序言中 - 当然,使用 LuaLaTeX 而不是 pdfLaTeX 来编译您的 tex 文件。

在此处输入图片描述

% !TEX TS-program = lualatex
\documentclass[authoryear,review,11pt]{elsarticle}

\usepackage{filecontents}
\begin{filecontents*}{\jobname.bib}
@article{key,
author = {Authorone, One and Authortwo, Two},
title  = {Title goes here},
journal= {Journal of MWE},
year   = {1991},
volume = {11},
number = {22},
pages  = {303--340},
}
\end{filecontents*}

\usepackage{luacode}
\begin{luacode}
function replace_ampersand ( line )
  line = string.gsub ( line, "\\cite[tp]?%*?{.-}", "{\\renewcommand\\&{and}%0}" )
  line = string.gsub ( line, "\\cite[tp]?%*?%[.*%]{.-}", "{\\renewcommand\\&{and}%0}" )
  return line
end
luatexbase.add_to_callback ( "process_input_buffer", 
    replace_ampersand , "replace_ampersand" )
\end{luacode}

\usepackage[colorlinks=true]{hyperref}

\begin{document}
\citet{key}

\cite[][]{key} 

\citep[see][]{key}

\citep[][p.~24]{key}

\citep*[see][p.~24]{key}

\section*{References}
\bibliographystyle{apalike2}
\bibliography{\jobname}
\end{document}

答案2

我不是natbib专家\citet,我对的选项了解不多\citet,但“安全”的重新定义\citet需要可选参数处理以及保留带星号的\citet*变体:

重新定义\&(如果确实需要)应该在一个组中进行,以便仍然可以在使用之外\&打印。&\citet

编辑改进版本\citet[][]{}\citet*[][]{}方式:

\begin{filecontents*}{\jobname.bib}
@article{key,
author = {Authorone, One and Authortwo, Two},
title = {{Title goes here.}},
journal = {Journal of MWE},
year = {1991},
volume = {11},
number = {22},
pages = {303--340}
}
\end{filecontents*}

%========  ========%

\documentclass[authoryear,review,11pt]{elsarticle}
\usepackage[colorlinks=true]{hyperref}
\usepackage{verbatim}


\usepackage{xparse}
\usepackage{letltxmacro}

\makeatletter
\LetLtxMacro\orig@citet\citet


\RenewDocumentCommand{\citet}{sO{}O{}m}{%
  {% Extra group
    \renewcommand{\&}{and}%
    \IfBooleanTF{#1}{%
      \orig@citet*[#2][#3]{#4}%
    }{%
     \orig@citet[#2][#3]{#4}%
    }%
  }% End of extra group
}


\makeatother

\begin{document}
\section*{In text citations}

In text command \verb|\citet{key}| prints \citet{key}. Note the ``and''.

In text command \verb|\citet{key}| prints \citet[A][]{key}. Note the ``and''.
In text command \verb|\citet{key}| prints \citet[][B]{key}. Note the ``and''.

In text command \verb|\citet{key}| prints \citet[A][B]{key}. Note the ``and''.

In text command \verb|\citet{key}| prints \citet[A][]{key}. Note the ``and''.

In text command \verb|\citet{key}| prints \citet*{key}. Note the ``and''.

In text command \verb|\citet{key}| prints \citet*[A][B]{key}. Note the ``and''.

In text command \verb|\citet{key}| prints \citet*[A]{key}. Note the ``and''.

In text command \verb|\citet{key}| prints \citet*[][B]{key}. Note the ``and''.

And here are some \verb|&| characters: \&\&\&

\section*{References}
\bibliographystyle{apalike2}
\bibliography{\jobname}
\end{document}

相关内容