如何删除标题和表格之间的空白?

时间:2020-06-10 17:45:50

标签: latex kable beamer kableextra

我也尝试在其他地方询问此问题,但是我无法删除表格标题和实际表格之间的空白。在我的常规rmarkdown文章中,不会发生这种情况,但是当我为演示文稿而扎弯时,它会产生这些奇怪的空白。有人知道为什么吗?

mean.party.class <- dat3 %>%
  group_by(party2, class2) %>%
   filter (party2 !="Indep" & class != "IDK")%>%
  summarise_at(vars(DL, DU, RL, RU, L, U, D, R), funs(mean(., na.rm=TRUE)))

knitr::kable(mean.party.class, col.names=c("Party ID", "Class", "DL", "DU", "RL",   "RU",   "L", "U", "D" , "R"), digits=2,  caption = "Mean Trust Per Group (Party and Social Class Identity)", "latex", booktabs = T) %>%
 kable_styling( full_width = F, position = "left",  latex_options = "scale_down", bootstrap_options = c("striped", "hover")) %>% footnote(
   general = c("1. D= Democrat, R=Republican, L=Lower Class, U = Upper Class; ", "2. Independents dropped")) %>% row_spec(0,bold=TRUE)

enter image description here

这里是一个示例

---
title: "Example"
author: "Its me"
date: "6/12/2020"
always_allow_html: true
header-includes:
- \usepackage{booktabs}
- \usepackage{longtable}
- \usepackage{tabu}
- \usepackage{threeparttable}
- \usepackage{adjustbox}
- \usepackage{caption} \captionsetup[table]{skip=0pt}
- \usepackage[font=small,skip=0pt]{caption}
- \usepackage{tikz}
- \usepackage{float}
- \usetikzlibrary{arrows,shapes,positioning}


output:
  beamer_presentation:
    theme: "Berkeley"
    colortheme: "dolphin"
    fonttheme: "structurebold"
    slide_level: 2 
    #incremental: true
---

```{r}
setwd("~/Google Drive/UC Davis /R stuff")
```

```{r, include=FALSE}
library(rticles)
library(kableExtra)
library(knitr)
library(plyr)
library(dplyr)
library(gdata)
library(ggplot2)
library(MASS)
library(corrplot)
library(haven)
library(factoextra)
library(ggplot2)
library(ggrepel)
library(viridis)
library(lattice)
library(optiscale)
library(acepack)
library(psych)
library(smacof)
library(MCMCpack)
library(basicspace)
library(boot)
library(pscl)
library(wnominate)
library(RColorBrewer)
library(sjPlot)
```

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

```{r, include= FALSE}
#load cars
data("mtcars")
head(mtcars, 2)
```

```{r, include= FALSE}
mean.mpg <- mtcars %>%
  summarise_at(vars(mpg), funs(mean(., na.rm=TRUE)))
```

## there it is
```{r}
knitr::kable(mean.mpg, col.names=c("mpg"), digits=2,  caption = "This is a table", "latex", booktabs = T, escape=F) %>%
 kable_styling(full_width = T, position = "left", latex_options = "scale_down", bootstrap_options = c("striped", "hover")) %>% footnote(
   general = c("1. test 1", "2. test 2"))%>%
  row_spec(0,bold=TRUE)
```

1 个答案:

答案 0 :(得分:1)

如果将keep_tex: true添加到标题中,则可以编辑生成的.tex文件,以消除造成额外空间的多种原因:

  • 删除桌子周围不必要的且通常有害的\resizebox{\linewidth}{!}{...}

  • 删除caption程序包(该程序不必要地多次加载)

  • 删除parskip程序包


\PassOptionsToPackage{unicode=true}{hyperref} % options for packages loaded elsewhere
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[ignorenonframetext,]{beamer}
\usepackage{pgfpages}
\setbeamertemplate{caption}[numbered]
\setbeamertemplate{caption label separator}{: }
\setbeamercolor{caption name}{fg=normal text.fg}
\beamertemplatenavigationsymbolsempty
% Prevent slide breaks in the middle of a paragraph:
\widowpenalties 1 10000
\raggedbottom
\setbeamertemplate{part page}{
\centering
\begin{beamercolorbox}[sep=16pt,center]{part title}
  \usebeamerfont{part title}\insertpart\par
\end{beamercolorbox}
}
\setbeamertemplate{section page}{
\centering
\begin{beamercolorbox}[sep=12pt,center]{part title}
  \usebeamerfont{section title}\insertsection\par
\end{beamercolorbox}
}
\setbeamertemplate{subsection page}{
\centering
\begin{beamercolorbox}[sep=8pt,center]{part title}
  \usebeamerfont{subsection title}\insertsubsection\par
\end{beamercolorbox}
}
\AtBeginPart{
  \frame{\partpage}
}
\AtBeginSection{
  \ifbibliography
  \else
    \frame{\sectionpage}
  \fi
}
\AtBeginSubsection{
  \frame{\subsectionpage}
}
\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}
  \usepackage{textcomp} % provides euro and other symbols
\else % if luatex or xelatex
  \usepackage{unicode-math}
  \defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\fi
\usetheme[]{Berkeley}
\usecolortheme{dolphin}
\usefonttheme{structurebold}
% 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
}{}
\IfFileExists{parskip.sty}{%
%\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
}
\usepackage{hyperref}
\hypersetup{
            pdftitle={Example},
            pdfauthor={Its me},
            pdfborder={0 0 0},
            breaklinks=true}
\urlstyle{same}  % don't use monospace font for urls
\newif\ifbibliography
\setlength{\emergencystretch}{3em}  % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{0}

% set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother

\usepackage{booktabs}
\usepackage{longtable}
\usepackage{tabu}
\usepackage{threeparttable}
\usepackage{adjustbox}
%\usepackage{caption}

%\captionsetup[table]{skip=0pt}
%\usepackage[font=small,skip=0pt]{caption}
\usepackage{tikz}
\usepackage{float}
\usetikzlibrary{arrows,shapes,positioning}

\title{Example}
\author{Its me}
\date{6/12/2020}

\begin{document}
\frame{\titlepage}

\begin{frame}{there it is}
\protect\hypertarget{there-it-is}{}

\begin{table}

\caption{\label{tab:unnamed-chunk-4}This is a table}
%\resizebox{\linewidth}{!}{
\begin{tabu} to \linewidth {>{\raggedleft}X}
\toprule
\textbf{mpg}\\
\midrule
20.09\\
\bottomrule
\multicolumn{1}{l}{\textit{Note: }}\\
\multicolumn{1}{l}{1. test 1}\\
\multicolumn{1}{l}{2. test 2}\\
\end{tabu}
%}
\end{table}

\end{frame}

\end{document}

enter image description here

(我会远离tabu并改用普通表)