使用 l3regex 检测单个首字母

使用 l3regex 检测单个首字母

编辑:现在我的问题是检测参数是否\mkbibnamegiven是单个首字母。我尝试这样做,l3regex但无法让它工作:

\documentclass{article}
\usepackage{lmodern}
\usepackage{xparse}
\usepackage{expl3}
\usepackage{l3regex}

\usepackage[style=authoryear]{biblatex}

\begin{filecontents*}{\jobname.bib}
  @book{doe2016, author = {J. Doe}, date = {2016}}
  @book{doe2017, author = {P. Doe}, date = {2017}}
  @book{doe2015a, author = {John Doe}, date = {2015}}
  @book{doe2015b, author = {James Doe}, date = {2015}}
\end{filecontents*}

\addbibresource{\jobname.bib}

\ExplSyntaxOn

\NewDocumentCommand{\IfInitial}{mmm}{%
  \regex_match:nnTF{\w\.}{#1}{{\bfseries #2}}{#3}% Doesn't work
  % \regex_match:nnTF{^\w.$}{#1}{{\bfseries #2}}{#3}% Also doesn't work
}

\ExplSyntaxOff

\renewcommand*{\mkbibnamefamily}[1]{\MakeLowercase{\scshape#1}}%
\renewcommand*{\mkbibnamegiven}[1]{
  \ifnumequal{\value{uniquename}}{2}{%
    \IfInitial{#1}{\MakeLowercase{\scshape#1}}{#1}%
  }{%
    \MakeLowercase{\scshape#1}%
  }%
}

\begin{document}

\noindent\cite{doe2015a}\\
\cite{doe2015b}\\
\cite{doe2016}\\% This gives me J. Doe, but I want j. Doe
\cite{doe2017}

\end{document}

原始问题:

这是我提出的问题的延伸这里

使用 BibLaTeX,我尝试在名字只是首字母时将其转换为小写字母,而在名字是全名时将其转换为正常字体。以下是我的尝试:

\documentclass{article}
\usepackage{lmodern}

\usepackage[style=authoryear]{biblatex}

\begin{filecontents*}{\jobname.bib}
  @book{doe2016, author = {J. Doe}, date = {2016}}
  @book{doe2017, author = {P. Doe}, date = {2017}}
  @book{doe2015a, author = {John Doe}, date = {2015}}
  @book{doe2015b, author = {James Doe}, date = {2015}}
\end{filecontents*}

\addbibresource{\jobname.bib}

\renewcommand*{\mkbibnamefamily}[1]{\MakeLowercase{\scshape#1}}%
\renewcommand*{\mkbibnamegiven}[1]{
  \ifnumequal{\value{uniquename}}{2}{#1}{\MakeLowercase{\scshape#1}}%
}

\begin{document}

\noindent\cite{doe2015a}\\% First name is normal, like I want it
\cite{doe2015b}\\% First name is normal, like I want it
\cite{doe2016}\\% First name is normal, but I want it in small caps
\cite{doe2017}% First name is in small caps, like I want it

\end{document}

忽略小写字母,结果如下:

约翰·多伊 2015

詹姆斯·多伊 2015

J. doe 2016

页2017 年母鹿

然而我想要的是:

约翰·多伊 2015

詹姆斯·多伊 2015

j. 2016 年母鹿

页2017 年母鹿

由于年份不同,doe2016不需要全名,所以我们只获取首字母。但的值uniquename仍然是 2,就好像我们需要全名一样。那么,我该如何更改名字的格式,以适应仅打印首字母的情况?

答案1

它不起作用,因为#1\namepartgiven,您需要扩展它才能获得其当前值。

\documentclass{article}
\usepackage{lmodern}
\usepackage{xparse}
\usepackage{expl3}
%\usepackage{l3regex} % not needed with a recent version

\usepackage[style=authoryear]{biblatex}

\begin{filecontents*}{\jobname.bib}
  @book{doe2016, author = {J. Doe}, date = {2016}}
  @book{doe2017, author = {P. Doe}, date = {2017}}
  @book{doe2015a, author = {John Doe}, date = {2015}}
  @book{doe2015b, author = {James Doe}, date = {2015}}
\end{filecontents*}

\addbibresource{\jobname.bib}

\ExplSyntaxOn

\NewDocumentCommand{\IfInitial}{mmm}
 {
  \regex_match:nVTF {\w\.} \namepartgiven {{\bfseries #2}} {#3}
 }
\cs_generate_variant:Nn \regex_match:nnTF { nV }

\ExplSyntaxOff

\renewcommand*{\mkbibnamefamily}[1]{\MakeLowercase{\scshape#1}}%
\renewcommand*{\mkbibnamegiven}[1]{
  \ifnumequal{\value{uniquename}}{2}{%
    \IfInitial{#1}{\MakeLowercase{\scshape#1}}{#1}%
  }{%
    \MakeLowercase{\scshape#1}%
  }%
}

\begin{document}

\noindent\cite{doe2015a}\\
\cite{doe2015b}\\
\cite{doe2016}\\% This gives me J. Doe, but I want j. Doe
\cite{doe2017}

\end{document}

在此处输入图片描述

相关内容