每个表的单独 \arraystretch 值

每个表的单独 \arraystretch 值

\arraystretch定义表格的垂直间距。如果我有多个表格,我该如何\arraystretch为每个表格定义一个单独的值?在我的例子中,最后一个定义\arraystretch适用于所有表格。

答案1

所提出的解决方案有一个限制,即它们不允许使用垂直对齐可选参数tabular

这是一个更加灵活的版本,它支持、以及tabulartabular*如果加载了相关包)。arraylongtable

如果width设置了键,tabular*则使用。否则,表格类型由type键决定(默认tabular)。请注意,每次调用时拉伸都会重置为 1。可以定义一个“继承”键,以便拉伸因子保留在嵌套表中。

\documentclass{article}
\usepackage{longtable}
%\usepackage{xparse} % only needed for LaTeX prior to 2020-10-01

\ExplSyntaxOn
\NewDocumentEnvironment{flexitab}{m}
 {
  \keys_set:nn { flexitab }
   {
    % default value for type
    type = tabular,
    % default value for stretch
    stretch = 1,
    % default value for alignment
    align = c,
    % default value for width
    width = -1000pt,
    #1
   }
  \dim_compare:nTF { \l_flexitab_width_dim > -1pt }
   {
    \tl_set:Nn \l_flexitab_type_tl { tabular* }
    \tl_set:Nn \l_flexitab_start_tl { \begin{tabular*}{\l_flexitab_width_dim} }
   }
   {
    \tl_set:Nn \l_flexitab_start_tl { \begin{\l_flexitab_type_tl} }
   }
  \tl_put_right:Nx \l_flexitab_start_tl
   {
    [\l_flexitab_align_tl]{\exp_not:V \l_flexitab_columns_tl}
   }
  \tl_set_eq:NN \arraystretch \l_flexitab_stretch_tl
  \tl_use:N \l_flexitab_start_tl
 }
 {
  \str_if_eq:eeTF {\l_flexitab_type_tl}{tabular*}
   { \end{tabular*} }
   { \end{\l_flexitab_type_tl} }
 }

\keys_define:nn { flexitab }
 {
  type    .tl_set:N  = \l_flexitab_type_tl,
  align   .tl_set:N  = \l_flexitab_align_tl,
  width   .dim_set:N = \l_flexitab_width_dim,
  columns .tl_set:N  = \l_flexitab_columns_tl,
  stretch .tl_set:N  = \l_flexitab_stretch_tl,
 }
\tl_new:N \l_flexitab_start_tl
\ExplSyntaxOff

\begin{document}

\begin{flexitab}{
  stretch=2,
  columns=cc,
}
AA & BBB \\
AA & BBB \\
C  & \begin{flexitab}{columns=@{}l@{},align=t}D\\D\\D\end{flexitab}
\end{flexitab}

\bigskip

\bigskip
$\begin{flexitab}{
  type=array,
  columns=cc,
}
aa & bb \\
cc & \begin{flexitab}{stretch=2,columns=l,align=b}aa \\ bb \\ccc\end{flexitab} \\
d & d
\end{flexitab}$

\noindent
\begin{flexitab}{
  stretch=2,
  width=\textwidth,
  columns=@{\extracolsep{\fill}}ll@{},
}
AAAA & BBBB \\
CC   & DDD
\end{flexitab}

\bigskip

\begin{flexitab}{
  type=longtable,
  columns=cc
}
aa & bb \\
\hline
\endhead
cc & dd \\
cc & dd \\
cc & dd \\
cc & dd \\
\end{flexitab}

\end{document}

在此处输入图片描述

答案2

如果您在组内的\arraystretch表之前定义直接(即),它应该只对该表有效。tabular

\begingroup
\renewcommand*{\arraystretch}{<value>}
\begin{tabular}{...}
 ...
\end{tabular}
\endgroup

或者定义你自己的环境:

\renewenvironment{mytabular}[1][1]{%
  \renewcommand*{\arraystretch}{#1}%
  \tabular%
}{%
  \endtabular
}

% ...

\begin{mytabular}[<stretch value>]{...}
 ...
\end{mytabular}

答案3

\documentclass{article}

\newenvironment{Tabular}[2][1]
  {\def\arraystretch{#1}\tabular{#2}}
  {\endtabular}

\begin{document}

\begin{Tabular}{ccc}
 c & c & C\\
 c & c & C
\end{Tabular}

\begin{Tabular}[3]{ccc}
 c & c & C\\
 c & c & C
\end{Tabular}

\end{document}

答案4

如果您使用表格环境来放置表格浮点数,那么您只需在该环境中重新定义 arraystretch 即可,而不会影响其他表格:

\documentclass{article}

\begin{document}
\begin{table}
    \centering
    \renewcommand*{\arraystretch}{0.95}
    \caption{table 1}
    \begin{tabular}{ccc}
        c & c & C\\
        c & c & C
    \end{tabular}
\end{table}

\begin{table}
    \centering
    \caption{table 2}
    \begin{tabular}[3]{ccc}
     c & c & C\\
     c & c & C
    \end{tabular}
\end{table}

\end{document}

相关内容