创建新环境来包装表格

创建新环境来包装表格

我有许多表格(和图形)超出了文档的边距,我不想单独更改每个表格和图形。我试图创建一个可以\begin{table} ... \end{table}调整所有内容大小的新环境,然后只需通过查找切换环境的名称即可。

以下是我的三个例子。

\documentclass[12pt,a4paper,titlepage]{book}

\usepackage[utf8]{inputenc}
\usepackage{graphicx} % enable graphics, images and resizebox


\usepackage{environ}

\NewEnviron{my_table} {
\begin{table}[!h]

\makebox[1 \width][c]{       %centering table
\resizebox{0.87 \width}{!}{   %resize table

\BODY

}
}

\end{table}
}

\begin{document}

\section{Table 1}
\begin{table}[!h]

\centering
\begin{tabular}{lllll}
  \hline
  & Model 1 & Model 2 & Model 3 & Model 4 \\ 
  \hline
Only 1 track & -0.53 (-0.65/-0.41) & -0.31 (-0.44/-0.18) & -0.31 (-0.44/-0.18) & -0.21 (-0.35/-0.07) \\ 
  Age selection $>$= 15 &  & -0.38 (-0.51/-0.26) & -0.38 (-0.51/-0.26) & -0.57 (-0.71/-0.43) \\ 
  \% of curric tracked &  &  & 0 (-0.23/0.23) & 0.59 (0.31/0.88) \\ 
  Vocational Index &  &  &  & -0.27 (-0.36/-0.19) \\ 
  Intercept & 1.77 (1.7/1.84) & 1.94 (1.85/2.03) & 1.94 (1.69/2.18) & 1.54 (1.27/1.81) \\ 
   \hline R square: & 23\% & 32\% & 32\% & 40\% \\ Sample size: & 195 & 195 & 195 & 183 \\ \hline \hline
\end{tabular}
\caption{Table 1}
\end{table}


\section{Table 2}

\begin{table}[!h]

\makebox[1 \width][c]{       %centering table
\resizebox{0.87 \width}{!}{   %resize table

\begin{tabular}{lllll}
  \hline
  & Model 1 & Model 2 & Model 3 & Model 4 \\ 
  \hline
Only 1 track & -0.53 (-0.65/-0.41) & -0.31 (-0.44/-0.18) & -0.31 (-0.44/-0.18) & -0.21 (-0.35/-0.07) \\ 
  Age selection $>$= 15 &  & -0.38 (-0.51/-0.26) & -0.38 (-0.51/-0.26) & -0.57 (-0.71/-0.43) \\ 
  \% of curric tracked &  &  & 0 (-0.23/0.23) & 0.59 (0.31/0.88) \\ 
  Vocational Index &  &  &  & -0.27 (-0.36/-0.19) \\ 
  Intercept & 1.77 (1.7/1.84) & 1.94 (1.85/2.03) & 1.94 (1.69/2.18) & 1.54 (1.27/1.81) \\ 
   \hline R square: & 23\% & 32\% & 32\% & 40\% \\ Sample size: & 195 & 195 & 195 & 183 \\ \hline \hline
\end{tabular}

} %close resize
} %close centering


\caption{Table 2}
\end{table}

\section{Table 3}

\begin{my_table}

\begin{tabular}{lllll}
  \hline
  & Model 1 & Model 2 & Model 3 & Model 4 \\ 
  \hline
Only 1 track & -0.53 (-0.65/-0.41) & -0.31 (-0.44/-0.18) & -0.31 (-0.44/-0.18) & -0.21 (-0.35/-0.07) \\ 
  Age selection $>$= 15 &  & -0.38 (-0.51/-0.26) & -0.38 (-0.51/-0.26) & -0.57 (-0.71/-0.43) \\ 
  \% of curric tracked &  &  & 0 (-0.23/0.23) & 0.59 (0.31/0.88) \\ 
  Vocational Index &  &  &  & -0.27 (-0.36/-0.19) \\ 
  Intercept & 1.77 (1.7/1.84) & 1.94 (1.85/2.03) & 1.94 (1.69/2.18) & 1.54 (1.27/1.81) \\ 
   \hline R square: & 23\% & 32\% & 32\% & 40\% \\ Sample size: & 195 & 195 & 195 & 183 \\ \hline \hline
\end{tabular}

\caption{Table 3}
\end{my_table}


\end{document}

简化一下:

1)第一个表格超出边距

2) 第二张表与上一张表相同,但使用\makebox\resizebox调整表大小以适合。它按我预期的方式工作。

3) 第三个表有问题。我采取的\makebox ..\resizebox策略是使用包创建一个环境environ\NewEnviron然后使用此环境包装表 3。我得到了几个错误,你只需复制这个 MWE 就可以重现这些错误。

关于如何使环境周围的包装器发挥作用,您有什么想法吗?

欢迎任何反馈!

相关内容