plk / biblatex

biblatex is a sophisticated bibliography system for LaTeX users. It has considerably more features than traditional bibtex and supports UTF-8
518 stars 118 forks source link

refsection=chapter does not work with KOMA-Script 3.26a #857

Closed komascript closed 5 years ago

komascript commented 5 years ago

I've got a bug report about an issue using refsection=chapter with KOMA-Script 3.26a. With this example:

\listfiles
\documentclass{scrbook}
\usepackage[backend=biber, refsection=chapter]{biblatex}
\begin{filecontents}{\jobname.bib}
  @misc{B1,
    title = {Bib1}
  }
  @misc{B2,
    title = {Bib2}
  }
\end{filecontents}
\addbibresource{\jobname.bib}

\begin{document}
\chapter{Ch1}
\nocite{B1}
\printbibliography
\chapter{Ch2}
\nocite{B2}
\printbibliography
\end{document}

Both chapters show the complete bibliography not only the cites of the chapter. A look into the aux-file (and comparing it with the same file using book instead of scrbook) shows, that refsection is not increased and the \newlabel commands are missing. I do not know enough about the biblatex code to find out the reason. I only found comments about code changes for KOMA-Script 3.26.

Please have a look into it. If I can help to fix the issue, e.g. adding another hook, please tell me.

moewew commented 5 years ago

Thank you for reporting this.

This is probably related to https://github.com/plk/biblatex/issues/713 and https://komascript.de/node/2168. I (wrongly, as it now turns out) assumed that I could also use the new \At@startsection to patch \chapter as well. The earlier code (\pretocmd\@makechapterhead and \pretocmd\@makeschapterhead) seems to do the right thing, so we could go back to that. But if there is an official hook to patch into chapter headings I would of course prefer to use that.

komascript commented 5 years ago

I see.

