我想防止在复制部分内容时更改参考编号。以下代码在使用plain
书目样式时有效,但我需要使用该IEEEtran
样式。
\begin{filecontents}{\jobname.bib}
@book{Knuth1984texbook,
Author = {Knuth, D.E.},
Title = {The TEXbook, volume A of Computers and typesetting},
Year = {1984}}
@book{Chomsky1957,
Address = {The Hague},
Author = {Noam Chomsky},
Publisher = {Mouton},
Title = {Syntactic Structures},
Year = {1957}}
\end{filecontents}
\documentclass[a4paper,conference]{IEEEtran}
\makeatletter
\newcommand\remembertext[2]{% #1 is a key, #2 is the text
\immediate\write\@auxout{\unexpanded{\global\long\@namedef{mytext@#1}{#2}}}%
#2%
}
\newcommand\recalltext[1]{%
\ifcsname mytext@#1\endcsname
\@nameuse{mytext@#1}%
\else
``??''
\fi
}
\makeatother
\begin{document}
The fragment copied, which must render [2] instead of [1]:
\begin{quote}
``\recalltext{foo}''
\end{quote}
The first cite of the document: \cite{Chomsky1957}
The fragment with the second cite that I want to copy: \remembertext{foo}{Donald Knuth's \TeX book
\cite{Knuth1984texbook}}
\bibliographystyle{IEEEtran}
% \bibliographystyle{plain}
\bibliography{\jobname}
\end{document}
答案1
\cite
这里的问题是,文档中每次使用 都会向.aux
文件写入一个引用请求。这是必要的,因为 BibTeX 随后会从文件中获取请求的引用.aux
。如果参考书目样式按引用顺序排序,则可以根据文件中的引用请求计算出该顺序.aux
。
当您在文档中重述后面的文本时,代码仍然包含并因此执行\cite
(至少在正常情况下,其中\cite
是一个健壮/不可扩展的命令),向文件发送引用请求.aux
。这意味着引用最终可能会比原计划更早地排序。
代码似乎适用于plain
,因为plain
不按外观对引用进行排序,而是按字母顺序排序。因此,文本中引用的顺序与排序无关。
.aux
如果我们暂时禁止使用 写入文件,.aux
我们可以利用引用请求不会写入文件\@fileswfalse
这一现象。这就是我们在 中所做的\recalltext
。请注意,只有当 中的引用请求\remembertext
照常生成时,引用才会起作用。
\documentclass[a4paper,conference]{IEEEtran}
\makeatletter
\newcommand\remembertext[2]{% #1 is a key, #2 is the text
\immediate\write\@auxout{\unexpanded{\global\long\@namedef{mytext@#1}{#2}}}%
#2%
}
\newcommand\recalltext[1]{%
\ifcsname mytext@#1\endcsname
\begingroup
\@fileswfalse
\@nameuse{mytext@#1}%
\endgroup
\else
\G@refundefinedtrue
``\textbf{???}''
\fi
}
\makeatother
\begin{filecontents}{\jobname.bib}
@book{Knuth1984texbook,
Author = {Knuth, D.E.},
Title = {The TEXbook, volume A of Computers and typesetting},
Year = {1984},
}
@book{Chomsky1957,
Address = {The Hague},
Author = {Noam Chomsky},
Publisher = {Mouton},
Title = {Syntactic Structures},
Year = {1957},
}
\end{filecontents}
\begin{document}
The fragment copied, which must render [2] instead of [1]:
\begin{quote}
``\recalltext{foo}''
\end{quote}
The first cite of the document: \cite{Chomsky1957}
The fragment with the second cite that I want to copy:
\remembertext{foo}{Donald Knuth's \TeX book \cite{Knuth1984texbook}}
\bibliographystyle{IEEEtran}
\bibliography{\jobname}
\end{document}
复制的片段必须渲染 [2] 而不是 [1]:
“Donald Knuth 的 TeXbook [2]”