1

I'm working on many papers with a few header files that do important stuff (define functions etc.). I'd like to create a new "header" (or: "footer") file that contains this:

\clearpage
\section{Sources}
\printbibliography
\clearpage

But some of these documents have no sources cited and there's an empty page added then.

Is there any way to do this?

\if(numberOfUsedSources > 1) {
    \clearpage
    \section{Sources}
    \printbibliography
    \clearpage
}

It would be really helpful!

Stackerli
  • 11
  • 2
  • Related: https://tex.stackexchange.com/questions/163451/total-number-of-citations, combine with, e.g., the ifnum example in the totcount documentation (Section 6). – Marijn Dec 05 '17 at 13:02

2 Answers2

3

You could simply use \printbibliography[heading=bibintoc,title={Sources}]. This should only print anything if there are cited entries, and will add a section titled "Sources" in that case. In case the documentclass uses chapters (e.g., book) you should use subbibintoc instead.

As, lacking a MWE, we don't know more details of your overall settings, I can only guess that, probably, the clearpages can be handled without the conditional, given the appearing/non appearing bibliography.

Update: Somewhat more elaborate, and handling the \clearpages as well. In full:

\documentclass{article}
\usepackage{biblatex}
\usepackage{kantlipsum}

\addbibresource{biblatex-examples.bib}

\defbibheading{myheading}[Sources]{%
    \clearpage%
    \section{#1}}

\defbibnote{mypostnote}{\clearpage}

\begin{document}

\section{Section 1}
\kant[1-4]

\nocite{*}
\printbibliography[heading=myheading,postnote=mypostnote]

\kant[1-4]

\newrefsection
\section{Section 2}

\kant[1-4]

\printbibliography[heading=myheading,postnote=mypostnote]

\kant[1-4]

\end{document}
gusbrs
  • 13,740
1

When asking a question it's always good to include a minimal working example so that people know what \documentclass etc you are using. A MWE also makes it easier to answer your question as it frequently helps clarify what the issues are-- and it gives people working code to start from.

There may be a standard biblatex way of doing this but, failing that, I suggest creating a new boolean, say \ifCiting, and then redefining \cite so that it first sets \Citingtrue and then uses the real \cite command. You can then use \ifCiting...\fi to print your bibliography section, or not.

Here's one way to do this:

\documentclass{article}
\usepackage{biblatex}

\let\realCite\cite% save the orginal \cite macro as \realCite for future use
\newif\ifCiting\Citingfalse% no citations by default
\renewcommand\cite[2][]{% optional argument is empty by default
  \Citingtrue% we have at least one citation!
  \if\relax\detokenize{#1}\relax% no optional argument
    \realCite{#2}%
  \else% an optional argument
    \realCite[#1]{#2}%
  \fi%
}

\begin{document}

\ifCiting
    \clearpage
    \section{Sources}
    \printbibliography
    \clearpage
\fi

\end{document}

This only tricky bit is in checking to see whether or not the optional argument to \cite is "empty". The standard way of doing this is the line \if\relax\detokenize{#1}\relax.

Edit

As Skillmon pointed out in the comments, a much more efficient (and "globally" correct) solution is:

\documentclass{article}
\usepackage{biblatex}

\let\realCite\cite% save the orginal \cite macro for future use
\newif\ifCiting\Citingfalse% no citations by default
\renewcommand*\cite{\global\Citingtrue\realCite}

\begin{document}

\ifCiting
    \clearpage
    \section{Sources}
    \printbibliography
    \clearpage
\fi

\end{document}
  • 1
    Why not use something like: \let\realCite\cite\renewcommand*\cite{\global\Citingtrue\realCite}? – Skillmon Dec 05 '17 at 13:21
  • @Skillmon Yes, that's much better on two counts - the addition of \global and much more succinct! I'll edit. –  Dec 05 '17 at 13:34
  • The only thing I'm not sure about atm is, whether to use \let or \LetLtxMacro. – Skillmon Dec 05 '17 at 13:37
  • @Skillmon In this instance, \let seems to work fine. –  Dec 05 '17 at 13:40