TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to check if a string is only of one char, and I ran into this problem: LaTeX complains that \StrLeft in the comparison takes and additional argument:

! Argument of \@secondoftwo has an extra }.
<inserted text> 
l.11 \IfStrEq{A}{\StrLeft{AB}{1}}

However, it doesn't in the first one.






I know I could take an additional third input and set a variable, but why is it not possible to use it like this?

share|improve this question
Using it as such with the last optional parameter to allow you to store the result: \StrLeft{AB}{1}[\LeftChar] \IfStrEq{A}{\LeftChar}{True}{False}. Alternatively, if you just want to check the first char you could use \IfBeginWith. – Peter Grill Apr 25 '12 at 18:19
Yes, but why doesn't it work in that special case? – Henrik Hansen Apr 25 '12 at 18:23
Actually I want to check if the string is only of one char – Henrik Hansen Apr 25 '12 at 18:24
Best way to check if it is one char would be: \StrLen{AB}[\length] \IfEq{\length}{1}{one char}{not one char}. I can't properly explain they "why" part, so will leave that to the real experts here. – Peter Grill Apr 25 '12 at 18:27
I guess \IfStrEq requires expandable input and \StrLeft isn't fully expandable. Therefore it breaks and the error is caused. – Martin Scharrer Apr 25 '12 at 18:29
up vote 6 down vote accepted

The "real" reason why


doesn't work is that \StrLeft{AB}{1} produces the set of instructions necessary to extract the first token from the string AB, not the first token itself, while \IfStrEq needs to see the token.

Such instructions aren't made only by "expandable macros", because some computations need to be performed and this involves assignment of values to registers, thus cannot be evaluated by \IfStrEq. This is essentially the reason why all xstring commands have an optional last argument where to store the result of an extraction or substitution.

In your case a two step procedure must be used:


will work, because \IfStrEq normally does expansion and \temp just contains the first token got from the string AB.

share|improve this answer

As Martin Scharrer said, you are using non expandable constructs so they do not work in all contexts, even if an error is avoided you need to decide if \def\temp{a}\temp is "equal" to "a" (and so have string length 1) or not. Either answer is reasonable but the coding required is completely different depending on what strings you want to consider, and what definition of equality you want.

Here's a simple expandable test just using TeX primitives that gives one definition of a test for string length 1:

$ tex one
This is TeX, Version 3.1415926 (Web2C 2010)
: length 0
a: length 1
abc: longer than 1
abcd: longer than 1
No pages of output.

produced by:

\def\isone#1{\immediate\write20{#1: \xisone#1{}{}\isone}}

     length 0%
     length 1%
     longer than 1%





share|improve this answer

Your Answer


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.