0

I need section titles to be uppercase. I've done this with sectsty, but have recently discovered an issue that it causes. In the table of contents, clicking on "References" does not bring me to the section, and there's the following warning:

pdfTeX warning (dest): name{section*.2} has been referenced but does not exist, replaced by a fixed one

For the LoF I can use \phantomsection and \addcontentsline, but when the bibliography is empty \printbibliography doesn't output a section, which would break this method (unless there's way of detecting if a \printbibliography command outputs nothing).

MWE:

\documentclass[12pt]{article}

\usepackage{float}
\usepackage{csquotes}
\usepackage{biblatex}
\usepackage[nottoc]{tocbibind}
\usepackage{hyperref}
\usepackage{sectsty}
    \allsectionsfont{\fontsize{12pt}{12pt}\selectfont}
    \sectionfont{\fontsize{12pt}{12pt}\selectfont\scshape\MakeUppercase} % Removing this fixes the problem

\begin{filecontents}[overwrite]{\jobname.bib}
@book{2people,
    author = {John Smith1 and Alex Smith2},
    title = {Some Other Thing},
    date = {1971}
}
\end{filecontents}
\addbibresource{\jobname.bib}

\begin{document}

\tableofcontents
\clearpage\section{First}
\begin{figure}[H]\caption{Caption \parencite{2people}}\end{figure}
\clearpage\printbibliography[heading=bibintoc]
\clearpage\listoffigures
%\cleardoublepage % Using these instead of tocbibind fixes the LoF link in the ToC
%\phantomsection\addcontentsline{toc}{section}{\listfigurename}
%\listoffigures

\end{document}
  • 1
    It won't work. You are putting the \MakeUppercase around the whole argument, and inside \MakeUppercase hyperref doesn't set an anchor. – Ulrike Fischer Jul 7 at 13:48
1

As Ulrike Fischer explained in the comments the \MakeUppercase at the end of \sectionfont applies to its complete argument and effectively disables hyperref's anchor. That's why you need manual \phantomsections.

You could define a new heading type mybibintoc

