I'm trying to built a code that counts the number of \ in a string. My code can work with any other symbols (for example, if I need to count the number of 'a's, that works). I already tried to modify the catcode of the backslash symbol to 12, but with no success.
Any ideas?
My (incomplete) solution:
\newcounter{alternatives}
\renewcommand\thealternatives{\alph{alternatives}}
\makeatletter
\def\endtab{\vspace{-4 mm}}
\def\endtabi{\endtab}
\newcount\testcount
\def\gobblechar{\let\char= }
\newcount\slashcount
\def\countunlessnil
{%
\let\a=0
\let\\=\a
\ifx\char\a
% \slashcount=
\advance\slashcount by 1
\fi
\ifx\char\nil
\let\next=\relax%
\else%
\let\next=\auxcountchar%
\fi
\let\\=\\
\next
}%
\def\auxcountchar{%
\afterassignment\countunlessnil\gobblechar%
}
\def\countchar#1{\edef\xx{#1} \expandafter\auxcountchar\xx\nil}
\def\analyze#1{%
\countchar{#1}%
}
\newlength{\alternativessize}
\newlength{\abcde}
\settowidth{\abcde}{a)~b)~c)~d)~e)}
\WithSuffix\DeclareRobustCommand\tab+{\@tabbb}
\def\@tabbb#1 \endtab
{
\tabiiii#1
\endtab\\
\analyze{dfgreggegewg\a}
\the\testcount
% \setcounter{alternatives}{0}
\settowidth{\alternativessize}{#1}
\setlength{\alternativessize}{\linewidth - \alternativessize}
\setlength{\alternativessize}{\alternativessize - \abcde}
\the\value{alternatives}\\[4 mm]
\ifthenelse{\equal{\value{alternatives}}{5}}
{
\setlength{\alternativessize}{0.17\alternativessize}
}
{
\setlength{\alternativessize}{0.27\alternativessize}
}
\setcounter{alternatives}{0}
% \setlength{\alternativessize}{0.18\alternativessize}
\begin{minipage}[t]{\linewidth}
\tabiii#1
\end{minipage}\vspace{-2 mm}\\
\endtab\\
\setcounter{alternatives}{0}
}
\def\tabiii#1\\
{
\def\tempa{#1}
\ifx\tempa\endtabi
\endtab
\else
\if\relax\detokenize{#1}\relax
\else
\stepcounter{alternatives}\thealternatives)~~\@ifnextchar {_}{}#1
\hspace{\alternativessize}
\fi
\expandafter\tabiii
\fi
}
\def\tabiiii#1\\
{
\def\tempa{#1}
\ifx\tempa\endtabi
\endtab
\else
\if\relax\detokenize{#1}\relax
\else
\stepcounter{alternatives}\thealternatives)~~\@ifnextchar {_}{}#1
\hspace{\alternativessize}
\fi
\expandafter\tabiii
\fi
}
\makeatother
When tabiii is called, it detokenize the sequence:
aaaaa\\
bbbbb\\
ccccc\\
ddddd\\
and so on, putting a marker a), b), c) in front of each line. I need to count the number of \, OR get the count of the \thealternative. Reason: I'm trying to make a code that cant put 4 or 5 alternatives in the same line, but I need to know the string size BEFORE TeX read it. This is why I'm calling tabiii and tabiiii: tabiiii reads the sentence to calculate the size of the spaces, and tabiii prints the content on the PDF file. Any ideas?
\indicates the beginning of a control sequence to LaTeX. – Peter Grill May 24 '12 at 5:09a\\ \foo\\ creturn? This seems an XY question, however: what's the real problem you want to solve? – egreg May 24 '12 at 13:17