Tables – \renewcommand{\arraystretch}{1.3} Causes LaTeX Error: \caption Outside Float in Rmarkdown

renewcommandrmarkdowntables

I'm writing my thesis and want to include several tables in Rmarkdown. The first table is long and includes several LaTeX symbols for regression parameters $\beta$. The rows seemed a bit squished so I extended the space between the rows using \renewcommand{\arraystretch}{1.3}. Everything works fine until I tried to use \renewcommand{\arraystretch}{1.3} in a second table. The LaTeX error is \caption outside float so I thought the problem was with the caption. I have finally figured out that the Rmarkdown file knits perfectly once I remove the \renewcommand{\arraystretch}{1.3} in the second table. I have included an example from Rmarkdown. Does anyone know why using \renewcommand{\arraystretch}{1.3} twice doesn't work for some reason? Is there another way to add vertical space between the rows? Thanks <3

test.Rmd (entire file)

---
title: "Real life data"
author: "Julie Berg"
date: "`r Sys.Date()`"
header-includes:
   - \usepackage{tikz}
   - \usetikzlibrary{automata, positioning, arrows}
   - \usepackage{graphics}
   - \usepackage{natbib}
output: 
  pdf_document:
    toc: true
    number_sections: true
    extra_dependencies: ["bbm"]
papersize: a5
---

```{r setup, include=FALSE, echo=FALSE, warning=FALSE, message=FALSE}
rm(list=ls())
knitr::opts_chunk$set(echo = FALSE, warning=FALSE, message=FALSE, out.width = "100%")

library(INLA)
library(survival)
library(mstate)
library(tidyverse)
library(patchwork)
library(dplyr)
library(ggplot2)
library(survival)
library(survminer)
```

# first table

\begin{table}
\centering
\renewcommand{\arraystretch}{1.3}
\begin{tabular}{ c|cccc }
  & mean & 0.025quant & 0.975quant & mode \\
 \hline
 $\beta_0^{52}$ & 1 & 1 & 1 & 1 \\
 $\beta_0^{25}$ & 2 & 2 & 2 & 2 \\
 $\beta_0^{21}$ & 3 & 3 & 3 & 3 \\

 $\beta^{52}_{\text{Age}}$ & 4 & 4 & 4 & 4 \\
 $\beta^{25}_{\text{Age}}$ & 5 & 5 & 5 & 5 \\
 $\beta^{21}_{\text{Age}}$ & 6 & 6 & 6 & 6 \\

 $\beta^{52}_{\text{Gen}}$ & 7 & 7 & 7 & 7 \\
 $\beta^{25}_{\text{Gen}}$ & 8 & 8 & 8 & 8 \\
 $\beta^{21}_{\text{Gen}}$ & 9 & 9 & 9 & 9 \\

 $\beta^{52}_{\text{Mass}}$ & 10 & 10 & 10 & 10\\
 $\beta^{25}_{\text{Mass}}$ & 11 & 11 & 11 & 11\\
 $\beta^{21}_{\text{Mass}}$ & 12 & 12 & 12 & 12\\

 $\beta^{52}_{\text{res}}$ & 13 & 13 & 13 & 13 \\
 $\beta^{25}_{\text{res}}$ & 14 & 14 & 14 & 14 \\
 $\beta^{21}_{\text{res}}$ & 15 & 15 & 15 & 15\\

 $\alpha_{52}$ & 16 & 16 & 16 & 16 \\
 $\alpha_{25}$ & 17 & 17 & 17 & 17\\
 $\alpha_{21}$ & 18 & 18 & 18 & 18\\
\end{tabular}
\caption{Estimated parameter values using all Weibull baseline models.}
\label{Tab:estimatesWeib}
\end{table}



# second table

\begin{table}
\centering
\renewcommand{\arraystretch}{1.3}
\begin{tabular}{ c|cccc }
  & mean & 0.025quant & 0.975quant & mode \\
 \hline
 $\beta_0^{52}$ & 1 & 1 & 1 & 1 \\
 $\beta_0^{25}$ & 2 & 2 & 2 & 2 \\
 $\beta_0^{21}$ & 3 & 3 & 3 & 3 \\

 $\beta^{52}_{\text{Age}}$ & 4 & 4 & 4 & 4 \\
 $\beta^{25}_{\text{Age}}$ & 5 & 5 & 5 & 5 \\
 $\beta^{21}_{\text{Age}}$ & 6 & 6 & 6 & 6 \\

 $\beta^{52}_{\text{Gen}}$ & 7 & 7 & 7 & 7 \\
 $\beta^{25}_{\text{Gen}}$ & 8 & 8 & 8 & 8 \\
 $\beta^{21}_{\text{Gen}}$ & 9 & 9 & 9 & 9 \\

 $\beta^{52}_{\text{Mass}}$ & 10 & 10 & 10 & 10\\
 $\beta^{25}_{\text{Mass}}$ & 11 & 11 & 11 & 11\\
 $\beta^{21}_{\text{Mass}}$ & 12 & 12 & 12 & 12\\

 $\beta^{52}_{\text{res}}$ & 13 & 13 & 13 & 13 \\
 $\beta^{25}_{\text{res}}$ & 14 & 14 & 14 & 14 \\
 $\beta^{21}_{\text{res}}$ & 15 & 15 & 15 & 15\\

 $\alpha_{52}$ & 16 & 16 & 16 & 16 \\
 $\alpha_{25}$ & 17 & 17 & 17 & 17\\
 $\alpha_{21}$ & 18 & 18 & 18 & 18\\
\end{tabular}
\caption{Estimated parameter values using an Exp baseline model.}
\label{Tab:estimatesExp}
\end{table}

The generated .tex file

% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
  a5paper,
]{article}
\usepackage{lmodern}
\usepackage{amsmath}
\usepackage{ifxetex,ifluatex}
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{textcomp} % provide euro and other symbols
  \usepackage{amssymb}
\else % if luatex or xetex
  \usepackage{unicode-math}
  \defaultfontfeatures{Scale=MatchLowercase}
  \defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
  \usepackage[]{microtype}
  \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
  \IfFileExists{parskip.sty}{%
    \usepackage{parskip}
  }{% else
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
  \KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\hypersetup{
  pdftitle={Real life data},
  pdfauthor={Julie Berg},
  hidelinks,
  pdfcreator={LaTeX via pandoc}}
\urlstyle{same} % disable monospaced font for URLs
\usepackage[margin=1in]{geometry}
\usepackage{graphicx}
\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}
% Set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{5}
\usepackage{tikz}
\usetikzlibrary{automata, positioning, arrows}
\usepackage{graphics}
\usepackage{natbib}
\usepackage{bbm}
\ifluatex
  \usepackage{selnolig}  % disable illegal ligatures
\fi

\title{Real life data}
\author{Julie Berg}
\date{2022-01-26}

\begin{document}
\maketitle

{
\setcounter{tocdepth}{2}
\tableofcontents
}
\hypertarget{first-table}{%
\section{first table}\label{first-table}}

\begin{table}
\centering
\renewcommand{\arraystretch}{1.3}
\begin{tabular}{ c|cccc }
  & mean & 0.025quant & 0.975quant & mode \\
 \hline
 $\beta_0^{52}$ & 1 & 1 & 1 & 1 \\
 $\beta_0^{25}$ & 2 & 2 & 2 & 2 \\
 $\beta_0^{21}$ & 3 & 3 & 3 & 3 \\

 $\beta^{52}_{\text{Age}}$ & 4 & 4 & 4 & 4 \\
 $\beta^{25}_{\text{Age}}$ & 5 & 5 & 5 & 5 \\
 $\beta^{21}_{\text{Age}}$ & 6 & 6 & 6 & 6 \\

 $\beta^{52}_{\text{Gen}}$ & 7 & 7 & 7 & 7 \\
 $\beta^{25}_{\text{Gen}}$ & 8 & 8 & 8 & 8 \\
 $\beta^{21}_{\text{Gen}}$ & 9 & 9 & 9 & 9 \\

 $\beta^{52}_{\text{Mass}}$ & 10 & 10 & 10 & 10\\
 $\beta^{25}_{\text{Mass}}$ & 11 & 11 & 11 & 11\\
 $\beta^{21}_{\text{Mass}}$ & 12 & 12 & 12 & 12\\

 $\beta^{52}_{\text{res}}$ & 13 & 13 & 13 & 13 \\
 $\beta^{25}_{\text{res}}$ & 14 & 14 & 14 & 14 \\
 $\beta^{21}_{\text{res}}$ & 15 & 15 & 15 & 15\\

 $\alpha_{52}$ & 16 & 16 & 16 & 16 \\
 $\alpha_{25}$ & 17 & 17 & 17 & 17\\
 $\alpha_{21}$ & 18 & 18 & 18 & 18\\
\end{tabular}
\caption{Estimated parameter values using all Weibull baseline models.}
\label{Tab:estimatesWeib}
\end{table}

\hypertarget{second-table}{%
\section{second table}\label{second-table}}

\textbackslash begin\{table\} \centering

\renewcommand{\arraystretch}{1.3}
\begin{tabular}{ c|cccc }
  & mean & 0.025quant & 0.975quant & mode \\
 \hline
 $\beta_0^{52}$ & 1 & 1 & 1 & 1 \\
 $\beta_0^{25}$ & 2 & 2 & 2 & 2 \\
 $\beta_0^{21}$ & 3 & 3 & 3 & 3 \\

 $\beta^{52}_{\text{Age}}$ & 4 & 4 & 4 & 4 \\
 $\beta^{25}_{\text{Age}}$ & 5 & 5 & 5 & 5 \\
 $\beta^{21}_{\text{Age}}$ & 6 & 6 & 6 & 6 \\

 $\beta^{52}_{\text{Gen}}$ & 7 & 7 & 7 & 7 \\
 $\beta^{25}_{\text{Gen}}$ & 8 & 8 & 8 & 8 \\
 $\beta^{21}_{\text{Gen}}$ & 9 & 9 & 9 & 9 \\

 $\beta^{52}_{\text{Mass}}$ & 10 & 10 & 10 & 10\\
 $\beta^{25}_{\text{Mass}}$ & 11 & 11 & 11 & 11\\
 $\beta^{21}_{\text{Mass}}$ & 12 & 12 & 12 & 12\\

 $\beta^{52}_{\text{res}}$ & 13 & 13 & 13 & 13 \\
 $\beta^{25}_{\text{res}}$ & 14 & 14 & 14 & 14 \\
 $\beta^{21}_{\text{res}}$ & 15 & 15 & 15 & 15\\

 $\alpha_{52}$ & 16 & 16 & 16 & 16 \\
 $\alpha_{25}$ & 17 & 17 & 17 & 17\\
 $\alpha_{21}$ & 18 & 18 & 18 & 18\\
\end{tabular}
\caption{Estimated parameter values using an Exp baseline model.}
\label{Tab:estimatesExp}

\textbackslash end\{table\}

\end{document}

Best Answer

The answer came from a friend who doesn't have a stackexchange user, so I will answer for him:

"Put \renewcommand{\arraystretch}{1.3} outside the very first table environment, and it should stretch out both tables without an error message. Then you don't need a second \renewcommand{\arraystretch}{1.3} for the second table"

This worked perfectly! I assume all following tables will be stretched, but in my case it doesn't matter as it is the last chapter with tables.

Related Question