Biblatex:從 \addCitation{http://myurl} 自動建立一個書目條目

Biblatex:從 \addCitation{http://myurl} 自動建立一個書目條目

在文件中,我需要透過 biblatex 條目添加許多鏈接,例如:

\NewDocumentCommand{\addLink}{O{}mm}{\href{#2}{#3}}

\addLink{https://en.wikipedia.org/}{Wikipedia}\cite{Wiki}

其中圍兜包含:

@online{Wiki,
    title        = {Wikipedia},
    author       = {Wikipedia},
    year         = 2024,
    url          = {https://en.wikipedia.org/},
}

但是手動維護這兩個清單是相當昂貴的,並且為簡單的連結添加圍脖條目是相當煩人且難以維護的。是否可以透過在 中自動建立條目來避免這種情況\NewDocumentCommand?除非在可選參數中另外指定,否則年份可以固定為2024並且author可以等於標題。

微量元素:

\documentclass[]{article}

\begin{filecontents}[noheader,overwrite]{main.bib}
@online{Wiki,
    title        = {Wikipedia},
    author       = {Wikipedia},
    year         = 2024,
    url          = {https://en.wikipedia.org/},
}
\end{filecontents}

\usepackage[
  style=alphabetic,% also 
  minalphanames=3,maxalphanames=4, % [Foo+99] -> [FBB+99].
  maxnames=99, % Do not put "et al". Sets maxbibnames, maxcitenames and maxsortnames.
  sortcites=true,
  %sorting=none,
  doi=false,
  url=false,
  giveninits=true, % Bob Foo --> B. Foo
  isbn=false,
  url=false,
  eprint=false,
  sortcites=false, % \cite{B,A,C}: [A,B,C] --> [B,A,C]
%backref=true, % [1] Title, blabla --> [1] Title, blabla (pages 1, 45, 56)
%% TODO: customize using https://tex.stackexchange.com/questions/36307/formatting-back-references-in-bibliography
]{biblatex}
\addbibresource{main.bib}%

\NewDocumentCommand{\addLink}{O{}mm}{
  \href{#2}{#3}
}
\usepackage{hyperref}


\begin{document}

You can go to \addLink{https://en.wikipedia.org/}{Wikipedia}\cite{Wiki}.

\printbibliography

\end{document}

編輯 我想我可以想到一個解決方案,將所有條目寫入臨時文件,並且(也許重命名?)並在啟動時加載該文件......但似乎是一個非常骯髒的解決方案。還有更好的嗎?

答案1

我不認為biblatex純粹在 LaTeX 方面將條目「注入」內部的公平機會,尤其是因為排序必須由 Biber 完成。

所以我認為你最好的機會是寫入臨時.bib文件和\addbibresource該文件。這都可以在輔助巨集中對使用者隱藏,如下所示我的答案使用 biblatex 將特定作者加粗

例如你可以嘗試

\documentclass[]{article}

\usepackage[
  style=alphabetic,% also 
  minalphanames=3,maxalphanames=4, % [Foo+99] -> [FBB+99].
  maxnames=99, % Do not put "et al". Sets maxbibnames, maxcitenames and maxsortnames.
  sortcites=true,
  %sorting=none,
  doi=false,
  url=false,
  giveninits=true, % Bob Foo --> B. Foo
  isbn=false,
  url=false,
  eprint=false,
  sortcites=false, % \cite{B,A,C}: [A,B,C] --> [B,A,C]
]{biblatex}
\usepackage{hyperref}


\makeatletter
\def\tbblx@bibfile@name{\jobname -tblx.bib}
\newwrite\tbblx@bibfile
\immediate\openout\tbblx@bibfile=\tbblx@bibfile@name

\immediate\write\tbblx@bibfile{%
  @comment{Auto-generated file}\blx@nl}

\newcounter{tbblx@name}
\setcounter{tbblx@name}{0}

\newcommand*{\tbblx@citeandwritetobib}[3]{%
  \stepcounter{tbblx@name}%
  \edef\tbblx@tmp@cite{%
    \noexpand\autocite{tbblx@name@\the\value{tbblx@name}}}%
  \tbblx@tmp@cite
  \immediate\write\tbblx@bibfile{%
    @online{tbblx@name@\the\value{tbblx@name},
          author  = {\unexpanded{#1}}, %
          title   = {\unexpanded{#2}},
          year    = {2024},
          url     = {\unexpanded{#3}},}%
  }%
}

\AtEndDocument{%
  \closeout\tbblx@bibfile}

\addbibresource{\tbblx@bibfile@name}

\NewDocumentCommand{\addLink}{O{#3}mm}{%
  \href{#2}{#3}
  \tbblx@citeandwritetobib{#1}{#3}{#2}%
}
\makeatother

\addbibresource{biblatex-examples.bib}

\begin{document}

You can go to \addLink{https://en.wikipedia.org/}{Wikipedia}.
\cite{sigfridsson}

\printbibliography

\end{document}

這會產生一個幫助文件<jobname>-tblx.bib,我們可以在.bib其中動態寫入所有條目(透過\tbblx@citeandwritetobib)。在此範例中,所有條目都給出了輸入金鑰tbblx@name@<counter>,但您當然可以變更程式碼,以便可以提供有用的輸入金鑰以供日後重複使用。 (目前,您無法輕鬆檢索特定條目的輸入鍵,因為規範沒有提及您要使用的輸入鍵,並且任意文字(如 ,authortitleurl不安全,不應用於輸入鍵這也是為什麼現在向條目\tbblx@citeandwritetobib發出 an 的原因\autocite(如果您使條目鍵可預測,則可以將其分開。)除此之外,我們只需要開啟和關閉輸出檔案的常規記錄。 (我們需要關閉\AtEndDocument以確保我們可以隨時在文件中寫入。)

答案2

只是為了補充 moewe 的答案,我意識到僅使用計數器很容易出錯(例如,如果我只刪除一個元素,如果我忘記重新運行 biblatex,則所有條目都將被移動而不會出現任何錯誤)。

所以我用的是:

\usepackage{pdftexcmds}
\makeatletter
\def\tbblx@bibfile@name{\jobname -tblx.bib}
\newwrite\tbblx@bibfile
\immediate\openout\tbblx@bibfile=\tbblx@bibfile@name

\immediate\write\tbblx@bibfile{%
  @comment{Auto-generated file}\blx@nl}

\usepackage{pdftexcmds}
\newcommand*{\tbblx@citeandwritetobib}[5]{%
  \edef\tbblx@name@entry{\pdf@mdfivesum{\detokenize{#1#2#3#4#5}}}%just the counter is not enough, as we might forget to run latexmk to regenerate the entries if one changes. Also, not adding the counter is better as it allows exactly identical entries to share the same line in the bibliography.
  \edef\tbblx@tmp@cite{%
    \noexpand\cite{tbblx@name@\tbblx@name@entry}}%
  \tbblx@tmp@cite
  \immediate\write\tbblx@bibfile{%
    @online{tbblx@name@\tbblx@name@entry,
          author  = {\unexpanded{#1}}, %
          title   = {\unexpanded{#2}},
          year    = {#4},
          url     = {\unexpanded{#3}}
        }%
  }%
}

\AtEndDocument{%
  \closeout\tbblx@bibfile}

\addbibresource{\tbblx@bibfile@name}

\NewDocumentCommand{\mylinkCite}{O{{#3}}mO{#4}mO{2024}}{%
  \href{#2}{#4}
  \tbblx@citeandwritetobib{#1}{#3}{#2}{#5}{}%
}

\makeatother

我這樣使用它:\mylinkCite[author, default to text]{link}[title, default to text]{text to write in pdf}[year (default 2024)]

另請注意,biblatex 不打算立即支援此功能,因為它太複雜而無法在內部實現Biblatex:從 \addCitation{http://myurl} 自動建立一個書目條目

相關內容