根据环境对 citep/citet/citeauthor 进行不同的着色

根据环境对 citep/citet/citeauthor 进行不同的着色

我正在写论文报告,其中通过citepen插入的引文citet是蓝色的,而 -command 的输出citeauthor是常规文本颜色。我使用以下组合实现了这一点这些 答案

问题是,这会对我定义的单独环境(sidebox使用 创建的)中的这些命令产生负面影响,并推翻为该环境定义的特定设置。顾名思义,我将这个环境用作一种侧边栏/框,我希望其中的所有文本(包括引文)都是另一种颜色。tcolorboxhypersetup

平均能量损失

\documentclass[12pt]{report}

\usepackage[colorlinks=true, citecolor=blue]{hyperref}
\usepackage{natbib}
\usepackage{filecontents}
\usepackage{xpatch}
\usepackage[most]{tcolorbox}

\xpretocmd{\citeauthor}{\hypersetup{citecolor=black}}{}{}
\xpretocmd{\citeyear}{\hypersetup{citecolor=black}}{}{}
\xpretocmd{\citep}{\hypersetup{citecolor=blue}}{}{}
\xpretocmd{\citet}{\hypersetup{citecolor=blue}}{}{}

\newcounter{sidebox}
\newenvironment{sidebox}[1]{
\def\sideboxtext{Box}
\hypersetup{citecolor=red}

\newtcolorbox[use counter=sidebox]{sideboxinner}{%
    empty,title={#1},
    minipage boxed title,
    boxed title style={empty,size=minimal,toprule=0pt,top=4pt,left=3mm,overlay={}},
    enlarge left by=8mm,
    width=\textwidth-8mm,
    coltitle=red,coltext=red,fonttitle=,fontupper=\hypersetup{linkcolor=red,citecolor=red}\small,fontlower=\hypersetup{linkcolor=red,citecolor=red},
    before=\par\medskip\noindent,parbox=false,boxsep=0pt,left=2.5mm,right=0mm,top=2pt,breakable,pad at break=0mm,
    before upper=\csname @totalleftmargin\endcsname0pt 
    }
\begin{sideboxinner}
    }
    {
\end{sideboxinner}}

\begin{filecontents}{main.bib}
@article{myref,
    title = {Fancy title},
    journal = {Journal of Fancy Publications},
    author = {Jane Dee and John Doe},
    year = {2017},
    pages = {293--311},
}
\end{filecontents}

\begin{document}

The work by \citeauthor{myref} was a fancy read~\citep{myref}. \citet{myref} was published in the text-coloured year of \citeyear{myref}.

\begin{sidebox}{This is a sidebox}
The work by \citeauthor{myref} was a fancy read~\citep{myref}. \citet{myref} was published in the text-coloured year of \citeyear{myref}.
\end{sidebox}

\bibliographystyle{plainnat}
\bibliography{main}

\end{document}

这编译为以下内容,其中侧框中的引用是蓝色和黑色。 在此处输入图片描述

删除xpretocmd-lines 会产生这样的效果,其中侧边栏按我想要的方式格式化,但是citeauthorciteyear常规文本中是蓝色的 - 而不是黑色。 在此处输入图片描述

迄今为止的尝试

在来这里之前,我曾尝试过自己解决这个问题。我认为 -commandsxpretocmd会影响全部 citep-、citet-、citeauthor- 和 -citeyear命令,我尝试根据环境有条件地重新定义这些命令(顺便说一下,使用\@currenvir,它tcb@savebox用于侧边栏)。这不起作用,大概是因为这些命令是在前言中执行的——其中使用环境仍然未知。

我也尝试xpretocmd在 -code 的开头重新定义这些 -commands sidebox(在这里我也更改了hyperref-settinsg)并在最后重置它们,但没有成功。

问题

我的问题很简单:我怎样才能实现期望的情况?

答案1

不改变超级设置,但定义可以改变的新颜色:

\documentclass[12pt]{report}

\usepackage[colorlinks=true, citecolor=blue]{hyperref}
\usepackage{natbib}
\usepackage{filecontents}
\usepackage{xpatch}
\usepackage[most]{tcolorbox}

\colorlet{citeauthor}{black}
\colorlet{citeyear}{black}
\colorlet{citep}{blue}
\colorlet{citet}{blue}

\xpretocmd{\citeauthor}{\hypersetup{citecolor=citeauthor}}{}{}
\xpretocmd{\citeyear}{\hypersetup{citecolor=citeyear}}{}{}
\xpretocmd{\citep}{\hypersetup{citecolor=citep}}{}{}
\xpretocmd{\citet}{\hypersetup{citecolor=citet}}{}{}

\newcounter{sidebox}
\newenvironment{sidebox}[1]{
\def\sideboxtext{Box}
\hypersetup{citecolor=red}

\newtcolorbox[use counter=sidebox]{sideboxinner}{%
    empty,title={#1},
    minipage boxed title,
    boxed title style={empty,size=minimal,toprule=0pt,top=4pt,left=3mm,overlay={}},
    enlarge left by=8mm,
    width=\textwidth-8mm,
    coltitle=red,coltext=red,fonttitle=,fontupper=\small,fontlower=,
    before=\par\medskip\noindent,parbox=false,boxsep=0pt,left=2.5mm,right=0mm,top=2pt,breakable,pad at break=0mm,
    before upper=\csname @totalleftmargin\endcsname0pt 
    }
\begin{sideboxinner}
\colorlet{citeauthor}{red}
\colorlet{citeyear}{red}
\colorlet{citep}{red}
\colorlet{citet}{red}
    }
    {
\end{sideboxinner}}

\begin{filecontents}{main.bib}
@article{myref,
    title = {Fancy title},
    journal = {Journal of Fancy Publications},
    author = {Jane Dee and John Doe},
    year = {2017},
    pages = {293--311},
}
\end{filecontents}

\begin{document}

The work by \citeauthor{myref} was a fancy read~\citep{myref}. \citet{myref} was published in the text-coloured year of \citeyear{myref}.

\begin{sidebox}{This is a sidebox}
The work by \citeauthor{myref} was a fancy read~\citep{myref}. \citet{myref} was published in the text-coloured year of \citeyear{myref}.
\end{sidebox}

The work by \citeauthor{myref} was a fancy read~\citep{myref}. \citet{myref} was published in the text-coloured year of \citeyear{myref}.

\bibliographystyle{plainnat}
\bibliography{main}

\end{document}

在此处输入图片描述

答案2

根据 @coffeetable 的回答,我找到了下面的解决方案。这略有不同(而不是在 -part 中定义颜色sideboxinner,而是在更高的地方定义颜色),以便不缩进 sidebox-content 的第一行。添加%- 符号和/或删除换行符(基于线程)并没有解决该问题。

无论如何,这消除了对额外hypersetup命令的需要。

\documentclass[12pt]{report}

\usepackage[colorlinks=true, citecolor=blue]{hyperref}
\usepackage{natbib}
\usepackage{filecontents}
\usepackage{xpatch}
\usepackage[most]{tcolorbox}

\colorlet{citeauthor}{black}
\colorlet{citeyear}{black}
\colorlet{citep}{blue}
\colorlet{citet}{blue}

\xpretocmd{\citeauthor}{\hypersetup{citecolor=citeauthor}}{}{}
\xpretocmd{\citeyear}{\hypersetup{citecolor=citeyear}}{}{}
\xpretocmd{\citep}{\hypersetup{citecolor=citep}}{}{}
\xpretocmd{\citet}{\hypersetup{citecolor=citet}}{}{}

\newcounter{sidebox}
\newenvironment{sidebox}[1]{
\def\sideboxtext{Box}
\colorlet{citeauthor}{red}
\colorlet{citeyear}{red}
\colorlet{citep}{red}
\colorlet{citet}{red}

\newtcolorbox[use counter=sidebox]{sideboxinner}{%
    empty,title={#1},
    minipage boxed title,
    boxed title style={empty,size=minimal,toprule=0pt,top=4pt,left=3mm,overlay={}},
    enlarge left by=8mm,
    width=\textwidth-8mm,
    coltitle=red,coltext=red,fonttitle=,fontupper=\small,fontlower=,
    before=\par\medskip\noindent,parbox=false,boxsep=0pt,left=2.5mm,right=0mm,top=2pt,breakable,pad at break=0mm,
    before upper=\csname @totalleftmargin\endcsname0pt 
    }
\begin{sideboxinner}
    }
    {
\end{sideboxinner}}

\begin{filecontents}{main.bib}
@article{myref,
    title = {Fancy title},
    journal = {Journal of Fancy Publications},
    author = {Jane Dee and John Doe},
    year = {2017},
    pages = {293--311},
}
\end{filecontents}

\begin{document}

The work by \citeauthor{myref} was a fancy read~\citep{myref}. \citet{myref} was published in the text-coloured year of \citeyear{myref}.

\begin{sidebox}{This is a sidebox}
The work by \citeauthor{myref} was a fancy read~\citep{myref}. \citet{myref} was published in the text-coloured year of \citeyear{myref}.
\end{sidebox}

The work by \citeauthor{myref} was a fancy read~\citep{myref}. \citet{myref} was published in the text-coloured year of \citeyear{myref}.

\bibliographystyle{plainnat}
\bibliography{main}

\end{document}

在此处输入图片描述

相关内容