\makeatletter
\defbibheading{mybibintoc}[\refname]{%
  \section*{#1}%
  \phantomsection
  \addcontentsline{toc}{section}{#1}%
  \@mkboth{\abx@MakeMarkcase{#1}}{\abx@MakeMarkcase{#1}}}
\makeatother

and use that \printbibliography[heading=mybibintoc].

This redefines the bibliography heading to automatically include \phantomsection. With that definition the link anchor will point to after the heading though, which you may find undesirable.

You could switch the order round a bit and move the \section*{#1}% to after \addcontentsline, but then the label could again attach to the wrong thing. If you are starting a new page before the bibliography anyway, the safest would be to include the \clearpage in the heading definition. Then the anchor is always good.

\makeatletter
\defbibheading{mybibintoc}[\refname]{%
  \clearpage
  \phantomsection
  \addcontentsline{toc}{section}{#1}%
  \section*{#1}%
  \@mkboth{\abx@MakeMarkcase{#1}}{\abx@MakeMarkcase{#1}}}
\makeatother

MWE

\documentclass[12pt]{article}

\usepackage{float}
\usepackage{csquotes}
\usepackage{biblatex}
\usepackage[nottoc]{tocbibind}
\usepackage{hyperref}
\usepackage{sectsty}
\allsectionsfont{\fontsize{12pt}{12pt}\selectfont}
\sectionfont{\fontsize{12pt}{12pt}\selectfont\scshape\MakeUppercase} % Removing this fixes the problem

\addbibresource{biblatex-examples.bib}

\makeatletter
\defbibheading{mybibintoc}[\refname]{%
  \clearpage
  \phantomsection
  \addcontentsline{toc}{section}{#1}%
  \section*{#1}%
  \@mkboth{\abx@MakeMarkcase{#1}}{\abx@MakeMarkcase{#1}}}
\makeatother

\usepackage{lipsum}
\newcommand*{\lorem}{lorem ipsum dolor sit amet}
\newcommand*{\lllorem}{\lorem\ \lorem\ \lorem}

\begin{document}

\tableofcontents
\clearpage\section{First}
\begin{figure}[H]\caption{Caption \parencite{sigfridsson}}\end{figure}
%\clearpage\section*{Second}\addcontentsline{toc}{section}{Second}
\lipsum[1-3] 
\lllorem\ \lllorem\ \lllorem\ Lorem.
\printbibliography [heading=mybibintoc]
%\clearpage\listoffigures
%\cleardoublepage % Using these instead of tocbibind fixes the LoF link in the ToC
%\phantomsection\addcontentsline{toc}{section}{\listfigurename}
%\listoffigures

\end{document}
| improve this answer | |
  • You're using just \clearpage, while in tex.stackexchange.com/a/48511 \cleardoublepage is used. Is there a difference when used in a single-sided document? – TakingItCasual Jul 8 at 6:12
  • @TakingItCasual Mhh, I didn't think about \cleardoublepage. I just took what you had in the MWE. In a onesided document there is no difference between \clearpage and \cleardoublepage, though. In a twosided document the question is if you want the new page to start on an odd page (when you would use \cleardoublepage) or if you don't care about odd or even (use \clearpage). ... – moewe Jul 8 at 6:25
  • ... It is only imperative to use \cleardoublepage if the sectioning command would issue it itself. If you had a two-sided book and would typeset the heading with \chapter* you could get a bad link (to the empty page) with \clearpage if the previous chapter ends on an odd page, there you need \cleardoublepage. But in your example you always force page breaks manually anyway, so it shouldn't matter. – moewe Jul 8 at 6:27
0

An alternative way of capitalizing the section titles that doesn't disrupt hyperref can be used:

\ExplSyntaxOn
\let\@oldsection\section
\RenewDocumentCommand{\section}{ s o m }{
    % \text_uppercase will replace \tl_upper_case in later l3token versions
    \tl_set:Nx\@section_upper{\cs_if_exist:NTF{\text_uppercase:n}
        {\text_uppercase:n{#3}}{\tl_upper_case:n{#3}}}
    \IfBooleanTF{#1}
        {\@oldsection*{\@section_upper}}
        {\IfNoValueTF{#2}
            {\@oldsection{\@section_upper}}
            {\@oldsection[#2]{\@section_upper}}}}
\ExplSyntaxOff

At least for the article class, you can style the section titles directly without sectsty. MWE:

\documentclass[12pt]{article}

\usepackage{etoolbox}
\usepackage{xparse}
\usepackage{float}
%\usepackage{csquotes}
\usepackage{biblatex}
\usepackage[nottoc]{tocbibind}
\usepackage{hyperref}

\makeatletter
% Make section/subsection text size same as normal text
\patchcmd{\section}{\Large}{\normalsize}{}{}
\patchcmd{\subsection}{\large}{\normalsize}{}{}
\ExplSyntaxOn
% Capitalize section titles
\let\@oldsection\section
\RenewDocumentCommand{\section}{ s o m }{
    % \text_uppercase will replace \tl_upper_case in later l3token versions
    \tl_set:Nx\@section_upper{\cs_if_exist:NTF{\text_uppercase:n}
        {\text_uppercase:n{#3}}{\tl_upper_case:n{#3}}}
    \IfBooleanTF{#1}
        {\@oldsection*{\@section_upper}}
        {\IfNoValueTF{#2}
            {\@oldsection{\@section_upper}}
            {\@oldsection[#2]{\@section_upper}}}}
\ExplSyntaxOff

% Capitalize section titles in ToC (/a/156917)
\let\@oldcontentsline\contentsline
\def\contentsline#1#2{%
    \expandafter\ifx\csname l@#1\endcsname\l@section
        \expandafter\@firstoftwo
    \else
        \expandafter\@secondoftwo
    \fi
    {\@oldcontentsline{#1}{\MakeUppercase{#2}}}%
    {\@oldcontentsline{#1}{#2}}%
}
\makeatother

\begin{filecontents}[overwrite]{\jobname.bib}
@book{2people,
    author = {John Smith1 and Alex Smith2},
    title = {Some Other Thing},
    date = {1971}
}
\end{filecontents}
\addbibresource{\jobname.bib}

\begin{document}

\tableofcontents
\clearpage\section{First Section}
\begin{figure}[H]\caption{caption text \parencite{2people}}\end{figure}
\clearpage\printbibliography[heading=bibintoc]
\clearpage\listoffigures

\end{document}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.