6

I need to count number of words in a macro defined as follow:

\def\columnNames{MATRIX, METHODA, METHODB}

I manually set number of words as follow:

\pgfmathsetmacro\numberOfColumns{3}

I need an automatic way to find number of words in a macro. The list is separated with commas ",".

I have found several solutions (1,2,...). However, I was not able to adapt to my situation.

1 Answer 1

7

The 'word count' element here is not important: all you require is a method to find the 'length' of a comma-separated list. That's relatively easy to do, but as expl3 provides a pre-defined solution I'd use that approach

\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn
\cs_new_eq:NN \commalistlength \clist_count:N
\ExplSyntaxOff
\usepackage{pgf}

\begin{document}
\def\columnNames{MATRIX, METHODA, METHODB}
\pgfmathsetmacro\numberOfColumns{\commalistlength{\columnNames}}
\show\numberOfColumns
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .