Sign up ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Here is a sample of code I am working on:



\StrLen{123456}\\                                    %Prints 6      
\ifthenelse{\equal{6}{6}}{TRUE}{FALSE}\\              %Prints True
%\ifthenelse{\equal{\StrLen{123456}}{6}}{TRUE}{FALSE} %Want to print true


I am trying to nest a command inside another command. In this case the \StrLen command inside the \ifthenelse command. Is there a way to get around the problem I am running into?

share|improve this question

migrated from Apr 9 '11 at 21:00

This question came from our site for professional and enthusiast programmers.

2 Answers 2

The problem with your attempt and the solution are explained in the xstring documentation:

The macros of this package are not purely expandable, i.e. they cannot be put in the argument of an \edef. Nestling macros is not possible neither.

For this reason, all the macros returning a result (i.e. all excepted the tests) have an optional argument in last position. The syntax is [ name ], where name is the name of the control sequence that will receive the result of the macro: the assignment is made with an \edef which make the result of the macro name purely expandable. Of course, if an optional argument is present, the macro does not display anything.

Thus, the solution is to use the optional argument of \StrLen to store the length in a command and then use this command inside ifthenelse:


  \StrLen{#1}[\MyStrLen]% we find the length of the string and store it in \MyStrLen
  \ifthenelse{\equal{\MyStrLen}{6}}% we compare the length of the string with 6



share|improve this answer

Since you're testing numbers, you want

\ifthenelse{\StrLen{123456} = 6}{TRUE}{FALSE}
share|improve this answer
This doesn't work because \StrLen isn't fully expandable, i.e. requires assignments. – Martin Scharrer Apr 9 '11 at 22:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.