\chapter (and \part) do not use \scr@startsection. So (for me) it is clear, that the hook of \At@startsection is not executed. Before I add corresponding commands \At@startchapter and \At@startpart and hooks for chapter and part style commands, I have to have a look into the code, whether or not there are already hooks, because there are a lot of hooks in the headings mechanism, because several wrapper class and package authors asked for hooks. I really cannot keep all of them in mind. :(

But currently I need some time.

e-kwsm commented 5 years ago

Just in case, the version of biblatex is 2018/11/02 v3.12 (sorry for cross post).

moewew commented 5 years ago

@komascript While \pretocmd\@makechapterhead and \pretocmd\@makeschapterhead seem to work in the MWE, a superficial look at the code in scrbook.cls suggests to me that it might be a slightly better idea to use \scr@makechapterhead and \scr@makeschapterhead instead. That should make things work with chapter preambles better. I didn't find any obvious code that modifies \scr@makechapterhead and \scr@makeschapterhead which would imply that it is not completely bonkers to patch into those commands.

Of course an official ruling as to what should be done here would be very much appreciated.

@e-kwsm A temporary work-around could be to restore the old behaviour of patching \@makechapterhead and \@makeschapterhead

\listfiles
\documentclass{scrbook}
\usepackage[backend=biber, refsection=chapter]{biblatex}
\addbibresource{biblatex-examples.bib}

\makeatletter
\def\blx@refpatch@chapter#1{%
  \ifundef\chapter
    {\blx@err@nodocdiv{chapter}}
    {\pretocmd\@makechapterhead{#1}
       {}
       {\blx@err@patch{\string\@makechapterhead}}
     \pretocmd\@makeschapterhead{#1}
       {}
       {\blx@err@patch{\string\@makeschapterhead}}}}
\makeatother

\begin{document}
\chapter{Ch1}
\nocite{sigfridsson}
\printbibliography
\chapter{Ch2}
\nocite{worman}
\printbibliography
\end{document}
moewew commented 5 years ago

@komascript Can I bother you about this again?

I had another look at the code again, but did not manage to find a hook for our purposes. Patching \@makechapterhead and \@makeschapterhead still works, but I don't know if it is considered safe or OK from the KOMA side. At the moment and purely from gut instinct I would prefer patching \scr@makechapterhead, but I would like to hear your opinion on that matter before I do anything.

komascript commented 5 years ago

@komascript Can I bother you about this again?

I have not forgotten this. But currently it was hard to find time to work on it. There were (and still are) problems with new versions of other packages too and I had to made a new bugfix release of KOMA-Script itself.

Patching \@makechapterhead and \@makeschapterhead still works

Indeed, but this would not patch other headings made with headings style chapter, e.g., if someone uses \RedeclareSectionCommand[style=chapter]{section} to make \section starts a new page.

So patching \scr@makechapterhead and \scr@makeschapterhead or \scr@@makechapterhead and \scr@@makeschapterhead would be better if they exist (and if you need a patch before the initial \clear(double)page even \scr@startchapter). But as KOMA-Script author I cannot recommend it, because I cannot promise that patching internal macros would be functional forever.

So best would be to have a hook and to use a patch only if the hook does not exist.

AFAIR the problem is, that you have to add labels automatically?

Can you tell me, where in the execution of \chapter (and \part) you would need to add code and whether or not such code could influence the page or paragraph breaking?

BTW: Depending whether or not you need the page information of the labels, even the hook of \At@startsection could be wrong for \section etc. because the code execution is before the initial vertical skip and so could be before the page break.

moewew commented 5 years ago

We want to be able to execute some initialisation at the beginning of chapters (or sections, ...) in such a way that the code has been executed when the first text-bit (heading or paragraph or ...?) that belongs to the new chapter/section is processed.

At the moment we need this for the three options refsection, refsegment and citereset. Two of those (refsection via \newrefsection and refsegment via \newrefsegment) indeed set \labels, but those labels are never used and I suspect they are only a clever way to provoke a LaTeX rerun warning in case the commands are moved. The page number is not relevant and the reference the label would return is also irrelevant as long as things settle down after enough LaTeX runs. Of course it would feel nicer if the code was executed on the page where the new chapter/section starts (and would return the chapter number), but that is not a requirement as far as I can tell (nor do I have high hopes that everything that 'feels nice' be achieved at the same time).

The code we insert should only do internal biblatex housekeeping. In particular it should not influence any parameters for page and paragraph breaking or produce visible output itself. (Of course the commands do influence output by biblatex commands later on, which could change the text in a paragraph, but that I guess that is not what you intended with your question.)

komascript commented 5 years ago

I've added a complete new hook mechanism to KOMA-Script, that can be used. Here is an example of the usage:

\documentclass{scrbook}

\newcommand*{\typeoutandshow}[2]{\typeout{TRACE: heading/#1 #2
    \ifx\IfUseNumber\UseNumberUsageError\else
    \IfUseNumber{with}{without} number\fi}}
\AddtoDoHook{heading/begingroup}{\typeoutandshow{begingroup}}
\AddtoDoHook{heading/endgroup}{\typeoutandshow{endgroup}}

\usepackage{blindtext}

\begin{document}
\tableofcontents
\blinddocument

But be careful, nobody should add an additional hook execution:
\ExecuteDoHook{heading/begingroup/section/fake}
\end{document}

with the result:

TRACE: heading/begingroup chapter without number
TRACE: heading/endgroup chapter without number
TRACE: heading/begingroup chapter with number
TRACE: heading/endgroup chapter with number
TRACE: heading/begingroup section with number
TRACE: heading/endgroup section with number
TRACE: heading/begingroup subsection with number
TRACE: heading/endgroup subsection with number
TRACE: heading/begingroup subsubsection without number
TRACE: heading/endgroup subsubsection without number
TRACE: heading/begingroup paragraph without number
TRACE: heading/endgroup paragraph without number
TRACE: heading/begingroup section with number
TRACE: heading/endgroup section with number
TRACE: heading/begingroup subsection with number
TRACE: heading/endgroup subsection with number
TRACE: heading/begingroup subsubsection without number
TRACE: heading/endgroup subsubsection without number
TRACE: heading/begingroup subsection with number
TRACE: heading/endgroup subsection with number
TRACE: heading/begingroup subsubsection without number
TRACE: heading/endgroup subsubsection without number
TRACE: heading/begingroup subsection with number
TRACE: heading/endgroup subsection with number
TRACE: heading/begingroup subsubsection without number
TRACE: heading/endgroup subsubsection without number
TRACE: heading/begingroup section/fake 

I do not really understand your code in biblatex.sty, but I hope, this will help you. Sorry, I urgently need some sleep now.

You can find some documentation of the new mechanism in scrkernel-basics.dtx or in German at https://komascript.de/node/2225

moewew commented 5 years ago

Thanks. I'll have a look and report back. Good night!

moewew commented 5 years ago

@komascript The new hook mechanism looks really promising. Thank you very much. I played around with v3.27.3072 from the SourceForge SVN. Do you expect any interface changes to what you have implemented so far? Or is the bit described in https://komascript.de/node/2225 so stable that we don't have to allow for a buffer before merging the changes at our end?

https://github.com/moewew/biblatex/commit/a189dadd55dcdbee3585443371feb6076fa856a4 in https://github.com/plk/biblatex/compare/dev...moewew:komachap is a first attempt to use the new hooks for our purposes. heading/begingroup/... seemed to give good results.

komascript commented 5 years ago

@moewew The mechanism is currently two days old. I hope that the command \AddtoDoHook and hook heading/begingroup/... are stable. But I cannot promise. Sometimes ideas seem to be good at the beginning but are not.

I would remove the c in \blx@ifkoma@AddtoDocHook.

moewew commented 5 years ago

Ah, thanks for catching that typo. At some point I started to call the command AddtoDocHook in my head, I only noticed that when I tried to use the command and it turned out it was undefined. I must have missed \blx@ifkoma@AddtoDocHook in the renaming process.

I guess I'll wait for bit before I merge the change, a new release of biblatex is probably not going to happen in the near future, so there is no need to rush things and we can see how the hooks develop on your side.


Link back to relevant https://komascript.de/node/2168

moewew commented 5 years ago

The pull request with the changes already discussed is at https://github.com/plk/biblatex/pull/887. We can merge it when KOMA-Script 3.27 with the hooks is released or shortly before the 3.13 gets ready (even if we then only anticipate the KOMA-Script development).

moewew commented 5 years ago

We're hoping to release biblatex 3.13 soon-ish (barring disaster) and so I merged #887 even though the KOMA Script version with the hooks hasn't been released yet. The code should work OK-ish even without the hooks, so we should be fine (fingers crossed).

moewew commented 5 years ago

biblatex 3.13 with the necessary changes has just been released. The MWE now produces the expected result with v3.26b of KOMA-Script and will hopefully use the correct hooks come v3.27.