附录

附录

总体情况

在文档中,我必须提及文档本身的书目行和相应的 BibTeX 块代码。

然后,BibTeX 代码的某些字段(如title author date)不应该硬编码,而应该依赖于相应的 LaTeX 命令定义。

我做了什么

为了获得这种行为,我做了以下 BibTeX 块代码定义:

\makeatletter
\newcommand\bibtexselfentry{
 @book{fauve2023TheSuperBook,
    title  = {\@title},
    author = {\@author},
    publisher = {MadPublisher},
    year = {\@date}
 }
}
\makeatother

如您所见,的值由-上下文内的命令title author year给出。\@title \@author \@year\makeatletter\makeatother

问题出在哪里

当我将此命令放入环境中时,问题就开始了filecontents

\begin{filecontents*}{jobname.bib}
\bibtexselfentry
\end{filecontents*}

因为 filecontents 创建的文件不包含预期的 bib 块,而是包含以下内容:

\bibtexselfentry

因此,后果是 BibTeX 无法找到它fauve2023TheSuperBook,并且就像它不存在一样(事实上它不存在于 bib 文件中)。

一些解释

环境filecontents放入了jobname.bib我给他的文字内容。Hi 不解释它。

问题

是否可以给filecontents某些命令的输出指定不打印文字命令名称?

或者,是否有其他方法可以根据命令输出为 bibfile 提供一些值,而不是对值进行硬编码?


附录

平均能量损失

\documentclass{article}
\usepackage{filecontents}


\title{The Super Book}
\author{Fauve}
\date{3 June 2023}


\makeatletter
\newcommand\bibtexselfentry{
 @book{fauve2023TheSuperBook,
    title  = {\@title},
    author = {\@author},
    publisher = {MadPublisher},
    year = {\@date}
 }
}
\makeatother


\begin{filecontents*}{jobname.bib}
\bibtexselfentry
\end{filecontents*}

\RequirePackage[backend=bibtex]{biblatex}

\addbibresource{jobname.bib}


\begin{document}
You can cite this work with:

\fullcite{fauve2023TheSuperBook}
\vspace{1cm}

Using the following Bib\TeX block:

\texttt
\bibtexselfentry



\end{document}

预期渲染

预期渲染

已获得渲染

已获得渲染

答案1

你的方法有几个问题。首先,filecontents在写入时没有扩展任何东西。它被设计用来写入环境的内容逐字,即不进行任何扩展。

此外,将定义写入\bibtexselfentry文件也无济于事,因为它会生成以下内容:

@book{fauve2023TheSuperBook, title  = {\@title},    author = {\@author}, publisher = {MadPublisher},     year = {\@date} }

但是对于类article\@title\@author\@date在 之后不再有效\maketitle\maketitle将它们重新定义为空。 只需添加\maketitleafter\begin{document}即可看到它。

因此,您需要像这里这样的真正扩展:

\documentclass{article}

\title{The Super Book}
\author{Fauve}
\date{3 June 2023}

\makeatletter
\newcommand\bibtexselfentry{
 @book{fauve2023TheSuperBook,
    title  = {\@title},
    author = {\@author},
    publisher = {MadPublisher},
    year = {\@date}
 }
}
\makeatother

\newwrite\mybibfile
\immediate\openout\mybibfile \jobname.bib
\immediate\write\mybibfile{\bibtexselfentry}
\immediate\closeout\mybibfile

\RequirePackage[backend=bibtex]{biblatex}

\addbibresource{\jobname.bib}

\begin{document}

You can cite this work with:

\fullcite{fauve2023TheSuperBook}
\vspace{1cm}

Using the following Bib\TeX block:

\texttt
\bibtexselfentry

\end{document}

生成一个bib文件:

@book{fauve2023TheSuperBook, title = {The Super Book}, author = {Fauve}, publisher = {MadPublisher}, year = {3 June 2023} }

我还删除了包filecontents,它已经过时多年了,而且在您的示例中也不需要。使用最新的 LaTeX,包会显示警告:

Package filecontents Warning: This package is obsolete. Disabling it and
(filecontents)                passing control to the filecontents environment
(filecontents)                defined by the LaTeX kernel.

我用 替换了jobname.bib\jobname.bib因为我不认为您想写入一个文件jobname.bib,而是一个bib以当前 TeX 作业为基本名称的文件。

所示示例可以得到改进,通过将行尾字符写入文件bib,例如使用

\newcommand\bibtexselfentry{%
 @book{fauve2023TheSuperBook,^^J%
    title  = {\@title},^^J%
    author = {\@author},^^J%
    publisher = {MadPublisher},^^J%
    year = {\@date}^^J%
 }%
}

来写:

@book{fauve2023TheSuperBook,
title = {The Super Book},
author = {Fauve},
publisher = {MadPublisher},
year = {3 June 2023}
}

也许使用\protected@write它的立即版本也是一个好主意:

\documentclass{article}
\usepackage{scrlfile}% provides \protected@immediate@write
\usepackage{listings}
\title{The Super Book}
\author{Fauve\and \textsc{Me}}
\date{3 June 2023}

\makeatletter
\newcommand\bibtexselfentry{%
 @book{fauve2023TheSuperBook,^^J%
    title  = {\@title},^^J%
    author = {\@author},^^J%
    publisher = {MadPublisher},^^J%
    year = {\@date}^^J%
 }%
}

\newwrite\mybibfile
\immediate\openout\mybibfile \jobname.bib
\begingroup
\def\and{\string\and\space}
\protected@immediate@write\mybibfile{}{\bibtexselfentry}
\immediate\closeout\mybibfile
\makeatother

\RequirePackage[backend=bibtex]{biblatex}

\addbibresource{\jobname.bib}

\begin{document}

You can cite this work with:

\fullcite{fauve2023TheSuperBook}
\vspace{1cm}

Using the following Bib\TeX block:

\lstinputlisting{\jobname.bib}

\end{document}

注意:如果你想对某些代码进行缩进,你也可以使用

\newcommand\bibtexselfentry{%
  @book{fauve2023TheSuperBook,^^J
  \space title  = {\@title},^^J
  \space author = {\@author},^^J
  \space publisher = {MadPublisher},^^J
  \space year = {\@date}^^J%
  }%
}

这将导致:

@book{fauve2023TheSuperBook,
  title = {The Super Book},
  author = {Fauve\and \protect \textsc  {Me}},
  publisher = {MadPublisher},
  year = {3 June 2023}
}

分别(没有对\author上一个示例中的进行任何更改):

@book{fauve2023TheSuperBook,
  title = {The Super Book},
  author = {Fauve},
  publisher = {MadPublisher},
  year = {3 June 2023}
}

缩进行的第一个空格来自 之后的行尾字符^^J,第二个空格来自\space命令。

我还建议使用backend=biber。旧版界面使用backend=bibtex功能较少,并且不受所有第三方样式支持。

相关内容