LaTeX 调整框缩小表格

LaTeX 调整框缩小表格

我在使用 Adjustbox 编写 LaTeX 代码时遇到了一个奇怪的问题。我目前正在使用 RMarkdown,它将 Markdown 转换为 LaTeX 代码,但无法缩小导致问题的范围。我认为显示.tex是最相关的,但为了以防万一,还是包括 RMarkdown。

  1. 原始 RMarkdown 代码

这是我的 RMarkdown 代码:

---
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. 生成用于创建 pdf 文件的 .tex 文件
% 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}

pdf 文件的外观:表格缩小,无法阅读。

在此处输入图片描述

我将非常感激任何能解决此问题的信息。谢谢。

答案1

最小化示例并删除不需要的包总是有帮助的。一个较小的示例是

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

制作

在此处输入图片描述

如果你删除

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

然后你得到

在此处输入图片描述

如果您全局设置键,则必须确保没有其他东西使用这些值。(我不确定为什么adjustbox在这里使用Gin键系列,但是......)

答案2

按照 David 的建议注释掉(或删除)代码,这很好,但可能会导致 RMarkdown(Quarto)中其他图形的格式被破坏。

相反,使用Gin以下代码覆盖 pandoc 模板代码的一部分

\setkeys{Gin}{width=\linewidth,height=\textheight,keepaspectratio}

是一个更稳定的解决方案。(通过放入 YAML 标头,例如include-in-header:在 Quarto 中)

相关内容