latex3 / hyperref

Hypertext support for LaTeX
165 stars 35 forks source link

backref adds 'writefilebrfwritefilebrfwritefilebrfwritefilebrfwritefilebrf' before first reference #313

Closed beausoleilmo closed 10 months ago

beausoleilmo commented 10 months ago

I wanted to use the backref option in hyperref package. But when using it, I noticed weird behaviours. First, when I add a reference in the document, it is seen in the document but a writefilebrfwritefilebrfwritefilebrfwritefilebrfwritefilebrf preceding it:

YzyKD

This is when I Pdflatex + BibTex + Pdflatex + Pdflatex

I'm not sure what is causing the problem so I'm pasting a simple example, but with all packages.

Second, if I uncomment the

\chapter*{\normalfont\huge\bfseries Bibliography} % Add title to bibliography 

I get this error:

    Chapter 1.
    (./bu1.aux)
    No file bul.bbl.

    [1{/usr/local/texlive/2023/texmf-var/fonts/map/pdftex/updmap/
    pdftex.map}{/usr/l
    ocal/texlive/2023/texmf-dist/fonts/enc/dvips/base/8r.enc}][2](./
    bul.aux
    •/bu1. aux:7: You can't use "\spacefactor' in vertical mode.
    1@->\spacefactor
    1.7 \Q
    writefile{brf}{\backcite{goossens93}{{1}{1.1}{section.1.1}}}
    ?

I use TeX, Version 3.141592653 (TeX Live 2023) (macOS)

This is a reproducible example


    \documentclass[12pt,oneside]{report} 

    \usepackage{filecontents}

    \begin{filecontents*}{mycitations.bib}
    @book{goossens93,
        author = "Frank Mittelbach and Michel Goossens  and Johannes Braams and David Carlisle  and Chris Rowley",
        title = "The {LaTeX} Companion",
        year = "1993",
        publisher = "Addison-Wesley",
        address = "Reading, Massachusetts"
    }
    @article{fujita2010economic,
        title={Economic effects of the unemployment insurance benefit},
        author={Fujita, Shigeru},
        journal={FRB Phil. Business Review},
        volume={4},
        year={2010}
    }
    @article{rothstein2011unemployment,
        title={Unemployment insurance and job search in the {Great Recession}},
        author={Rothstein, Jesse},
        journal={NBER},
        volume={w17534},
        year={2011}
    }
    @article{test,
        title={sup dude},
        author={Me, You},
        journal={Greatest Journal},
        volume={inf},
        year={2156}
    }

    \end{filecontents*}

    \usepackage[top=1truein,bottom=1truein,left=1truein,right=1truein, showframe]{geometry}
    \usepackage[hidelinks, linktoc=all, pdftex, pdfpagelabels=false, backref=page]{hyperref}
    \usepackage[bf,rm,medium,compact, explicit]{titlesec}
    \titleformat{\chapter}[display]
    {\normalfont\LARGE\bfseries\raggedright}{\chaptertitlename\ \thechapter}{20pt}{\huge #1}
    \titleformat{name = \chapter, numberless}
      {\normalfont\huge\bfseries\color{black}}
      {}
      {0em}
      {#1}%
    \titlespacing*{name = \chapter, numberless}{0pt}{50pt}{40pt}
    \usepackage{indentfirst}
    \usepackage{makeidx}
    \usepackage[acronym]{glossaries}
    \usepackage[automake]{glossaries-extra}
    \usepackage[round]{natbib}
    \usepackage[sectionbib,globalcitecopy]{bibunits}
    \defaultbibliographystyle{apalike} 
    \defaultbibliography{mycitations.bib}
    \def\urlprefix{}

    \setlength{\parindent}{3em} % indentation length =3em ~1.05cm
    \setlength{\parskip}{0pt} % Vertical space between paragraphs
    \DeclareMathOperator{\pdf}{pdf}
    \DeclareMathOperator{\average}{average}
    \newcommand*{\tabindent}{ \hspace{5.6mm}} 

    \renewcommand{\bibpreamble}{%
      [Citing pages are listed after each reference.]%
      \par\bigskip}

    \usepackage{etoolbox}
    \makeatletter
    \patchcmd{\BR@backref}{\newblock}{\newblock[}{}{}
    \patchcmd{\BR@backref}{\par}{]\par}{}{}
    \makeatother

    \makeglossaries

    \begin{document}
    \bibliographyunit[\chapter]

    \chapter{title}\label{ch:test}
    \section{Introduction}
    this is a test \cite{goossens93}. for citations \citep{fujita2010economic},  \citep{rothstein2011unemployment}.
    \cite{goossens93}
    \phantomsection
    \addcontentsline{toc}{section}{\protect\numberline{\thesection}Bibliography}

    \putbib

    \cleardoublepage

    \cite{test}
    \cleardoublepage

    \addtocounter{chapter}{1}
    \phantomsection % <- added
    %\chapter*{\normalfont\huge\bfseries Bibliography} % Add title to bibliography 
    \addcontentsline{toc}{chapter}{Bibliography} % Add to toc 
    %\renewcommand{\bibsection}{}

    \bibliographystyle{apalike}
    \bibliography{mycitations}

    \end{document}
u-fischer commented 10 months ago

That is not a hyperref/backref issue. bibunits reads in the aux-file with the wrong catcode setting. Try

  \makeatletter
  \def\@startbibunit{%
  \global\let\@startbibunitorrelax\relax
  \global\let\@finishbibunit\@finishstartedbibunit
  \global\advance\@bibunitauxcnt 1
  \if@filesw
    {\endlinechar-1 
     \makeatletter %new
    \@input{\@bibunitname.aux}}%
    \immediate\openout\@bibunitaux\@bibunitname.aux
    \immediate\write\@bibunitaux{\string\bibstyle{\@localbibstyle}}%
  \fi
}
 \makeatletter 
beausoleilmo commented 10 months ago

Thanks for the fast reply! This indeed solved the first part of the problem! However, I'm still struggling to get the

\chapter*{\normalfont\huge\bfseries Bibliography} % Add title to bibliography 
\addcontentsline{toc}{chapter}{Bibliography} % Add to toc 
\renewcommand{\bibsection}{}

part of the script to work. Basically, if you 'uncomment' the \chapter* and \renewcommand, you'll probably get the error

Chapter 1.
(./bu1.aux)
No file bu1.bbl.
[1{/usr/local/texlive/2023/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
[2] (./bu1.aux
./bu1.aux:7: You can't use `\spacefactor' in vertical mode.
\@->\spacefactor 
                 \@m {}
l.7 \@
      writefile{brf}{\backcite{goossens93}{{1}{1.1}{section.1.1}}}
? ./bu1.aux:7: Missing $ inserted.
<inserted text> 
                $
l.7 \@
      writefile{brf}{\backcite{goossens93}{{1}{1.1}{section.1.1}}}
? 

What is causing this and how to resolve it?

u-fischer commented 10 months ago

This here is not a bibunits support site but the hyperref issue tracker. And the problem is the same: the aux is read in another place with the wrong catcode.