\foreach and \StrLen

I don´t know how to make a foreach with a StrLen inside it, is it possible?

\documentclass[a4paper,12pt,hidelinks]{article}

\usepackage[utf8]{inputenc}
\usepackage[brazil]{babel}
\usepackage[T1]{fontenc}

\usepackage{filecontents}
\usepackage{csvsimple}
\usepackage{xstring}
\usepackage{alphalph}
\usepackage{etoolbox}
\usepackage{tikz}

\newcommand \nveics {2}

\newcommand \alfanumchassi {136564765}
\newcommand \alfanumchassib {36564765}

\ifdef{\alfanumchassi}{\StrLen{\alfanumchassi}[\chassi]}{}
\StrLen{\alfanumchassib}[\chassib]

% \foreach \n in {b,...,\alphalph{\nveics}}{
% \IfStrEq{\csname \x \endcsname}{s}{\global\renewcommand \furtodanoimovel {n}}{}
% }

\begin{document}

{\chassi}\\

{\chassib}\\

\end{document}


If I comment "\StrLen{\alfanumchassib}[\chassib]" and uncomment "\foreach \n in {b,...,\alphalph{\nveics}}{..." I have a lot of errors.

• Could you tell us what you're trying to do? One thing I noticed is that \csname\x\endcsname is kind of weird. Either you'll get the unusual control sequence \\x or you wanted to write \expandafer\csname\x\endcsname to get a control sequence named by the string returned by \x. But where do you expect \x to be defined? I'm confused. – wrtlprnft Apr 8 '16 at 22:01
• @wrtlprnft \csname does full expansion until finding the matching \endcsname; no need to use \expandafter. – egreg Apr 8 '16 at 23:06
• @egreg: interesting, I'm sure I've seen this construction a couple of times… Thanks for clearing that up! Edit: never mind, that was probably always the other way around (e.g. expanding \csname before a \def). – wrtlprnft Apr 8 '16 at 23:20

It's quite hard to guess what you're trying to do.

1. You have \foreach \n and use \x

2. \csname\x\endcsname is probably an error, because it would usually be something undefined

3. \global\renewcommand won't do a global redefinition

4. In your code \furtodanoimovel is not defined, so \renewcommand would raise an error anyway.

Here is a version that might do what you want:

\documentclass{article}

\usepackage{xstring}
\usepackage{alphalph}
\usepackage{tikz}

\newcommand \nveics {4}

\foreach \x in {b,...,\alphalph{\nveics}}{
\IfStrEq{\x}{c}{\gdef\furtodanoimovel{n}}{}
}

\begin{document}

\texttt{\meaning\furtodanoimovel}

\end{document}


As you see, the macro \furtodanoimovel has been defined.

• I disagree with point 2 (or maybe I'm too tired): isn't \csname x\endcsname the same as \x? You have an extra backslash there. – wrtlprnft Apr 8 '16 at 23:24
• @wrtlprnft Yes, you're right – egreg Apr 8 '16 at 23:35

I'm so sorry, the script inside the foreach was wrong, the correct is to put \StrLen inside it, this gives me an error and I don't know how to make it or using another command similar to \StrLen for this case.

There is a difference in using \x or \n in \foreach? I thinked that I could use any command like \w, \z or anything that doesn't exists.

• (disregarding the fact that this is not an answer) you can use any possible macro name, you just have to be consistent about which you use. \foreach \x in {1,...10} { \n } is not going to work. – wrtlprnft Apr 9 '16 at 16:13