retorquere / zotero-better-bibtex

Make Zotero effective for us LaTeX holdouts
https://retorque.re/zotero-better-bibtex/
MIT License
5.19k stars 284 forks source link

BBT does not escape # in first argument of \href in note #2617

Closed 1-user1 closed 1 year ago

1-user1 commented 1 year ago

Debug log ID

I am not willing to provide any personal data

What happened?

When a note has a URL in it, BBT does not escape the # character in \href's first argument:

Note in Zotero:

https://www.example.com/#foo

Created item:

@misc{TheKey,
  title = {TheTitle},
  author = {{TheAuthor}},
  date = {2023},
  note = {\href{https://www.example.com/#foo}{https://www.example.com/\#foo}}
}
github-actions[bot] commented 1 year ago

Hello there,

Hope you're doing well! @retorquere is here to help you get the most out of your experience with Better BibTeX. To make sure he can assist you effectively, he kindly asks for your cooperation in providing a debug log – it's like giving him the key to understanding and solving the puzzle!

Getting your debug log is a breeze and will save us both time in the long run. Trust us, it's way quicker than discussing why it's important. :smiley:

How to Share Your Debug Log:

  1. If the issue involves specific references or exports, just right-click on the relevant item(s) and choose "Better BibTeX -> Submit Better BibTeX debug log" from the menu.

  2. For other issues, follow these simple steps:

    • Restart Zotero with debugging enabled (Help -> Debug Output Logging -> Restart with logging enabled).
    • Reproduce the problem.
    • Select "Send Better BibTeX debug report..." from the help menu.

Once you hit that submit button, you'll get a special red debug ID. Just share that with us in the issue thread. If the question is regarding an export, don't forget to include what you see exported and what you expected.

By sharing your debug log, you're giving @retorquere a clearer picture of your setup and the items causing the issue. It's like a superhero cape for him – he can swoop in and tackle the problem much faster.

We totally get that your time is valuable, and we appreciate your effort in helping us help you. @2017MK2017, you might be surprised at how much this simple step speeds up the whole process.

Thanks a bunch!

retorquere commented 1 year ago

The first parameter of \href is in verbatim mode, which has different escaping rules.

1-user1 commented 1 year ago

Nevertheless: Biber runs and after that lualatex crashes

retorquere commented 1 year ago

Can you give an MWE latex document?

1-user1 commented 1 year ago
\documentclass{article}

\begin{filecontents}{\jobname.bib}
@misc{TheKey,
  title = {TheTitle},
  author = {{TheAuthor}},
  date = {2023},
  note = {\href{https://www.example.com/#foo}{https://www.example.com/\#foo}}
}
\end{filecontents}

\usepackage[backend=biber,style=apa]{biblatex}
\addbibresource{\jobname.bib}

\begin{document}

Test see \parencite{TheKey}.

\end{document}
retorquere commented 1 year ago

You need to load hyperref to use href

retorquere commented 1 year ago

Does this address your question?

1-user1 commented 1 year ago

Adding

\addbibresource{\jobname.bib}

\usepackage{hyperref}

\begin{document}

does not help.


With and without, the pdf is correctly created, but LuaLaTex throws:

Illegal parameter number in definition of \abx@field@note.

l.19 Test see \parencite{TheKey}.
You meant to type ## instead of #, right?
Or maybe a } was forgotten somewhere earlier, and things
are all screwed up? I'm going to assume that you meant ##.
retorquere commented 1 year ago

This compiles without error with me on overleaf with lualatex

\documentclass{article}
\begin{filecontents}{\jobname.bib}
@misc{TheKey,
  title = {TheTitle},
  author = {{TheAuthor}},
  date = {2023},
  note = {\href{https://www.example.com/\foo}{https://www.example.com/\#foo}}
}
\end{filecontents}
\usepackage{hyperref}
\usepackage[backend=biber,style=apa]{biblatex}
\addbibresource{\jobname.bib}

\begin{document}

Test see \parencite{TheKey}.

\end{document}

make sure to recompile from scratch from the dropdown menu at least once. Otherwise your compilation uses cached files with the error in it.

1-user1 commented 1 year ago

Your note replaced # by \: note = {\href{https://www.example.com/\foo}{https://www.example.com/\#foo}} instead of note = {\href{https://www.example.com/#foo}{https://www.example.com/\#foo}}

retorquere commented 1 year ago

@njbart ?

njbart commented 1 year ago

It seems that hash characters in the first argument of \href should indeed be escaped in case \href is used in the argument of some other command (I assume that this is what’s happening in a biblatex context). See https://tex.stackexchange.com/a/366941.

retorquere commented 1 year ago

Looks like I owe you an apology @2017MK2017. ~I'll get you a fix later today.~ this is going to take a little longer. My bibtex generator is largely stateless, so "is used as an argument" is going to take some tinkering.

@njbart many thanks, I don't know how you unearth these things.

retorquere commented 1 year ago

Ha, wait, because I'm exporting to bib(la)tex, not regular latex, I can be sure this will always apply.

retorquere commented 1 year ago

@2017MK2017 can you make a sample entry that exports the wrong way, export it as BBT json, rename it to .txt, and attach it here?

github-actions[bot] commented 1 year ago

:robot: this is your friendly neighborhood build bot announcing test build 6.7.109.2617.4910 ("testcases for #2617")

Install in Zotero by downloading test build 6.7.109.2617.4910, opening the Zotero "Tools" menu, selecting "Add-ons", open the gear menu in the top right, and select "Install Add-on From File...".

1-user1 commented 1 year ago

@2017MK2017 can you make a sample entry that exports the wrong way, export it as BBT json, rename it to .txt, and attach it here?

Entry: sample_entry.txt

Better CSL JSON does not export the note. Right click, add note and insert:

https://www.example.com/#foo

retorquere commented 1 year ago

I need a "BetterBibTeX JSON" export instead of a "Better CSL JSON" export.

I'm looking into giving more privacy options in the debug log wizard, but this sort of thing is why I press for debug logs. It automates what I need for my work.

retorquere commented 1 year ago

How do I actually output this URL in the note field? I want to check the output for % characters vs how it renders.

retorquere commented 1 year ago

@2017MK2017 the build above fixes it for me, would appreciate it if you could check if it works for you.

@njbart can I get a percent sign in the URL? The MWE below renders the URL with \% in the output, but if I remove the backslash, rendering breaks.

\documentclass{article}
\usepackage{url}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@misc{something-else_one,
  author  = {Some One},
  title   = {Something else},
  year    = {nodate},
  url     = {https://somesite.org/someotherpage},
  urldate = {2017-12-19},
  note    = {\url{https://somesite.org/\%20someotherpage}}
}
\end{filecontents}
\usepackage[style=apa]{biblatex}
\DeclareLanguageMapping{english}{english-apa}
\usepackage{xpatch}
\xpatchbibdriver{misc}
  {\printfield{entrysubtype}}
  {\printfield{entrysubtype}%
   \newunit\newblock
   \printfield{note}}
  {}
  {}
\addbibresource{\jobname.bib}
\pagestyle{empty}
\begin{document}
\nocite{*}
\printbibliography
\end{document}
njbart commented 1 year ago

Your MWE seems to work as expected once \usepackage{hyperref} is added. (I also removed \usepackage{filecontents}, as this is obsolete, the filecontents environment is now defined in the latex kernel.)

\documentclass{article}
\usepackage{url}
\usepackage{hyperref}
\begin{filecontents}{\jobname.bib}
@misc{something-else_one,
  author  = {Some One},
  title   = {Something else},
  year    = {nodate},
  url     = {https://somesite.org/someotherpage},
  urldate = {2017-12-19},
  note    = {\url{https://somesite.org/\%20someotherpage}}
}
\end{filecontents}
\usepackage[style=apa]{biblatex}
\DeclareLanguageMapping{english}{english-apa}
\usepackage{xpatch}
\xpatchbibdriver{misc}
  {\printfield{entrysubtype}}
  {\printfield{entrysubtype}%
   \newunit\newblock
   \printfield{note}}
  {}
  {}
\addbibresource{\jobname.bib}
\pagestyle{empty}
\begin{document}
\nocite{*}
\printbibliography
\end{document}
retorquere commented 1 year ago

So it looks like in url-as-argument verbatim-ish mode, \, % and # need escaping (and subsequent unescaping when parsing bibtex).