2

the title may be confusing, but I'm not sure how to name it better.

This current question is the result of my other ones, and the hints I got from users in this board.

Steven B. Segletes helped me by creating a way to build a special table, that is build column by column and is based on the stackengine package.

I tried to work with that solution, and build a way to provide input for the first of that 'columns', but I always get the error message mentioned in the title.

This is what I got:

\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}

Do you know what the problem might be?

The while construct can be tested with something like

\setcounter{stCurRow}{0}
\begin{itemize}
    \whileboolexpr
            { test {\ifnumcomp{\value{stCurRow}}{<}{\value{stNumCats}}} }
            {\stepcounter{stCurRow} \item \csuse{stcat\thestCurRow}}
\end{itemize}
muffel
  • 1,565
  • 1
  • 13
  • 24
  • One immediate issue is that the \LongStack argument is fragile and needs to be protected. – Steven B. Segletes Aug 13 '13 at 18:10
  • The problem is in \whileboolexpr { test {\ifnumcomp{\value{stCurRow}}{<}{\value{stNumCats}}} } {\mybox{{\stepcounter{stCurRow} \csuse{stcat\thestCurRow}}}}, which doesn't work, even if taken outside of the \Longstack and put into a \protected@edef – Steven B. Segletes Aug 13 '13 at 18:14
  • @StevenB.Segletes I added an example of the while construct, how it worked in a single way. Do you see any chance to modify it so ot works with \Longstack? – muffel Aug 13 '13 at 18:27

1 Answers1

3

Based on your other question, I took the liberty to read between the lines on where you are heading. Is this what you seek? What I did, rather than try to resolve the issue of placing do-loop code inside a \Longstack, which I doubt I could make work, I chose instead to build the stack up row by row, knowing each row's argument.

The option argument of [1] to \PrintCategories indicates that this is column 1, and not to box the upper left (row one) element.

\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}

enter image description here