表间距问题通过带有pandoc的LaTeX转换为PDF

时间:2017-06-14 15:55:01

标签: r knitr r-markdown pandoc texreg

我面临的问题是,在R texreg()中似乎没有打破表格下方注释中的界限。此外,模型之间的边距不会像htmlreg()那样同样地扩展,从而产生干净的结果。像\n或HTML标记这样的手动中断不适用于'texreg'。有部分workaround from MichaelChirico,但是必须更改texreg()函数,但是当我们并排处理多个模型时,该方法似乎不尊重这种情况。

以下是我正在使用的代码:

---
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
```

这是输出:

ugly table

有人有线索吗?任何帮助表示赞赏。

这里生产的LaTeX代码:

\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}

1 个答案:

答案 0 :(得分:2)

在这里,我很乐意引用伟大的answer I received from Fran at https://tex.stackexchange.com/

根据他的说法,自定义注释以LaTeX代码中的\multicolumn结尾。因此,我们无法使用par\\等换行符命令。但我们可以使用\parbox实现自动换行。如果我们仍然想要自定义换行符,我们可以使用四个反斜杠\\\\。为了更好地格式化,我们使用\\vspace{2pt}。为了给Coefficients和GOF提供更多空间,我们可以在R chunk之前添加\renewcommand\arraystretch{1.3}(默认值为1):

\renewcommand\arraystretch{1.3}  

```{r results="asis", echo = TRUE, comment = FALSE, message = FALSE}

library(texreg)
texreg(list(lm1, lm2, lm3), custom.note = "\\parbox{.4\\linewidth}
{\\vspace{2pt}Lorem ipsum dolor sit amet, consetetur sadipscing elitr, 
sed diam nonumy eirmod tempor invidunt ut labore et dolore magna 
aliquyam. \\\\ Lorem ipsum dolor sit amet, consetetur sadipscing elitr, 
sed diam nonumy eirmod tempor invidunt ut labore et dolore magna 
aliquyam. \\\\ %stars.}")

```

enter image description here