7

Update

I rewrote the post providing a MWE according to the suggestions by the comments below.

Problem

When generating the list of references at the end of my paper, the PDF output has two issues which I can't solve.

1) Links overflow page border but should get a page break at the dash.

2) Bibliography items are split across pages but should stay as one block.

issue

My Document

I'm using RMarkdown with Pandoc to generate a PDF based on http://rmarkdown.rstudio.com/authoring_pandoc_markdown.html#citations

According to the documentation, the list of references is placed at the end of the document. So, I don't have anything like \printbibliography or so.

Using 'knit pdf' in RStudio seems to execute:

/Applications/RStudio.app/Contents/MacOS/pandoc/pandoc +RTS -K512m -RTS MWE.utf8.md 
--to latex 
--from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash 
--output MWE.tex 
--template /usr/local/lib/R/3.3/site-library/rmarkdown/rmd/latex/default-1.15.2.tex 
--highlight-style tango 
--latex-engine /Library/TeX/texbin/xelatex 
--variable graphics=yes 
--bibliography library.bib 
--filter /Applications/RStudio.app/Contents/MacOS/pandoc/pandoc-citeproc

MWE.Rmd

---
output: 
  pdf_document:
    latex_engine: xelatex
    keep_tex: true
bibliography: library.bib
fontsize: 12pt
geometry:
  margin = 1.2in,
  paper = a4paper,
  inner = 1.5cm,
  bindingoffset = 2cm,
  outer = 2cm,
  top = 2.5cm,
  bottom = 2.5cm
header-includes:
- \usepackage{setspace}
- \usepackage{lipsum}
- \onehalfspacing
---

#  Content

@Boner2015 states that...

I need another line...

And another...

\lipsum[1-3]

# References

\setlength{\parindent}{-0.2in}
\setlength{\leftskip}{0.2in}
\setlength{\parskip}{8pt}
\noindent

library.bib

