阅读时重写部分 BibTeX 文件?

阅读时重写部分 BibTeX 文件?

我使用 Mendeley 来管理我的参考书目、pdf 文件并生成 BibTeX 文件。它有一个严重的错误:标题中的斜体字(例如,生物的科学名称)被写入 .bib 文件,如下所示

{\textless}i{\textgreater}Hordeum spontaneum{\textless}/i{\textgreater}

而不是

\textit{Hordeum spontaneum}.

换句话说,他们正在翻译类似 html 的内容姓名转换为 LaTeX。当然,LaTeX 会逐字打印。

在他们的帮助论坛上,各种各样的人怎么恳求他们都没能解决这个问题。替代方案要么是使用搜索/替换功能浏览 .bib 文件,要么是运行某人编写的 Python 脚本来执行相同操作。困难在于每次 Mendeley 覆盖 .bib 文件时,即每次编辑任何条目时,都必须执行此操作。

但我突然想到可能有一种方法(远远超出了我对 BibTeX 的掌握)可以在 .tex 代码中写入一些内容,本质上是这样的,

\AtEveryBibItem{if the title contains {\textless}i{\textgreater} }{replace it with \textit{ }{}
\AtEveryBibItem{if the title contains {\textless}\i{\textgreater} }{replace it with } }{}

有没有合理的方法来做到这一点?

梅威瑟:

\documentclass[nobib, sfsidenotes]{tufte-book}
\usepackage[dvipsnames]{xcolor}
%
\usepackage[natbib, bibstyle=authoryear, dashed=true, backend=biber,
  doi=false, url=false, arxiv=false, isbn=false, eprint=false, 
  autocite=inline, backref=true, citestyle=verbose, giveninits=true]
  {biblatex}
%
\usepackage{filecontents}
\begin{filecontents*}{jobname.bib}
    @article{King2011,
    author = {King, E G and Roff, D A and Fairbairn, D J},
    journal = {Journal of Evolutionary Biology},
    language = {en},
    number = {2},
    pages = {256--264},
    shorttitle = {Trade-off acquisition and allocation in Gryllus fi},
    title = {{Trade-off acquisition and allocation in 
      {\textless}i{\textgreater}Gryllus firmus{\textless}/i{\textgreater}: a 
      test of the Y model}},
    volume = {24},
    year = {2011}
    }
\end{filecontents*}
%
\addbibresource{jobname.bib}
%
\DeclareBibliographyCategory{Cat1}
\defbibheading{Cat1}{\color{blue}\section*{Cat1}}
%
\DeclareBibliographyCategory{Read}
\defbibheading{Read}{\color{ProcessBlue}\section*{Read}} 
%
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55
% Get rid of "In:"
\renewbibmacro{in:}{%
  \ifentrytype{article}{}{\printtext{\bibstring{in}\intitlepunct}}}
%
% Add colon and space after volume
\renewcommand*{\bibpagespunct}{\addcolon\space}
%
% Get rid of paretheses around year
\usepackage{xpatch}
\xpatchbibmacro{date+extrayear}{%
  \printtext[parens]%
}{%
  \setunit*{\addperiod\space}%
  \printtext%
}{}{}
%
\usepackage{bibentry}
\ExecuteBibliographyOptions{maxnames=20,minnames=1,maxcitenames=2, 
    citetracker=true,
    ibidtracker=constrict, loccittracker=constrict}%,
%
% Biber directives to prevent use of number and month fields, prevents 
  printing in journal entries
\DeclareSourcemap{
  \maps[datatype=bibtex, overwrite]{
    \map{
      \step[fieldset=number, null]
      \step[fieldset=month, null]
    }
  }
}
%
\renewcommand*{\nameyeardelim}{\addcomma\space}         
\renewcommand*{\bibinitdelim}{\unspace\nopunct}         
\DeclareNameAlias{author}{last-first}                   
% turn off quote marks for titles
\DeclareFieldFormat[article,inbook,incollection,inproceedings,
  patent,thesis,unpublished]{title}{#1\isdot}
%
\DeclareFieldFormat[article]{pages}{#1}
\DeclareFieldFormat[article]{volume}{\textbf{#1}}
%
\DefineBibliographyStrings{english}{%
  backrefpage = {page},% originally "cited on page"
  backrefpages = {pages},% originally "cited on pages"
}
%
\newcommand{\addcite}[2]{%
{\color{blue}\cite{#2}}\addtocategory{#1}{#2}
}
\newcommand{\donecite}[2]{%
    {\color{ProcessBlue}\cite{#2}}\addtocategory{Read}{#2}
}
% Get rid of extraneous language info %
\AtEveryBibitem{%
  \clearlist{language}%
}
\AtEveryCitekey{%
  \clearlist{language}%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

No offense to the author, but I read this and I'm just getting crickets: \donecite{Cat1}{King2011}
%
%
\printbibheading[heading=bibintoc]
%
\bibbycategory
%
\end{document} 

答案1

在此处输入图片描述

\documentclass{article}

\usepackage[T1]{fontenc}
\def\zz{%
\def\textless{\aftergroup\zztextless}}

\def\zztextless#1#2#3{%
\ifx i#1\begingroup\itshape#3\else
\ifx /#1\endgroup\fi\fi}


\begin{document}

\begin{zz}
Please don't use  {\textless}i{\textgreater}this{\textless}/i{\textgreater},
fix the  {\textless}i{\textgreater}input{\textless}/i{\textgreater} instead!

\end{zz}

\end{document}

答案2

你可以用源图替换错误的内容:

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
%
\usepackage[]{biblatex}
%
\usepackage{filecontents}
\begin{filecontents*}{jobname.bib}
    @article{King2011,
    author = {King, E G and Roff, D A and Fairbairn, D J},
    journal = {Journal of Evolutionary Biology},
    language = {en},
    number = {2},
    pages = {256--264},
    shorttitle = {Trade-off acquisition and allocation in Gryllus fi},
    title = {{Trade-off acquisition and allocation in
      {\textless}i{\textgreater}Gryllus firmus{\textless}/i{\textgreater}: a
      test of the Y model}},
    volume = {24},
    year = {2011}
    }
\end{filecontents*}
%
\addbibresource{jobname.bib}
%

\DeclareSourcemap{
  \maps[datatype = bibtex]{
    \map{
       \step[fieldsource = title,
          match = \regexp{{<}i{>}},
          replace = \regexp{\\textit\{}]
        \step[fieldsource = title,
          match = \regexp{{<}/i{>}},
          replace = \regexp{\}}]
    }
  }
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

No offense to the author, but I read this and I'm just getting crickets: \cite{King2011}
%
%
\printbibliography
%
%
\end{document}

生成的 .bbl 文件包含以下条目:

 \field{title}{{Trade-off acquisition and allocation in \textit{Gryllus firmus}: a test of the Y model}}

答案3

经过一番折腾,我放弃了在 TeX 中修复的解决方案(并接受 David 的初始答案)。网上有一个 Python(2.7)脚本,稍加改动就可以使用 - 请参阅 https://itskathylam.wordpress.com/2016/01/12/dealing-with-italics-in-bibtex-files-exported-from-mendeley/。我必须稍微调整一下才能让它工作,但它在我的电脑上运行良好。

另一个可能有用的来源是https://github.com/scotartt/clean_bib

还有一个是https://ramblingacademic.com/2016/06/fixing-bibtex-files-mendeley/

我还没尝试过这些。

相关内容