You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用natbib创建\citepred命令使特定参考文献显示为红色

实现Natbib中标记新增参考文献为红色的方案

核心思路

通过自定义\citepred命令跟踪需要标红的引用条目,借助工具包拦截参考文献的输出逻辑,对目标条目自动添加红色格式,无需手动修改bib文件。

具体实现步骤

  • 引入xcolor包提供颜色支持,etoolbox包用于管理需要标红的引用键列表
  • 定义\citepred命令:继承\citep的引用功能,同时将当前引用键加入标红列表
  • 重写\bibitem命令:输出参考文献前检查当前键是否在标红列表中,若是则用红色渲染

修改后的完整代码

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{xcolor}
\usepackage{etoolbox}

% 导入natbib并设置格式
\usepackage{natbib}
\bibliographystyle{abbrvnat}
\setcitestyle{authoryear,open={((},close={))}}

% 初始化存储标红引用键的列表
\newcommand{\redcites}{}

% 自定义\citepred命令:执行引用并记录目标键
\newcommand{\citepred}[1]{%
  \citep{#1}%
  \forcsvlist{\listadd\redcites}{#1}%
}

% 重写bibitem,自动判断是否标红
\let\oldbibitem\bibitem
\renewcommand{\bibitem}[1]{%
  \ifthenelse{\inlist{#1}{\redcites}}%
    {\color{red}\oldbibitem{#1}}%
    {\oldbibitem{#1}}%
}

\title{Natbib Example}
\author{Overleaf team}

\begin{document}

\maketitle

\section{First Section}
This document is an example with two cited items: \textit{The \LaTeX\ Companion} book \cite[see][chap 2]{latexcompanion} and Einstein's journal paper \citepred{einstein} (this reference will appear in red). 

% 导入参考文献文件
\bibliography{sample}

\end{document}

使用说明

  • 用\citepred{引用键}替代原\citep{引用键},即可让对应参考文献在列表中显示为红色
  • 支持批量标记多个条目,例如\citepred{einstein,knuthwebsite}会同时标红这两个参考文献
  • bib文件无需任何修改,保持原内容即可

内容的提问来源于stack exchange,提问作者duboisoc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 21:30:06