我正在撰写一篇关于大量艺术作品的论文,并一直在尝试自动创建章节标题,但是当我尝试检查 Biblatex 作者条目是否真的存在时遇到了障碍。
我想做这样的事情:
\documentclass{article}
\usepackage[backend=biber]{biblatex}
\newcommand{\sectiontitle}[1]{\section{%
%check for author field in #1 and include "\citeauthor{#1} - " if it exists%
\citetitle{#1}}}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@ARTWORK{gizapyramid,
title = {Pyramids at Gizeh},
location = {Giza, Egypt},
year = {ca.\@ 2500 BCE}
}
@ARTWORK{bar,
title = {A Bar at the Folies-Berg\`{e}re},
location = {London, England},
year = {1882},
author = {Manet, \'{E}douard}
}
\end{filecontents}
\addbibresource{\jobname.bib}
\begin{document}
\sectiontitle{bar}
\sectiontitle{gizapyramid}
\end{document}
为了达到与此相同的结果:
\documentclass{article}
\usepackage[backend=biber]{biblatex}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@ARTWORK{gizapyramid,
title = {Pyramids at Gizeh},
location = {Giza, Egypt},
year = {ca.\@ 2500 BCE}
}
@ARTWORK{bar,
title = {A Bar at the Folies-Berg\`{e}re},
location = {London, England},
year = {1882},
author = {Manet, \'{E}douard}
}
\end{filecontents}
\addbibresource{\jobname.bib}
\begin{document}
\section{\citeauthor{bar} - \citetitle{bar}}
\section{\citetitle{gizapyramid}}
\end{document}
但是,我不确定如何进行检查才能完成此操作。我如何检查 Biblatex 作者条目是否确实存在并将其实现到我的代码中?
答案1
@lockstep 比我早了一两分钟,但这可能有点不同,所以我发布它,因为它可能在某些地方有用。
\documentclass{article}
\usepackage[backend=biber]{biblatex}
\makeatletter
\let\foo\blx@getdata@cite
\def\usefoo#1{\expandafter\expandafter\expandafter\@empty\csname abx@field@#1\endcsname}
\newcommand{\sectiontitle}[1]{%
\let\abx@name@author\relax\foo{#1}%
\section{%
\ifx\abx@name@author\relax
\else
\citeauthor{#1} --- %
\fi
\citetitle{#1}}}
\makeatother
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@ARTWORK{gizapyramid,
title = {Pyramids at Gizeh},
location = {Giza, Egypt},
year = {ca.\@ 2500 BCE}
}
@ARTWORK{bar,
title = {A Bar at the Folies-Berg\`{e}re},
location = {London, England},
year = {1882},
author = {\'{E}douard Manet}
}
\end{filecontents}
\addbibresource{\jobname.bib}
\begin{document}
\sectiontitle{bar}
\sectiontitle{gizapyramid}
\end{document}
答案2
包含测试的新引用命令\citeauthortitle
(模仿)似乎可以解决问题。\citetitle
\ifnameundef
\documentclass{article}
\usepackage[backend=biber]{biblatex}
\DeclareCiteCommand{\citeauthortitle}
{\boolfalse{citetracker}%
\boolfalse{pagetracker}%
\usebibmacro{prenote}}
{\ifciteindex
{\indexfield{indextitle}}
{}%
\ifnameundef{author}{% NEW
}{% NEW
\printnames{labelname}~-- % NEW
}% NEW
\printfield[citetitle]{labeltitle}}
{\multicitedelim}
{\usebibmacro{postnote}}
\newcommand{\sectiontitle}[1]{\section{\citeauthortitle{#1}}}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@ARTWORK{gizapyramid,
title = {Pyramids at Gizeh},
location = {Giza, Egypt},
year = {ca.\@ 2500 BCE}
}
@ARTWORK{bar,
title = {A Bar at the Folies-Berg\`{e}re},
location = {London, England},
year = {1882},
author = {Manet, \'{E}douard}
}
\end{filecontents}
\addbibresource{\jobname.bib}
\begin{document}
\sectiontitle{bar}
\sectiontitle{gizapyramid}
\end{document}
注意:我使用是\ifnameundef
因为author
字段在 中被视为“名称列表” biblatex
。要测试“文字列表”,应该\iflistundef
对其他字段使用\iffieldundef
。