创建“堆栈”列表会导致“\@firstoftwo 的参数有一个额外的 }”

创建“堆栈”列表会导致“\@firstoftwo 的参数有一个额外的 }”

标题可能令人困惑,但我不确定如何更好地命名它。

当前这个问题是我其他问题的结果,也是我从该论坛的用户那里得到的提示。

Steven B. Segletes 帮助了我创建一种构建特殊表格的方法,即逐列构建,并基于堆栈引擎包裹。

我尝试使用该解决方案,并建立一种方法来为第一个“列”提供输入,但我总是收到标题中提到的错误消息。

这是我得到的:

\documentclass{article}
\usepackage{stackengine}
\usepackage{etoolbox}

% 
\fboxsep=3pt
\fboxrule=.25pt
\def\mystrutheight{.7\baselineskip}
\def\mystrutdepth{.1\baselineskip}
\def\horzbuffer{2ex}
%
\def\mybox{}
\newcounter{index}
\newlength\cellwidth
\newlength\colwidth
\newcommand\myLongstack[1]{%
  \renewcommand\mybox[1]{##1}%
  \getargsC{#1}%
  \setlength\colwidth{0pt}%
  \setcounter{index}{0}%
  \whiledo{\value{index} < \narg}{%
    \stepcounter{index}%
    \setlength\cellwidth{\widthof{\csname arg\roman{index}\endcsname}}%
    \ifthenelse{\cellwidth > \colwidth}{%
      \setlength\colwidth{\the\cellwidth}}{}%
  }%
  \setlength\colwidth{\the\colwidth + \horzbuffer}%
  \xdef\myboxwidth{\the\colwidth}%
  \renewcommand\mybox[1]{\kern -\fboxrule\protect\framebox[\myboxwidth]{%
    \protect\rule[-\mystrutdepth]{0ex}{\mystrutheight}\smash{##1}}}%
  \Longstack{#1}%
}
\setstackgap{L}{\mystrutheight+2\fboxsep+\fboxrule}

%start of my own code
\newcounter{stNumCats} %number of categories
\newcounter{stCurRow} %current row 
\newcommand{\STAddCategory}[1]{
        % #1 is category name
        \stepcounter{stNumCats}
        \csdef{stcat\thestNumCats}{#1}
}
\newcommand{\PrintCategories}{
    \setcounter{stCurRow}{0}
        \myLongstack{
            \whileboolexpr
                { test {\ifnumcomp{\value{stCurRow}}{<}{\value{stNumCats}}} }
                {\mybox{{\stepcounter{stCurRow} \csuse{stcat\thestCurRow}}}}
        } 
}

\begin{document}

\STAddCategory{Cat A}
\STAddCategory{Cat B}
\PrintCategories

\end{document}

您知道可能存在什么问题吗?

尽管构造可以用类似的东西来测试

\setcounter{stCurRow}{0}
\begin{itemize}
    \whileboolexpr
            { test {\ifnumcomp{\value{stCurRow}}{<}{\value{stNumCats}}} }
            {\stepcounter{stCurRow} \item \csuse{stcat\thestCurRow}}
\end{itemize}

答案1

根据您的另一个问题,我冒昧地从字里行间读出了您的意图。这就是您想要的吗?我所做的是,而不是尝试解决将 do 循环代码放在 中的问题(\Longstack我怀疑我能否做到这一点),而是选择逐行构建堆栈,并了解每行的参数。

选项参数[1]to\PrintCategories表示这是第 1 列,并且不将左上角(第一行)元素装箱。

\documentclass{article}
\usepackage{stackengine}
\usepackage{etoolbox}

% 
\fboxsep=3pt
\fboxrule=.5pt
\def\mystrutheight{.7\baselineskip}
\def\mystrutdepth{.1\baselineskip}
\def\horzbuffer{2ex}
%
\def\mybox{}
\newcounter{index}
\newlength\cellwidth
\newlength\colwidth

\setstackgap{L}{\mystrutheight+2\fboxsep+\fboxrule}
\def\stacktype{L}

%start of my own code
\newcounter{stNumCats} %number of categories
\newcounter{stCurRow} %current row 
\newcommand{\STAddCategory}[1]{%
%       #1 is category name
        \stepcounter{stNumCats}%
        \csdef{stcat\thestNumCats}{#1}%
}
\newcommand{\PrintCategories}[1][0]{%
  \setlength\colwidth{0pt}%
  \setcounter{index}{0}%
  \whiledo{\value{index} < \value{stNumCats}}{%
    \stepcounter{index}%
    \setlength\cellwidth{\widthof{\csname stcat\theindex\endcsname}}%
    \ifthenelse{\cellwidth > \colwidth}{%
      \setlength\colwidth{\the\cellwidth}}{}%
  }%
  \setlength\colwidth{\the\colwidth + \horzbuffer}%
  \xdef\myboxwidth{\the\colwidth}%
  \renewcommand\mybox[1]{\kern -\fboxrule\framebox[\myboxwidth]{%
    \rule[-\mystrutdepth]{0ex}{\mystrutheight}\smash{##1}}}%
%
    \setcounter{stCurRow}{0}%
    \whiledo{\value{stCurRow} < \value{stNumCats}}{%
      \stepcounter{stCurRow}%
      \ifthenelse{\equal{\thestCurRow}{1}}{%
        \ifthenelse{\equal{#1}{1}}
        {\savestack{\tempstack}%
          {\csname stcat\thestCurRow\endcsname}}%
        {\savestack{\tempstack}%
          {\mybox{\csname stcat\thestCurRow\endcsname}}}%
      }{%
        \ifthenelse{\equal{\thestCurRow}{\thestNumCats}}{%
          \savestack{\tempstack}{\stackon{%
            \csname stcat\thestCurRow\endcsname}{\tempstack}}%
          }{%
          \savestack{\tempstack}{\stackon{\mybox{%
            \csname stcat\thestCurRow\endcsname}}{\tempstack}}%
          }%
      }%
    }%
   \tempstack%
   \setcounter{stNumCats}{0}%
}

\begin{document}

\STAddCategory{Cat A}%
\STAddCategory{Category B}%
\STAddCategory{C}%
\PrintCategories[1]%
\STAddCategory{37}%
\STAddCategory{546}%
\STAddCategory{123456}%
\PrintCategories%
\STAddCategory{12674}%
\STAddCategory{the end}%
\STAddCategory{0}%
\PrintCategories

\end{document}

在此处输入图片描述

相关内容