在应用中降价我经常在这里找到有关“LaTeX”和“pandoc”的答案TeX Stackchange. 因此,我想在这里重复一下我的我在 stackoverflow 上问过的问题希望找到使用 pandoc 通过 LaTeX 转换为 PDF 时以下问题的解决方案:
我面临的问题是,Rtexreg()
似乎无法在表格下方的注释中换行。此外,模型之间的边距没有像现在这样均匀延伸,从而htmlreg()
产生干净的结果。手动换行(如\n
或 HTML 标签)无法与“texreg”配合使用。部分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
```
输出结果如下:
有人知道吗?任何帮助都值得感激。
以下是生成的 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
请注意,您的自定义注释以 结尾\multicolumn
,因此您不能使用换行符命令作为\par
或\\
,但您可以使用\parbox
:
custom.note = "\\parbox{\\linewidth}{Lorem ipsum dolor sit amet,
\\\\ consetetur sadipscing elitr, sed diam. %stars.}"
您还可以允许在较窄的范围内自动换行\parbox
,从而避免最后一列的宽度过大:
custom.note = "\\parbox{.4\\linewidth}{Lorem ipsum dolor sit amet,
consetetur sadipscing elitr, sed diam. %stars.}"