计算环境内的环境宽度

计算环境内的环境宽度

在外部环境中,我希望 TableWidth 始终与它前面的表格(内部环境的)宽度完全相同,以便第二个示例最后一行的文本位于一行上。

我觉得这可以使用 environ 包来完成,但我不知道如何将它与 \NewDocumentEnvironment 结合起来。

我的MWE

\documentclass{article}
\usepackage{
array,%         actions for tabular column cells
collcell,%      macro calls for tabular column cells
xparse,%        optional params and starred commands
ifthen,%        easy booleans, tests and loops
}

\newlength\TableWidth

\newcommand\Tfer[1]{%
  \ifthenelse{\equal{#1}{A}}{%
    \textbf{CAB}\relax%
  }{%
    #1\relax%
  }%
}

\newcolumntype{T}{>{\collectcell\Tfer}c<{\endcollectcell}}

\newenvironment{Inner}[1][t]{%
  \begin{tabular}[#1]{TTTT}%
}{\end{tabular}}

\NewDocumentEnvironment{Outer}{O{t}D(){\empty}}{%
  \begin{Inner}[#1]%
}{%
  \ifx#2\empty\else%
    \multicolumn{4}{l}{%
      \footnotesize%
      \begin{tabular}{p{\TableWidth}}\hline%
        #2%
      \end{tabular}%
    }%
  \fi%
  \end{Inner}%
}%

\begin{document}

\setlength\TableWidth{2.5cm}

\begin{Outer}(\raggedright some very long testing text)
  1 & B & b  & bb \\
  2 & A & ll & a \\
\end{Outer}

\begin{Outer}(\raggedright some very long testing text)
  123456789 & BBBBB & bbb & bbbb \\
  234567890 & A     & lll & aaaa \\
\end{Outer}

\end{document}

答案1

您不需要内部环境:想法是在一个框中正确设置表格,然后一旦我们知道主表格部分的宽度就使用外部表格。

\documentclass{article}
\usepackage{
collcell,%      macro calls for tabular column cells
xparse,%        optional params and starred commands
}

\newcolumntype{T}{>{\collectcell\Tfer}c<{\endcollectcell}}
\newsavebox{\jackbox}

\ExplSyntaxOn
\NewDocumentCommand\Tfer{m}
 {
  \str_if_eq:nnTF { #1 } { A } { \textbf{CAB} } { #1 }
 }
\ExplSyntaxOff

\NewDocumentEnvironment{Outer}{O{t}d()}
 {
  \begin{lrbox}{\jackbox}
  \begin{tabular}[#1]{@{}TTTT@{}}
 }
 {
  \end{tabular}
  \end{lrbox}
  \begin{tabular}[#1]{p{\wd\jackbox}}
  \usebox{\jackbox}
  \\
  \IfValueT{#2}{\hline\footnotesize #2}
  \end{tabular}
 }

\begin{document}

\begin{Outer}(\raggedright some very long testing text)
  1 & B & b  & bb \\
  2 & A & ll & a \\
\end{Outer}

\begin{Outer}(\raggedright some very long testing text)
  123456789 & BBBBB & bbb & bbbb \\
  234567890 & A     & lll & aaaa \\
\end{Outer}

\end{document}

在此处输入图片描述

答案2

您可以存储表格前后的位置,然后计算差异。为了避免最里面的表格干扰,您可以将其可见长度设置为零。我不确定您想对边距的 tabcolsep 做什么。如果需要,您必须对其进行调整。

\documentclass{article}
\usepackage{
array,%         actions for tabular column cells
collcell,%      macro calls for tabular column cells
xparse,%        optional params and starred commands
ifthen,%        easy booleans, tests and loops
}

\usepackage[savepos]{zref}

\newlength\TableWidth
\newcounter{postmpcnt}

\newcommand\Tfer[1]{%
  \ifthenelse{\equal{#1}{A}}{%
    \textbf{CAB}\relax%
  }{%
    #1\relax%
  }%
}

\newcolumntype{T}{>{\collectcell\Tfer}c<{\endcollectcell}}

\newenvironment{Inner}[1][t]
 {%
  \stepcounter{postmpcnt}%
  \setlength\TableWidth{%
      \dimexpr
            \zposx{stoppos-\number\value{postmpcnt}}sp -
            \zposx{startpos-\number\value{postmpcnt}}sp
       \relax}%
  %
  \leavevmode\zsaveposx {startpos-\number\value{postmpcnt}}%
  \begin{tabular}[#1]{TTTT}%  
  }
  {\end{tabular}%
   \zsaveposx {stoppos-\number\value{postmpcnt}}}

\NewDocumentEnvironment{Outer}{O{t}D(){\empty}}{%
  \begin{Inner}[#1]%
}{%
  \ifx#2\empty\else%
      \multicolumn{1}{l}{%
      \footnotesize%
      \makebox[0pt][l]{\begin{tabular}{@{}p{\dimexpr\TableWidth-2\tabcolsep}@{}}\hline%
        #2%
      \end{tabular}}}%
    %
  \fi%
  \end{Inner}%
}%

\begin{document}

\setlength\TableWidth{2.5cm}

\begin{Outer}(\raggedright some very long testing text)
  1 & B & b  & bb \\
  2 & A & ll & a \\
\end{Outer}

\begin{Outer}(\raggedright some very long testing text)
  123456789 & BBBBB & bbb & bbbb \\
  234567890 & A     & lll & aaaa \\
\end{Outer}

\end{document}

在此处输入图片描述

相关内容