无法在具有多行列的表格中更新宏值

无法在具有多行列的表格中更新宏值

我正在编写命令 \newrow 来生成 longtable 中的一行。问题是这里的表格环境看不到 \subColWid 的值(输出为 0pt)。只有在使用多行时才会发生这种情况。有人能帮我弄清楚为什么 \setlength 更新的 \subColWid 在表格中不可见(包括列规范)。谢谢。

\documentclass{article}

\usepackage{fullpage}
\usepackage{longtable}
\usepackage{array}
\usepackage{multirow}

% define my macros
\newlength{\firstColWid}
\newlength{\secondColWid}
\newlength{\subColWid}
\setlength\firstColWid{0.1\textwidth}
\setlength\secondColWid{0.9\textwidth}
%\setlength{\subColWid}{0.5\secondColWid} % uncomment this line to see correct result

\setlength{\tabcolsep}{0pt}

\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}

\newcommand{\newrow}[2]{ % a command to produce a row spanning two lines
    % set width for each cell in the second column
    \setlength\subColWid{0.2\secondColWid} % this has effect here but not in the 
    %tabular
    % \the\subColWid
    \multirow{2}{*}{#1}  & subColWid = \the\subColWid \\ \cline{2-2}
                         & 
    % use a tabular to represent all the remaining values
    \begin{tabular}[c]{ @{\extracolsep{\fill}} *{2}{M{\subColWid} | }}
        % subColWid = \the\subColWid & #2 \\
        #2 & \the\subColWid \\
    \end{tabular}

}


\begin{document}
\begin{longtable}[c]{|M{\firstColWid}|@{\extracolsep{\fill}} M{\secondColWid}|} % 
    \hline
    % give row number and another cell
    \newrow{1}{Row1} \\ \hline
    \newrow{2}{Row2} \\
    \hline

\end{longtable}
\end{document}

答案1

您可能想在前面加上\setlength\global期望获得想要的效果。在这个特定情况下,这似乎有效。但通常不是。请参阅更新请参阅下文了解更多详情。

\documentclass{article}

\usepackage{fullpage}
\usepackage{longtable}
\usepackage{array}
\usepackage{multirow}

% define my macros
\newlength{\firstColWid}
\newlength{\secondColWid}
\newlength{\subColWid}
\setlength\firstColWid{0.1\textwidth}
\setlength\secondColWid{0.9\textwidth}
%\setlength{\subColWid}{0.5\secondColWid} % uncomment this line to see correct result

\setlength{\tabcolsep}{0pt}

\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}

\newcommand{\newrow}[2]{ % a command to produce a row spanning two lines
    % set width for each cell in the second column
    \global\setlength\subColWid{0.2\secondColWid} % this has effect here but not in the 
    %tabular
    % \the\subColWid
    \multirow{2}{*}{#1}  & subColWid = \the\subColWid \\ \cline{2-2}
                         & 
    % use a tabular to represent all the remaining values
    \begin{tabular}[c]{ @{\extracolsep{\fill}} *{2}{M{\subColWid} | }}
        % subColWid = \the\subColWid & #2 \\
        #2 & \the\subColWid \\
    \end{tabular}

}


\begin{document}

\begin{longtable}[c]{|M{\firstColWid}|@{\extracolsep{\fill}} M{\secondColWid}|} % 
    \hline
    % give row number and another cell
    \newrow{1}{Row1} \\ \hline
    \newrow{2}{Row2} \\
    \hline

\end{longtable}
\end{document}

当然,这将影响\subColWid文档其余部分的行为。如果您需要旧值,则应执行以下操作

\newlength\myoldlength
\setlength\myoldlength{\subColWid}

一旦你完成了你的表,你可以使用恢复旧值

 \setlength\subColWid{\myoldlength}

更新

在这里读了一些内容之后(考虑到@egreg写的关于使用\global\setlength 这里[请参阅他的讨论结尾处的相关评论]),我\global\setlength上面的使用仅在 LaTeX 中起作用。@AndrewSwann 提供了一个更好的解决方案这里\subColWid。因此,当从表中更改它时,获得所需值的更好方法似乎是按如下方式定义命令:

\newcommand{\newrow}[2]{ % a command to produce a row spanning two lines
    % set width for each cell in the second column
    \global\subColWid=\dimexpr0.2\secondColWid\relax % this has effect here but not in the 
    %tabular
    % \the\subColWid
    \multirow{2}{*}{#1}  & subColWid = \the\subColWid \\ \cline{2-2}
                         & 
    % use a tabular to represent all the remaining values
    \begin{tabular}[c]{ @{\extracolsep{\fill}} *{2}{M{\subColWid} | }}
        % subColWid = \the\subColWid & #2 \\
        #2 & \the\subColWid \\
    \end{tabular}
}

相关内容