# Appending “Chapter #:” to numbered chapters but not unnumbered ones in a bibliography by section

I use the following code to general a bibliography-by-section in a 'book' document, but this appends "Chapter #:" to all chapters, including unnumbered ones. How can I modify the code to only append it to numbered chapters?

MWE:

\documentclass{book}

\usepackage{lipsum}
\usepackage[backend=biber,
natbib=true,
style=authoryear]{biblatex}
\usepackage{nameref} % For ref. by segment

\section*{Chapter \ref{refsegment:\therefsection\therefsegment}:
\nameref{refsegment:\therefsection\therefsegment}}
}

\begin{document}

\chapter{Numbered chapter}
\setcounter{chapter}{1}
\begin{refsegment}
\lipsum[1]
\cite{glashow,weinberg}
\end{refsegment}

\chapter*{Unnumbered}
\begin{refsegment}
\lipsum[2]
\cite{reese}
\end{refsegment}

\end{document}

• The most easy and obvious workaround would be to omit the string chapter including the number from the definition ;-). – Johannes_B Nov 13 '14 at 9:32
• Thanks, but I've already thought of that ;) I'd rather include the string, if possible without hacking the code to absurd lengths. – ashaels Nov 13 '14 at 13:46

If you load the hyperref package, then information about whether the label is for a numbered or unnumbered chapter is written to in the .aux file. You can then access this via the fourth element of the result of \real@setref and provide appropriate text.

For numbered chapters this fourth element is chapter.7 for Chapter 7, for unnumbered chapters it is of the form chapter*.1. You can use the xstring package to check for the existence of the substring *.

Here is a macro \mychref that prints the type of heading text you wish for depending whether the chapter is numbered or not.

\makeatletter
\newcommand*{\mychref}[1]{\IfSubStr{%
\expandafter\real@setref\csname r@#1\endcsname\@fourthoffive{#1}%
}{*.}{}{Chapter \ref{#1}: }\nameref{#1}}
\makeatother


\documentclass{book}

\usepackage{lipsum}
\usepackage[backend=biber,
natbib=true,
style=authoryear]{biblatex}
\usepackage{xstring,nameref,hyperref}

\makeatletter
\newcommand*{\mychref}[1]{\IfSubStr{%
\expandafter\real@setref\csname r@#1\endcsname\@fourthoffive{#1}%
}{*.}{}{Chapter \ref{#1}: }\nameref{#1}}
\makeatother

\section*{\mychref{refsegment:\therefsection\therefsegment}}
}

\begin{document}

\chapter{Numbered chapter}
\setcounter{chapter}{1}
\begin{refsegment}
\lipsum[1]
\cite{glashow,weinberg}
\end{refsegment}

\chapter*{Unnumbered}
\begin{refsegment}
\lipsum[2]
\cite{reese}
\end{refsegment}