仅在第一次使用时生成 \cite{} 的命令

仅在第一次使用时生成 \cite{} 的命令

在一本有关 R 的书中,我\pkg{}对每次提及 R 包都使用一个命令,该命令还会通过另一个命令生成索引扩展\ixp{}

\newcommand{\pkg}[1]{\textsf{#1}\ixp{#1}}

所有软件包都有 .bib 条目,我想修改它,以便软件包\cite{}在其第一的使用,但随后会生成一个\nocite{}。也就是说,

\newcommand{\pkg}[1]{\textsf{#1}\ixp{#1} \citep{#1}}

但是,如果 .bib 键参数 ( ) 之前在文本中出现过,则位置\citep{#1}会变成。 (我记得,词汇表包会做类似的事情。) 我该如何修改此定义以实现此行为?\nocite{#1}#1

这里的目标是生成所有软件包的参考文献,但要避免每次提及都引用文本而造成文本混乱。\nocite{}为了在作者索引中生成条目,需要在第一次出现后使用。

以下是 MWE:

\documentclass[12pt]{book}

\usepackage[comma]{natbib}
\renewcommand{\bibname}{References}
\bibliographystyle{abbrvnat}

\usepackage{imakeidx}
\makeindex[title=Subject Index,columns=2]

% Cited packages

\newcommand{\Rpackage}[1]{\pkg{#1} package}
%\newcommand{\pkg}[1]{\textsf{#1}\ixp{#1}}
\newcommand{\pkg}[1]{\textsf{#1}\ixp{#1} \citep{#1}}

% R packages:  indexed under both package name and packages!
\newcommand{\ixp}[1]{%
   \index{#1@\textsf{#1} package}%
   \index{package!#1@\textsf{#1}}%
    }

\usepackage{filecontents}
\begin{filecontents}{references.bib}
@Manual{vcd,
  title =        "vcd: Visualizing Categorical Data",
  author =       "David Meyer and Achim Zeileis and Kurt Hornik",
  year =         "2015",
  note =         "R package version 1.3-3",
}

@Manual{vcdExtra,
  title =        "vcdExtra: vcd Extensions and Additions",
  author =       "Michael Friendly",
  year =         "2015",
  note =         "R package version 0.6-7",
  URL =          "http://CRAN.R-project.org/package=vcdExtra",
}

\end{filecontents}

\begin{document}

\chapter{First chapter}
Here we mention the \pkg{vcd} package and \pkg{vcdExtra}.  
% Should generate a \citep{vcd} and \citep{vcdExtra} 

\section{First section}
Here we mention the \pkg{vcd} package and \pkg{vcdExtra} again.
These should generate nocite instead.
% Should generate a \nocite{vcd} and \nocite{vcdExtra} 

\chapter{Second chapter}
Here we mention the \pkg{vcd} package and \pkg{vcdExtra} again
These should generate nocite instead.
% Should generate a \nocite{vcd} and \nocite{vcdExtra} 

\section{First section}
asfdAFASFAF

\bibliography{references}
\printindex

\end{document}
% ----------------------------------------------------------------

答案1

也许你需要这个:

\def\pkg#1{\textsf{#1}\ixp{#1}~\citex{#1}}
\def\citex#1{\expandafter\ifx\csname cit:#1\endcsname\relax
      \expandafter\gdef\csname cit:#1\endcsname{}%
      \citep{#1}%
   \else
      \nocite{#1}%
   \fi
}

\citex宏测试该参数是否之前使用过,即控制序列是否\cit:parameter已定义。如果没有,则扩展为\citep{parameter}并定义\cit:parameter。否则扩展为\nocite{parameter}

答案2

我添加了一个*无论如何都会打印引用的 -variant,它不算作第一个(也许你想在介绍中或文档的后面部分使用它)。

\begin{filecontents*}{\jobname.bib}
@Manual{vcd,
  title =        "vcd: Visualizing Categorical Data",
  author =       "David Meyer and Achim Zeileis and Kurt Hornik",
  year =         "2015",
  note =         "R package version 1.3-3",
}

@Manual{vcdExtra,
  title =        "{vcdExtra}: vcd Extensions and Additions",
  author =       "Michael Friendly",
  year =         "2015",
  note =         "R package version 0.6-7",
  URL =          "http://CRAN.R-project.org/package=vcdExtra",
}

\end{filecontents*}

\documentclass[12pt]{book}

\usepackage[comma]{natbib}
\renewcommand{\bibname}{References}
\bibliographystyle{abbrvnat}

\usepackage{imakeidx}
\makeindex[title=Subject Index,columns=2]

% Cited packages

\newcommand{\Rpackage}[1]{\pkg{#1} package}
%\newcommand{\pkg}[1]{\textsf{#1}\ixp{#1}}

\makeatletter
\DeclareRobustCommand{\pkg}{%
  \@ifstar{\@tempswatrue\pkg@do}{\@tempswafalse\pkg@do}%
}
\newcommand\pkg@do[1]{%
  % fixed part
  \textsf{#1}\ixp{#1}%
  \if@tempswa
    \space\citep{#1}%
  \else
    \ifcsname pkg@cited@#1\endcsname
      %\nocite{#1}% uncomment, if you want, but it does nothing
    \else
      \space\citep{#1}%
      \global\expandafter\let\csname pkg@cited@#1\endcsname\@empty
    \fi
  \fi
}
\makeatother

% R packages:  indexed under both package name and packages!
\newcommand{\ixp}[1]{%
   \index{#1@\textsf{#1} package}%
   \index{package!#1@\textsf{#1}}%
    }

\begin{document}

\chapter{First chapter}
Here we mention the \pkg{vcd} package and \pkg{vcdExtra}.  
% Should generate a \citep{vcd} and \citep{vcdExtra} 

\section{First section}
Here we mention the \pkg{vcd} package and \pkg{vcdExtra} again.
These should generate nocite instead.
% Should generate a \nocite{vcd} and \nocite{vcdExtra} 

But we want to cite \pkg*{vcd} again.

\chapter{Second chapter}
Here we mention the \pkg{vcd} package and \pkg{vcdExtra} again
These should generate nocite instead.
% Should generate a \nocite{vcd} and \nocite{vcdExtra} 

\section{First section}
asfdAFASFAF

\bibliography{\jobname}
\printindex

\end{document}

在此处输入图片描述

答案3

假设这\nocite不是真正需要的,那么这应该有效:

\newcommand{\expkg|[1]{\textsf{#1}\ixp{#1}}
\newcommand{\pkg}[1]{\textsf{#1}\ixp{#1} \citep{#1}\expkg{#1}}

毕竟,单个条目\cite将在文件中放置一个条目\.aux,告诉 bibtex 识别.bib文件中的匹配条目并在.bbl文件中创建一个等效条目——只需要一个。的通常目的\nocite 是确保项目包含在文件中.bbl(因此包含在参考书目中),如果它是不是文中确实引用了。

答案4

这不是一个直接的答案,但也许你会发现它很有用。这是我编写的一个包,但从未发布过,用于类似但不完全相同的目的。这个包的目的是使编写有关编程语言的文章变得更容易。要引用 Java 等语言,请\Java在输入中写入。它会添加引用,但前提是这是第一次使用该命令。

\需要TeX格式{LaTeX2e}
\ProvidesPackage{yogi-lang}[2005/09/12 v0.6]
\DeclareOption{简洁}{\放松}
\DeclareOption{详细}{\放松}
\进程选项

\RequirePackage{xspace}
\newcommand{\@pl}[1]{\mbox{\textsc{\ensureLR{#1}}}\xspace}
\newcommand\lang[1]{\@pl{#1}}

\newcommand\NonCitingUseX[1]{\csname nocite#1\endcsname}
\newcommand\NonCitingUse[1]{\NonCitingUseX{#1}\xspace}
\newcommand\@IgnoreLanguageCitation[1]{%
    \expandafter\gdef\csname#1\endcsname{\csname nocite#1\endcsname}%
}
\newcommand\StopLanguageCitation[1]{%
\expandafter\gdef\csname#1\endcsname{\csname nocite#1\endcsname\xspace}%
}


\def\@LANG#1#2#3{%
    \expandafter\gdef\csname nocite#1\endcsname{#3}% 定义基本版本,例如 \nociteJava
    \expandafter\gdef\csname#1\endcsname{% 首次调用。
        \NonCitingUseX{#1}\relax % 调用基本版本。
        \StopLanguageCitation{#1}\relax % 重置定义以供将来使用。
         ~#2\relax % 附加引用。
        \x空格
    }%
    \x空格
}

\newcommand\NewLanguageCitation[2]{\@LANG{#1}{\textup{#2}}{\textsc{#1}}}
\newcommand\NewLangaugeCitationSpecial[3]{\@LANG{#1}{#2}{#3}}

\DeclareOption{简洁}{%
\gdef\@LANG#1#2#3{%
    \expandafter\gdef\csname nocite#1\endcsname{#3}% 定义基本版本,例如 \nociteJava
    \expandafter\gdef\csname#1\endcsname{% 首次调用。
        \NonCitingUseX{#1}\relax % 调用基本版本。
        \StopLanguageCitation{#1}\relax % 重置定义以供将来使用。
        %~#2\relax % 附加引用。
        \x空格
    }%
   \x空格
}}

\进程选项
\def\STOPLANGUAGECITATION{\def\langcite##1{}}
\def\STARTLANGUAGECITATION{\def\langcite##1{\cite{##1}}}
\STARTLANGUAGECITATION
%使用字母顺序
\NewLanguageCitation{Go}{\langcite{XXX}}
\NewLangaugeCitationSpecial{AJEE}{\langcite{Cohen:Gil:04}}{\textsc{AspectJ2EE}}
\NewLanguageCitation{Ada}{\langcite{Tucker:97}}
\NewLanguageCitation{BCPL}{\langcite{Richards:Whitby-Strevens:80}}
\NewLanguageCitation{AspectJ}{\langcite{Kiczales:2001}}
\NewLanguageCitation{AWK}{\langcite{Aho:Kernighan:Weinberger:Book:88}}
\NewLanguageCitation{基本}{}
\NewLanguageCitation{CPL}{\langcite{Kernighan:Ritchie:Book:88}}
\NewLangaugeCitationSpecial{CC}{\langcite{Stroustrup:Book:97}}{\mbox{C++}}
\NewLanguageCitation{塞西尔}{\langcite{Chambers:Cecil:93}}
\NewLanguageCitation{CLOS}{\langcite{CLOS}}
\NewLanguageCitation{Cobol}{}
\NewLanguageCitation{CommonLoops}{\langcite{Bobrow:Kahn:Kiczales:Masinter:Stefik:Zdybel:86}}
\NewLangaugeCitationSpecial{CProlog}{\langcite{Shapiro:87}}{\textsc{Concurrent Prolog}}
\NewLangaugeCitationSpecial{CSharp}{\langcite{Hejlsberg:Wiltamuth:Golde:03}}{\textsc{C}${}^{\#}$}
\NewLanguageCitation{Datalog}{\langcite{Ceri:Gottlob:Tanca:90}}
\NewLanguageCitation{迪伦}{\langcite{迪伦:沙利特:97}}
\NewLanguageCitation{埃菲尔}{\langcite{埃菲尔:参考:97}}
\NewLanguageCitation{变化无常}{\langcite{Drossopoulou:Damiani:Ciancaglini:Giannini:01}}
\NewLanguageCitation{风味}{\langcite{Moon:Flavors:86}}
\新语言引用{Fortran}{}
\NewLanguageCitation{Haskell}{\langcite{Jones:03}}
\NewLanguageCitation{Jam}{\langcite{安科纳:拉戈里奥:祖卡:03}}
\NewLanguageCitation{Java}{\langcite{Arnold:Gosling:96}}
\NewLanguageCitation{JavaScript}{\langcite{Flanagan:11}}
\NewLanguageCitation{Kea}{\langcite{Kea}}
\NewLanguageCitation{Lisp}{\langcite{Graham:95}}
\NewLanguageCitation{Mathematica}{\langcite{mangano:10}}
\NewLanguageCitation{梅萨}{}
\NewLanguageCitation{MixGen}{\langcite{Allen:Bannet:Cartwright:03}}
\NewLanguageCitation{ML}{\langcite{ml-spec:Milner:Tofte:Harper:MacQueen:97}}
\NewLanguageCitation{Modula}{\langcite{Nelson:91}}
\NewLanguageCitation{NextGen}{\langcite{Allen:Bannet:Cartwright:03}}
\NewLanguageCitation{Oberon}{\langcite{Wirth:Reiser:Book:92}}
\NewLanguageCitation{ObjectiveC}{\langcite{Cox:Book:Evolutionary:86}}
\NewLanguageCitation{Pascal}{\langcite{Wirth:71}}
\NewLanguageCitation{Perl}{\langcite{Perl:94}}
\NewLanguageCitation{PHP}{\langcite{Lerdorf:Tatroe:02}}
\NewLangaugeCitationSpecial{PLI}{\langcite{Hughes:86}}{\textsc{PL/1}}
\NewLanguageCitation{PolyGlot}{\langcite{PolyGlot}}
\NewLanguageCitation{Prolog}{\langcite{Deransart:Cervoni:Ed-Dbali:96}}
\NewLanguageCitation{Python}{\langcite{Lutz:96}}
\NewLanguageCitation{Ruby}{\langcite{Thomas:Hunt:00}}
\NewLanguageCitation{Scala}{\langcite{Odersky:Altherr:Cremet:Emir:Maneth:Micheloud:Mihaylov:Schinz:Stenman:Zenger:04}}
\NewLanguageCitation{方案}{\langcite{Dickey:92}}
\NewLanguageCitation{SED}{\langcite{ref:sed}}
\NewLanguageCitation{自我}{\langcite{Ungar:Smith:87}}
\NewLanguageCitation{Smalltalk}{\langcite{Goldberg:Book:84}}
\新语言引用{SQL}{}
\NewLanguageCitation{Tcl}{\langcite{Ousterhout:90}}
\NewLangaugeCitationSpecial{THETA}{\langcite{Liskov:al:95}}{\lang{Theta}}
\NewLanguageCitation{XQuery}{\langcite{XQuery}}
\NewLanguageCitation{APL}{\langcite{Iverson:62}}

相关内容