定义的宏、biblatex 和 URL 引用之间的不良交互

定义的宏、biblatex 和 URL 引用之间的不良交互

在使用 pdflatex、biblatex 和 biber 编译文档时,我一直遇到问题。我已将其缩小到我定义的宏,它导致 biblatex 处理 URL 链接的方式出现问题。如果我删除参考书目中的 URL,或者删除 \sb 的定义,那么文档就可以编译。

这是在 Windows 10 上使用 cygwin 的 TeXLive。

文档test.tex

\documentclass[12pt]{article}
\usepackage{biblatex}

\bibliography{refs}

\def \sb {{\bf s}}

\title{A Paper}
\author{A. Author}

\begin{document}

\section{Introduction}

Ref \cite{FedRetirement2018}

\printbibliography

\end{document}

参考书目文件refs.bib

@report{FedRetirement2018,
  author =   {Chien, Yi Li and Morris, Paul},
  title  =   {Many Americans Still Lack Retirement Savings},
  year   =   {2018},
  url    = {https://tinyurl.com/y5cvh89j}
}

运行输出pdflatex; biber; pdflatex

pdflatex test.tex
This is pdfTeX, Version 3.141592653-2.6-1.40.22 (TeX Live 2021/Cygwin) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
(./test.tex
LaTeX2e <2020-10-01> patch level 4
L3 programming layer <2021-02-18>
(/usr/share/texmf-dist/tex/latex/base/article.cls
Document Class: article 2020/04/10 v1.4m Standard LaTeX document class
(/usr/share/texmf-dist/tex/latex/base/size12.clo))
(/usr/share/texmf-dist/tex/latex/biblatex/biblatex.sty
(/usr/share/texmf-dist/tex/generic/pdftexcmds/pdftexcmds.sty
(/usr/share/texmf-dist/tex/generic/infwarerr/infwarerr.sty)
(/usr/share/texmf-dist/tex/generic/iftex/iftex.sty)
(/usr/share/texmf-dist/tex/generic/ltxcmds/ltxcmds.sty))
(/usr/share/texmf-dist/tex/latex/etoolbox/etoolbox.sty)
(/usr/share/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/share/texmf-dist/tex/latex/kvoptions/kvoptions.sty
(/usr/share/texmf-dist/tex/generic/kvsetkeys/kvsetkeys.sty))
(/usr/share/texmf-dist/tex/latex/logreq/logreq.sty
(/usr/share/texmf-dist/tex/latex/logreq/logreq.def))
(/usr/share/texmf-dist/tex/latex/base/ifthen.sty)
(/usr/share/texmf-dist/tex/latex/url/url.sty)
(/usr/share/texmf-dist/tex/latex/biblatex/blx-dm.def)
(/usr/share/texmf-dist/tex/latex/biblatex/blx-compat.def)
(/usr/share/texmf-dist/tex/latex/biblatex/biblatex.def)
(/usr/share/texmf-dist/tex/latex/biblatex/bbx/numeric.bbx
(/usr/share/texmf-dist/tex/latex/biblatex/bbx/standard.bbx))
(/usr/share/texmf-dist/tex/latex/biblatex/cbx/numeric.cbx)
(/usr/share/texmf-dist/tex/latex/biblatex/biblatex.cfg))
(/usr/share/texmf-dist/tex/latex/l3backend/l3backend-pdftex.def)
No file test.aux.
(/usr/share/texmf-dist/tex/latex/biblatex/lbx/english.lbx)
(/usr/share/texmf-dist/tex/latex/l3kernel/expl3.sty)
(/usr/share/texmf-dist/tex/latex/biblatex/blx-case-expl3.sty
(/usr/share/texmf-dist/tex/latex/l3packages/xparse/xparse.sty
(/usr/share/texmf-dist/tex/latex/l3packages/xparse/xparse-2020-10-01.sty
(/usr/share/texmf-dist/tex/latex/l3packages/xparse/xparse-generic.tex))))
No file test.bbl.

LaTeX Warning: Citation 'FedRetirement2018' on page 1 undefined on input line 1
5.


LaTeX Warning: Empty bibliography on input line 17.

[1{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}] (./test.aux)

LaTeX Warning: There were undefined references.


Package biblatex Warning: Please (re)run Biber on the file:
(biblatex)                test
(biblatex)                and rerun LaTeX afterwards.

 )</usr/share/texmf-dist/fonts/type1/public/amsfonts/cm/cmbx12.pfb></usr/share/
texmf-dist/fonts/type1/public/amsfonts/cm/cmr12.pfb>
Output written on test.pdf (1 page, 19194 bytes).
Transcript written on test.log.

biber test
INFO - This is Biber 2.16
INFO - Logfile is 'test.blg'
INFO - Reading 'test.bcf'
INFO - Found 1 citekeys in bib section 0
INFO - Processing section 0
INFO - Looking for bibtex format file 'refs.bib' for section 0
INFO - LaTeX decoding ...
INFO - Found BibTeX data source 'refs.bib'
INFO - Overriding locale 'en-US' defaults 'normalization = NFD' with 'normalization = prenormalized'
INFO - Overriding locale 'en-US' defaults 'variable = shifted' with 'variable = non-ignorable'
INFO - Sorting list 'nty/global//global/global' of type 'entry' with template 'nty' and locale 'en-US'
INFO - No sort tailoring available for locale 'en-US'
INFO - Writing 'test.bbl' with encoding 'UTF-8'
INFO - Output to test.bbl

pdflatex test.tex
This is pdfTeX, Version 3.141592653-2.6-1.40.22 (TeX Live 2021/Cygwin) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
(./test.tex
LaTeX2e <2020-10-01> patch level 4
L3 programming layer <2021-02-18>
(/usr/share/texmf-dist/tex/latex/base/article.cls
Document Class: article 2020/04/10 v1.4m Standard LaTeX document class
(/usr/share/texmf-dist/tex/latex/base/size12.clo))
(/usr/share/texmf-dist/tex/latex/biblatex/biblatex.sty
(/usr/share/texmf-dist/tex/generic/pdftexcmds/pdftexcmds.sty
(/usr/share/texmf-dist/tex/generic/infwarerr/infwarerr.sty)
(/usr/share/texmf-dist/tex/generic/iftex/iftex.sty)
(/usr/share/texmf-dist/tex/generic/ltxcmds/ltxcmds.sty))
(/usr/share/texmf-dist/tex/latex/etoolbox/etoolbox.sty)
(/usr/share/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/share/texmf-dist/tex/latex/kvoptions/kvoptions.sty
(/usr/share/texmf-dist/tex/generic/kvsetkeys/kvsetkeys.sty))
(/usr/share/texmf-dist/tex/latex/logreq/logreq.sty
(/usr/share/texmf-dist/tex/latex/logreq/logreq.def))
(/usr/share/texmf-dist/tex/latex/base/ifthen.sty)
(/usr/share/texmf-dist/tex/latex/url/url.sty)
(/usr/share/texmf-dist/tex/latex/biblatex/blx-dm.def)
(/usr/share/texmf-dist/tex/latex/biblatex/blx-compat.def)
(/usr/share/texmf-dist/tex/latex/biblatex/biblatex.def)
(/usr/share/texmf-dist/tex/latex/biblatex/bbx/numeric.bbx
(/usr/share/texmf-dist/tex/latex/biblatex/bbx/standard.bbx))
(/usr/share/texmf-dist/tex/latex/biblatex/cbx/numeric.cbx)
(/usr/share/texmf-dist/tex/latex/biblatex/biblatex.cfg))
(/usr/share/texmf-dist/tex/latex/l3backend/l3backend-pdftex.def) (./test.aux)
(/usr/share/texmf-dist/tex/latex/biblatex/lbx/english.lbx)
(/usr/share/texmf-dist/tex/latex/l3kernel/expl3.sty)
(/usr/share/texmf-dist/tex/latex/biblatex/blx-case-expl3.sty
(/usr/share/texmf-dist/tex/latex/l3packages/xparse/xparse.sty
(/usr/share/texmf-dist/tex/latex/l3packages/xparse/xparse-2020-10-01.sty
(/usr/share/texmf-dist/tex/latex/l3packages/xparse/xparse-generic.tex))))
(./test.bbl)
! Illegal parameter number in definition of `.
<to be read again> 
                   1
l.18 
     
? 

答案1

正如乌尔丽克·菲舍尔 在评论中

\def \sb {{\bf s}}

用新定义覆盖这些 LaTeX 内核命令\sb(定义为)。由于代码使用,因此这种情况不会出现警告。(如果代码使用了 LaTeX 约定,则会出现一条错误消息,告诉我们已定义。)\let\sb=_\def\newcommand\sb

不幸的是,该url包依赖于 LaTeX 内核的定义\sb并与新定义相违背。

最好的解决方案是不要重新定义\sb。为命令使用不同的名称。大多数编辑器都有搜索和替换功能,因此您可以轻松地在源中切换到新名称。

\documentclass[12pt]{article}
\usepackage{biblatex}

\addbibresource{biblatex-examples.bib}

\newcommand\bolds{\mathbf{s}}

\begin{document}
Lorem \autocite{ctan}
ipsum~\(\bolds^{2}=4\).

\printbibliography
\end{document}

如果你必须坚持使用\sb作为宏名,你可以尝试使用url内核的私有副本\sb,如下所示

\documentclass[12pt]{article}
\usepackage{biblatex}

\addbibresource{biblatex-examples.bib}

\makeatletter
\newcommand*{\real@sb}{}
\let\real@sb=\sb
\def\set@mathnolig#1{\ifnum\mathcode`#1<32768
   \begingroup\lccode`\~`#1\lowercase{\endgroup
     \edef~}{\mathchar\number\mathcode`#1\real@sb{\/}}%
   \mathcode`#1=32768 \fi}
\makeatother

% this overrides the LaTeX kernel command
% avoid this if possible
% code assuming \sb has its usual definition
% may break 
\def \sb {\mathbf{s}}

\begin{document}
Lorem \autocite{ctan}
ipsum~\(\sb^{2}=4\).

\printbibliography
\end{document}

但这只能使url工作正常。其他代码的使用\sb仍然会中断。不要这样做。


请注意,它\bf实际上并未在 LaTeX 内核中定义,仅由一些文档类提供,以便向后兼容 LaTeX 2.09。1994 年发布了 LaTeX2e,它具有不同的字体选择方案。用于\textbf{<text>}粗体文本和\mathbf{<math>}粗体数学。请参阅https://texfaq.org/FAQ-2letterfontcmd

相关内容