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선택적 인수에 달리 지정되지 않는 한 제목과 동일할 수 있습니다.

MWE:

\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나는 정렬이 Biber에 의해 수행되어야 하기 때문에 순전히 LaTeX 측면에서 의 내부 에 항목을 "주입"할 공정한 기회가 없다고 생각합니다 .

.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작성하는 도우미 파일이 생성됩니다 . 이 예에서는 모든 항목에 항목 키가 제공되지만 나중에 재사용하기 위해 유용한 항목 키를 제공할 수 있도록 코드를 변경할 수도 있습니다. (현재 특정 항목에 대한 입력 키를 쉽게 검색할 수 없습니다. 사양에는 사용하려는 입력 키와 와 같은 임의의 텍스트에 대해 아무 것도 언급되어 있지 않거나 안전 하지 않아 입력 키에 사용해서는 안 되기 때문입니다. . 이것이 바로 현재 항목에 an을 발행하는 이유 입니다. 입력 키를 예측 가능하게 만들면 이를 분리할 수 있습니다.) 그 외에도 출력 파일을 열고 닫는 일반적인 기록이 필요합니다. ( 언제든지 문서에 쓸 수 있도록 닫아야 합니다 .).bib\tbblx@citeandwritetobibtbblx@name@<counter>authortitleurl\tbblx@citeandwritetobib\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}에서 자동으로 턱받이 항목을 생성합니다.

관련 정보