[Tex/LaTex] Table spacing issue converting to PDF via LaTeX with pandoc

knitrpandocrtables

In the application of Rmarkdown I often found my answers concerning “LaTeX” and “pandoc” here at TeX Stackexchange. For that reason I want to repeat here my question I asked at stackoverflow hoping to find a solution to following issue while converting to PDF via LaTeX with pandoc:

I'm facing the issue that in R texreg() doesn't seem to break the lines in the notes below the table. Also the margins between the models are not extended equally like it's happening with htmlreg() which gives a clean result. Manual breaks like \n or HTML tags are not working with 'texreg'. There's a partly workaround from MichaelChirico, but one has to change the texreg() function but it seems the method doesn't respect the case when we're dealing with more than one model side by side.

Here is the code I'm using:

---
title: "Untitled"
author: "jaySf"
date: "13 Juni 2017"
output:
  pdf_document: default
  html_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r results="asis"}
library(car)
lm1 <- lm(prestige ~ income, data=Duncan)
lm2 <- lm(prestige ~ education, data=Duncan)
lm3 <- lm(prestige ~ income + education, data=Duncan)

library(texreg)
texreg(list(lm1, lm2, lm3), custom.note = "Lorem ipsum dolor sit amet, <br>consetetur sadipscing 
       elitr, sed diam. %stars.") # for pdf output

htmlreg(list(lm1, lm2, lm3), custom.note = "Lorem ipsum dolor sit amet, <br>consetetur sadipscing 
       elitr, sed diam. %stars.") # for html output
```

And this is the output:

ugly table

Does anybody have a clue? Any help is appreciated.

And here the LaTeX code which is produced:

\documentclass[]{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}
\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=1in]{geometry}
\usepackage{hyperref}
\hypersetup{unicode=true,
            pdftitle={Untitled},
            pdfauthor={jaySf},
            pdfborder={0 0 0},
            breaklinks=true}
\urlstyle{same}  % don't use monospace font for urls
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{{#1}}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{{#1}}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{{#1}}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{{#1}}}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{{#1}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{{#1}}}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{{#1}}}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{{#1}}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{{#1}}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{{#1}}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{{#1}}}
\newcommand{\ImportTok}[1]{{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{{#1}}}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{{#1}}}}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{{#1}}}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{{#1}}}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{{#1}}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{{#1}}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{{#1}}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{{#1}}}}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{{#1}}}}
\newcommand{\BuiltInTok}[1]{{#1}}
\newcommand{\ExtensionTok}[1]{{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{{#1}}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{{#1}}}
\newcommand{\RegionMarkerTok}[1]{{#1}}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{{#1}}}}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{{#1}}}}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{{#1}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{{#1}}}}
\newcommand{\NormalTok}[1]{{#1}}
\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}
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\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}
% 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

%%% 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{Untitled}
  \pretitle{\vspace{\droptitle}\centering\huge}
  \posttitle{\par}
  \author{jaySf}
  \preauthor{\centering\large\emph}
  \postauthor{\par}
  \predate{\centering\large\emph}
  \postdate{\par}
  \date{13 Juni 2017}


\begin{document}
\maketitle

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(car)}
\NormalTok{lm1 <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(prestige ~}\StringTok{ }\NormalTok{income, }\DataTypeTok{data=}\NormalTok{Duncan)}
\NormalTok{lm2 <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(prestige ~}\StringTok{ }\NormalTok{education, }\DataTypeTok{data=}\NormalTok{Duncan)}
\NormalTok{lm3 <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(prestige ~}\StringTok{ }\NormalTok{income +}\StringTok{ }\NormalTok{education, }\DataTypeTok{data=}\NormalTok{Duncan)}

\KeywordTok{library}\NormalTok{(texreg)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Version:  1.36.23
## Date:     2017-03-03
## Author:   Philip Leifeld (University of Glasgow)
## 
## Please cite the JSS article in your publications -- see citation("texreg").
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{texreg}\NormalTok{(}\KeywordTok{list}\NormalTok{(lm1, lm2, lm3), }\DataTypeTok{custom.note =} \StringTok{"Lorem ipsum dolor sit amet, <br>consetetur sadipscing }
\StringTok{       elitr, sed diam. %stars."}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{table}
\begin{center}
\begin{tabular}{l c c c }
\hline
 & Model 1 & Model 2 & Model 3 \\
\hline
(Intercept) & $2.46$       & $0.28$       & $-6.06$      \\
            & $(5.19)$     & $(5.09)$     & $(4.27)$     \\
income      & $1.08^{***}$ &              & $0.60^{***}$ \\
            & $(0.11)$     &              & $(0.12)$     \\
education   &              & $0.90^{***}$ & $0.55^{***}$ \\
            &              & $(0.08)$     & $(0.10)$     \\
\hline
R$^2$       & 0.70         & 0.73         & 0.83         \\
Adj. R$^2$  & 0.69         & 0.72         & 0.82         \\
Num. obs.   & 45           & 45           & 45           \\
RMSE        & 17.40        & 16.69        & 13.37        \\
\hline
\multicolumn{4}{l}{\scriptsize{Lorem ipsum dolor sit amet, <br>consetetur sadipscing 
       elitr, sed diam. $^{***}p<0.001$, $^{**}p<0.01$, $^*p<0.05$.}}
\end{tabular}
\caption{Statistical models}
\label{table:coefficients}
\end{center}
\end{table}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{htmlreg}\NormalTok{(}\KeywordTok{list}\NormalTok{(lm1, lm2, lm3), }\DataTypeTok{custom.note =} \StringTok{"Lorem ipsum dolor sit amet, <br>consetetur sadipscing }
\StringTok{       elitr, sed diam. %stars."}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

Statistical models

Model 1

Model 2

Model 3

(Intercept)

2.46

0.28

-6.06

(5.19)

(5.09)

(4.27)

income

1.08***

0.60***

(0.11)

(0.12)

education

0.90***

0.55***

(0.08)

(0.10)

R2

0.70

0.73

0.83

Adj. R2

0.69

0.72

0.82

Num. obs.

45

45

45

RMSE

17.40

16.69

13.37

{Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam.
\emph{\textbf{p \textless{} 0.001, }p \textless{} 0.01, }p \textless{}
0.05.}


\end{document}

Best Answer

Note that you custom note end in a \multicolumn, so you can not use line break comands as \par or \\, but you can use a \parbox:

custom.note = "\\parbox{\\linewidth}{Lorem ipsum dolor sit amet, 
\\\\ consetetur sadipscing    elitr, sed diam. %stars.}"

mwe

You can also allow an automatic line break in a narrow \parbox, thus avoiding the excessive width of the last column:

custom.note = "\\parbox{.4\\linewidth}{Lorem ipsum dolor sit amet,
consetetur sadipscing elitr, sed diam. %stars.}"

mwe