选择性修改引文的书目宏在 Babel 速记中失败

选择性修改引文的书目宏在 Babel 速记中失败

我使用的代码如下:

\documentclass{article}

\usepackage[english]{babel}

\useshorthands*{"}
\defineshorthand{"-}{\texorpdfstring{\babelhyphen{repeat}}{\discretionary{-}{-}{-}}}

\usepackage{biblatex}
\usepackage{csquotes}

\usepackage{xstring}
\verbtocs{\lecture}|Lecture, topic: \enquote {|

\makeatletter
\newbibmacro{unpublished+titles}[1]{%
    \iffieldundef{title}%
    {#1}%
    {%
        % https://tex.stackexchange.com/a/155276:
        \edef\mytemporary{\thefield{title}}%
        \edef\mytitle{\expandafter\strip@prefix\meaning\mytemporary}%
        \IfBeginWith*{\mytitle}{\clt}%
            {#1}%
            {\mkbibquote{#1\isdot}}%
    }%
}
\makeatother
\DeclareFieldFormat[unpublished]{citetitle}{\usebibmacro{unpublished+titles}{#1}}
\DeclareFieldFormat[unpublished]{title}{\usebibmacro{unpublished+titles}{#1}}

\usepackage{filecontents}
\begin{filecontents*}{\jobname.bib}
    @Unpublished{A1,
        author       = {Author},
        title        = {Some other work}
    }
    @Unpublished{A2,
        author       = {Author},
        title        = {Lecture, topic: \enquote{test}}
    }
    @Unpublished{A3,
        author       = {Author},
        title        = {Test"-test}
    }
\end{filecontents*}
\addbibresource{\jobname.bib}

\begin{document}
    \noindent Citations: \cite{A1}~\cite{A2}%~\cite{A3}

    \printbibliography
\end{document}

在这种情况下,它会检测类型的书目条目unpublished,如果它们的标题以“ ”开头Lecture, topic: \enquote{,它会从书目引用中删除标题的第一部分。一个说明性结果如下:

引用

"-然而,这只适用于标题中不包含简写的条目。如果包含简写,则会出现以下问题:

错误

在 MWE 中取消注释%~\cite{A3}会导致此类错误。

是否可以扩展此功能以涵盖包含"-简写的标题?通过更正上述代码或以其他方式产生所需的结果。

答案1

代码对所涉及的宏进行了相当大的扩展,但是\texorpdfstring{\babelhyphen{repeat}}{\discretionary{-}{-}{-}}不可扩展,因此会中断。

我能找到的一个可行的解决方法是将此定义存储在一个强大的宏中,并在 的定义中使用它"-。在下面的代码中,标题的扩展也保持在最低限度(通过使用\savefield*{title}而不是\edef构造)。

\documentclass{article}
\usepackage[english]{babel}
\usepackage{csquotes}
\usepackage{biblatex}
\usepackage{xstring}
\usepackage{hyperref}

\newrobustcmd*{\robustbutbreakablehyphen}{%
  \texorpdfstring{\babelhyphen{repeat}}{\discretionary{-}{-}{-}}}

\useshorthands*{"}
\defineshorthand{"-}{\robustbutbreakablehyphen}

\makeatletter
\verbtocs{\lecture}|Lecture, topic: \enquote {|

\newbibmacro{unpublished+titles}[1]{%
  \iffieldundef{title}%
    {#1}
    {%
       % https://tex.stackexchange.com/a/155276:
       \savefield*{title}{\mytitle}%
       \IfBeginWith*{\mytitle}{\lecture}
         {#1}
         {\mkbibquote{#1\isdot}}%
    }%
}
\makeatother

\DeclareFieldFormat[unpublished]{citetitle}{\usebibmacro{unpublished+titles}{#1}}
\DeclareFieldFormat[unpublished]{title}{\usebibmacro{unpublished+titles}{#1}}

\usepackage{filecontents}
\begin{filecontents*}{\jobname.bib}
@unpublished{A1,
  author       = {Author},
  title        = {Some other work},
}
@unpublished{A2,
  author       = {Author},
  title        = {Lecture, topic: \enquote{test}},
    }
@unpublished{A3,
  author       = {Author},
  title        = {Test"-test},
}
\end{filecontents*}
\addbibresource{\jobname.bib}

\begin{document}
  Citations: \cite{A1,A2,A3}

  \printbibliography
\end{document}

作者。讲座,主题:“测试”。//作者。“其他一些工作”。//作者。“测试-测试”。


就像 PLK相关问题我建议使用 Biber 源映射和字段注释而不是直接xstring操作。Biber 可以使用 Perl RegEx 扫描字段(并且不会扩展 LaTeX 宏,因此不存在可扩展性和链接问题),因此对于这种情况来说,Biber 拥有强大的工具。

\documentclass{article}
\usepackage[english]{babel}
\usepackage{csquotes}
\usepackage{biblatex}
\usepackage{xstring}
\usepackage{hyperref}

\useshorthands*{"}
\defineshorthand{"-}{\texorpdfstring{\babelhyphen{repeat}}{\discretionary{-}{-}{-}}}

\DeclareSourcemap{
  \maps[datatype=bibtex]{
    \map{
       \step[fieldsource=title, match=\regexp{Lecture,\s+topic:\s\\enquote\s*\{}, final]
       \step[fieldset=title+an, fieldvalue={=noquotes}]
    }
  }
}

\newbibmacro{unpublished+titles}[1]{%
  \iffieldannotation[title]{noquotes}
    {#1}
    {\mkbibquote{#1\isdot}}%
}

\DeclareFieldFormat[unpublished]{citetitle}{\usebibmacro{unpublished+titles}{#1}}
\DeclareFieldFormat[unpublished]{title}{\usebibmacro{unpublished+titles}{#1}}


\usepackage{filecontents}
\begin{filecontents*}{\jobname.bib}
@unpublished{A1,
  author       = {Author},
  title        = {Some other work},
}
@unpublished{A2,
  author       = {Author},
  title        = {Lecture, topic: \enquote{test}},
    }
@unpublished{A3,
  author       = {Author},
  title        = {Test"-test},
}
\end{filecontents*}
\addbibresource{\jobname.bib}

\begin{document}
  Citations: \cite{A1,A2,A3}

  \printbibliography
\end{document}

输出是一样的。

相关内容