LaTeX adjustbox shrinks table

adjustboxrmarkdown

I've been having a strange issue with my LaTeX code with adjustbox. I'm currently using RMarkdown which converts markdown to LaTeX codes, and I'm having trouble narrowing down where it is causing the issue. I think showing .tex is the most relevant, but including RMarkdown just in case.

  1. The original RMarkdown code

Here's my RMarkdown code:

---
title: "Untitled"
output: 
  pdf_document: 
    extra_dependencies: float
    number_sections: yes
    keep_tex: true
date: '2022-02-16'
header-includes:
  - \usepackage{booktabs}
  - \usepackage{geometry}
  - \usepackage{tikz}
  - \usepackage{adjustbox}
---


\begingroup
\centering
\begin{adjustbox}{width = \textwidth, center}
   \begin{tabular}{lcccc}
      \tabularnewline \midrule \midrule
      Dependent Variable: & \multicolumn{4}{c}{Sepal.Width}\\
      Species        & Full sample     & setosa         & versicolor    & virginica \\   
      Model:         & (1)             & (2)            & (3)           & (4)\\  
      \midrule
      \emph{Variables}\\
      (Intercept)    & 1.038$^{***}$   & -0.5304        & 0.8252$^{*}$  & 1.438$^{***}$\\   
                     & (0.2882)        & (0.5561)       & (0.4302)      & (0.4450)\\   
      Sepal.Length   & 0.5612$^{***}$  & 0.8049$^{***}$ & 0.1455        & 0.2217$^{*}$\\   
                     & (0.0653)        & (0.1090)       & (0.1098)      & (0.1308)\\   
      Petal.Length   & -0.3353$^{***}$ & -0.0486        & 0.2538$^{**}$ & 0.0136\\   
                     & (0.0307)        & (0.2212)       & (0.1206)      & (0.1507)\\   
      \midrule
      \emph{Fit statistics}\\
      Observations   & 150             & 50             & 50            & 50\\  
      R$^2$          & 0.45641         & 0.55184        & 0.33890       & 0.20919\\  
      Adjusted R$^2$ & 0.44902         & 0.53277        & 0.31076       & 0.17554\\  
      \midrule \midrule
      \multicolumn{5}{l}{\emph{IID standard-errors in parentheses}}\\
      \multicolumn{5}{l}{\emph{Signif. Codes: ***: 0.01, **: 0.05, *: 0.1}}\\
   \end{tabular}
\end{adjustbox}
\par\endgroup

  1. .tex file that was generated to create pdf file
% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
]{article}
\usepackage{amsmath,amssymb}
\usepackage{lmodern}
\usepackage{iftex}
\ifPDFTeX
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{textcomp} % provide euro and other symbols
\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={Untitled},
  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{booktabs}
\usepackage{geometry}
\usepackage{tikz}
\usepackage{adjustbox}
\usepackage{float}
\ifLuaTeX
  \usepackage{selnolig}  % disable illegal ligatures
\fi

\title{Untitled}
\author{}
\date{\vspace{-2.5em}2022-02-16}

\begin{document}
\maketitle

\begingroup
\centering

\begin{adjustbox}{width = \textwidth, center}
   \begin{tabular}{lcccc}
      \tabularnewline \midrule \midrule
      Dependent Variable: & \multicolumn{4}{c}{Sepal.Width}\\
      Species        & Full sample     & setosa         & versicolor    & virginica \\   
      Model:         & (1)             & (2)            & (3)           & (4)\\  
      \midrule
      \emph{Variables}\\
      (Intercept)    & 1.038$^{***}$   & -0.5304        & 0.8252$^{*}$  & 1.438$^{***}$\\   
                     & (0.2882)        & (0.5561)       & (0.4302)      & (0.4450)\\   
      Sepal.Length   & 0.5612$^{***}$  & 0.8049$^{***}$ & 0.1455        & 0.2217$^{*}$\\   
                     & (0.0653)        & (0.1090)       & (0.1098)      & (0.1308)\\   
      Petal.Length   & -0.3353$^{***}$ & -0.0486        & 0.2538$^{**}$ & 0.0136\\   
                     & (0.0307)        & (0.2212)       & (0.1206)      & (0.1507)\\   
      \midrule
      \emph{Fit statistics}\\
      Observations   & 150             & 50             & 50            & 50\\  
      R$^2$          & 0.45641         & 0.55184        & 0.33890       & 0.20919\\  
      Adjusted R$^2$ & 0.44902         & 0.53277        & 0.31076       & 0.17554\\  
      \midrule \midrule
      \multicolumn{5}{l}{\emph{IID standard-errors in parentheses}}\\
      \multicolumn{5}{l}{\emph{Signif. Codes: ***: 0.01, **: 0.05, *: 0.1}}\\
   \end{tabular}
\end{adjustbox}
\par\endgroup

\end{document}

The outlook of pdf file : table shrunk and unreadable.

enter image description here

I'd appreciate any piece of information fixing this issue. Thank you.

Best Answer

It always helps to minimize the example, removing unneeeded packages. A smaller example is

%
\documentclass[
]{article}

\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
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
% 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, ...]{}

\usepackage{booktabs}
\usepackage{adjustbox}


\title{Untitled}
\author{}
\date{\vspace{-2.5em}2022-02-16}

\begin{document}
\maketitle

\begingroup
\centering

\begin{adjustbox}{width = \textwidth, center}
   \begin{tabular}{lcccc}
      \tabularnewline \midrule \midrule
      Dependent Variable: & \multicolumn{4}{c}{Sepal.Width}\\
      Species        & Full sample     & setosa         & versicolor    & virginica \\   
      Model:         & (1)             & (2)            & (3)           & (4)\\  
      \midrule
      \emph{Variables}\\
      (Intercept)    & 1.038$^{***}$   & -0.5304        & 0.8252$^{*}$  & 1.438$^{***}$\\   
                     & (0.2882)        & (0.5561)       & (0.4302)      & (0.4450)\\   
      Sepal.Length   & 0.5612$^{***}$  & 0.8049$^{***}$ & 0.1455        & 0.2217$^{*}$\\   
                     & (0.0653)        & (0.1090)       & (0.1098)      & (0.1308)\\   
      Petal.Length   & -0.3353$^{***}$ & -0.0486        & 0.2538$^{**}$ & 0.0136\\   
                     & (0.0307)        & (0.2212)       & (0.1206)      & (0.1507)\\   
      \midrule
      \emph{Fit statistics}\\
      Observations   & 150             & 50             & 50            & 50\\  
      R$^2$          & 0.45641         & 0.55184        & 0.33890       & 0.20919\\  
      Adjusted R$^2$ & 0.44902         & 0.53277        & 0.31076       & 0.17554\\  
      \midrule \midrule
      \multicolumn{5}{l}{\emph{IID standard-errors in parentheses}}\\
      \multicolumn{5}{l}{\emph{Signif. Codes: ***: 0.01, **: 0.05, *: 0.1}}\\
   \end{tabular}
\end{adjustbox}
\par\endgroup

\end{document}

Producing

enter image description here

If you remove

\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}

Then you get

enter image description here

If you globally set keys, you have to be sure nothing else is using the values. (I'm not sure why adjustbox is using the Gin key family here but...)