@misc{Boner2015,
author = {Bon{\'{e}}r, Jonas},
title = {{Interview with Tony Rae Moore: Typesafe AMA Podcast Ep. 03 feat. How Akka Actors Help Build Microservices with Jonas Bon{\'{e}}r}},
url = {https://www.lightbend.com/blog/typesafe-ama-podcast-ep-03-feat-akka-actors-microservices-jonas-boner},
urldate = {2016-11-22},
year = {2015}
}

Generated MWE.tex

\documentclass[12pt,]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
\else % if luatex or xelatex
  \ifxetex
    \usepackage{mathspec}
  \else
    \usepackage{fontspec}
  \fi
  \defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
  \newcommand{\euro}{â¬}
\fi
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\usepackage{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\usepackage[margin = 1.2in, paper = a4paper, inner = 1.5cm, bindingoffset = 2cm,
outer = 2cm, top = 2.5cm, bottom = 2.5cm]{geometry}
\usepackage{hyperref}
\PassOptionsToPackage{usenames,dvipsnames}{color} % color is loaded by hyperref
\hypersetup{unicode=true,
            pdfborder={0 0 0},
            breaklinks=true}
\urlstyle{same}  % don't use monospace font for urls
\usepackage{graphicx,grffile}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
\setlength{\emergencystretch}{3em}  % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{0}

%%% Use protect on footnotes to avoid problems with footnotes in titles
\let\rmarkdownfootnote\footnote%
\def\footnote{\protect\rmarkdownfootnote}

%%% Change title format to be more compact
\usepackage{titling}

% Create subtitle command for use in maketitle
\newcommand{\subtitle}[1]{
  \posttitle{
    \begin{center}\large#1\end{center}
    }
}

\setlength{\droptitle}{-2em}
  \title{}
  \pretitle{\vspace{\droptitle}}
  \posttitle{}
  \author{}
  \preauthor{}\postauthor{}
  \date{}
  \predate{}\postdate{}


\usepackage{setspace}
\usepackage{lipsum}
\onehalfspacing

% Redefines (sub)paragraphs to behave more like sections
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi

\begin{document}

\section{Content}\label{content}

Bonér (2015) states that\ldots{}

I need another line\ldots{}

And another\ldots{}

\lipsum[1-3]

\section{References}\label{references}

\setlength{\parindent}{-0.2in} \setlength{\leftskip}{0.2in}
\setlength{\parskip}{8pt} \noindent

\hypertarget{refs}{}
\hypertarget{ref-Boner2015}{}
Bonér, Jonas. 2015. ``Interview with Tony Rae Moore: Typesafe AMA
Podcast Ep. 03 Feat. How Akka Actors Help Build Microservices with Jonas
Bonér.''
\url{https://www.lightbend.com/blog/typesafe-ama-podcast-ep-03-feat-akka-actors-microservices-jonas-boner}.

\end{document}
  • 1
    Breaking long URLs is a pain, have a look at Line breaks of long URLs in biblatex bibliography? and linked questions. If you want us to have a closer look at your actual problem, please consider showing us the generated LaTeX code in MWE form, see also MWEB. – moewe Nov 25 '16 at 15:00
  • 1
    Ah, can you please check if you really use the package biblatex as your tag suggests? In your code example I find a .csl mentioned which makes me think you are not actually using biblatex at all, but rather some internal Pandoc citation converter. – moewe Nov 25 '16 at 15:03
  • 1
    The package url or hyperref should help with the linebreak if the URL is marked as such in the generated .tex file. But I think .csl issues need to be asked in the Pandoc group because the issue is not the TeX code but how the code is generated in the first place. (And, really, this site would need the generated .tex code to do anything helpful at all.) – jon Nov 25 '16 at 16:32
  • @moewe, thanks for that input - being completely new to Latex, RMarkdown, Pandoc, etc. I wasn't sure what was relevant or could cause a conflict. Anyway, I hope, this MWE is more useful now. I also removed the biblatex tag: I just use a .bib file but not the package... – Toaditoad Nov 25 '16 at 17:04
  • @jon, I will have a look into that. According to the .tex, the url seems marked as \url. – Toaditoad Nov 25 '16 at 17:04
  • Thank you very much for the update, maybe http://tex.stackexchange.com/q/3033/35864, http://tex.stackexchange.com/q/54946/35864 can help you. As I said, URL breaking is hard, so you will have to experiment a bit. – moewe Nov 25 '16 at 17:10
  • You need to let the URLs break at hyphen points (which is questionable, but maybe better than right in the middle of a word, which is also possible). To do that you want to let hyperef load the url package with the option hyphens: \PassOptionsToPackage{hyphens}{url} (put this before loading hyperref). – jon Nov 25 '16 at 17:18
  • @jon, when I place in \PassOptionsToPackage{hyphens}{url} in the header-includes:, the generated .tex already loads the 'hyperref' package before that. How can I specify the order in the YAML? By now, I feel that RMarkdown makes things more complicated than it seemed at the beginning... – Toaditoad Nov 25 '16 at 19:58
  • I'd have to see the YAML. But RMarkdown is doing it badly if you can't load packages before hyperref! This is a package that should often be loaded quite late in the preamble (often last of all). There is a question or two on this site about package loading order. If it is not possible to change the package loading order, perhaps you should point the RMarkdown devs to it. (In my opinion, loading hyperref late should be the default.) – jon Nov 25 '16 at 20:42
  • @jon, the YAML is the part between --- in my MWE.Rmd code block. I can place packages in header-includes: to be loaded in the preamble. When generating the document, it creates an intermediate .tex that is generated based on some tex template what puts my additions after some standard loadings. I don't see any way to specify the order of loading packages in the YAML. So, workaround for now to add \PassOptionsToPackage{hyphens}{url} in front of \usepackage{hyperref} in the default template what lets the urls break in an acceptable way. – Toaditoad Nov 28 '16 at 08:18
  • Regarding the other issue to keep one bibliography item as one block at the same page, I realized that using the Citations Style Language in xml just generates the bib item in plain text without any wrapping container/group resulting in one big paragraph (see at the end of MWE.tex) - Is there any way for Latex to interpret that as separate paragraphs in order to use something like \widowpenalties 1 10000\raggedbottom? – Toaditoad Nov 28 '16 at 09:33
  • Oh, I see. I thought you meant the actual template ... I wasn't paying attention! – jon Nov 28 '16 at 20:20

2 Answers2

5

For issue 1, I added the following under header-includes:

\def\UrlBreaks{\do\/\do-\do?}

It adds additional characters on which the url can break, which can be done while hypperref is already loaded, apparently.

The solution for issue 2 is not doing it for me.

1

Thanks to @jon and @moewe, issue 1 can be resolved by putting \PassOptionsToPackage{hyphens}{url} before loading hyperref (\usepackage{hyperref}). In my case that was not possible in the header-include: of the RMarkdown YAML because it uses some default .tex file where hyperrefis already loaded. For me, it is okay to modify the default .text file.

Regarding issue 2, apparently RMarkdown with Pandoc and CSL generates paragraphs (separated by an empty line) for each bibliography item of the following format

\hypertarget{ref-Boner2015}{}
Bonér, J. (2015). \emph{Interview with Tony Rae Moore: Typesafe AMA
Podcast Ep. 03 feat. How Akka Actors Help Build Microservices with Jonas
Bonér}. {[}Online{]}. Available from:
\url{https://www.lightbend.com/blog/typesafe-ama-podcast-ep-03-feat-akka-actors-microservices-jonas-boner}.
{[}Accessed: 22 November 2016{]}.

Usually, according to How to avoid page-breaks inside paragraphs? this would work:

\widowpenalties 1 10000
\raggedbottom

But, having \hypertarget prevents page breaks (Multiple \hypertarget overflow the page?). Therefore, in addition, I had to place a \leavevmode in front of every \hypertarget resulting in overwriting \hypertarget:

\usepackage{letltxmacro}
\LetLtxMacro{\oldhypertarget}{\hypertarget}
\renewcommand{\hypertarget}[2]{\leavevmode\oldhypertarget{#1}{#2}}