# The \IfStrEq (from xstring package) does not work properly

I read a file line to \a. Say \a equals A Q B with a space before A and after B. Then I use \StrBetween[2,3]{\a}{ }{ }[\itstr], so that \itstr equals Q. Now \IfStrEq{\itstr}{Q}{true}{false} should be true, but it is not. Would you help me with this please.

• Welcome to TeX.SE. Please provide a compilable document not just inline fragments – user31729 Sep 9 '17 at 6:20
• I have an \afile whose first line is A Q B. \readline\afile to \a (\a=" A Q B ") \StrBetween[2,3]{\a}{ }{ }[\itstr] (\itstr="Q") \IfStrEq{\itstr}{Q}{True}{False} – Naser Sep 9 '17 at 6:22
• That's not a compilable document. It's a line. A document begins with \documentclass{article} \usepackage{xstring} \begin{document}...\end{document} – user31729 Sep 9 '17 at 6:23
• Related: tex.stackexchange.com/questions/59565/… (But please tell me someone where can I find the question about MWE... I can never find it when need it) – koleygr Sep 9 '17 at 6:26
• Shouldn't it be \StrBetween[1,2]{\a}{\space}{\space}[\itstr] rather, in order to catch the content between the first (1) and second (2) occurcence of the explicit space tokens in the string, so \itstr should expand to Q? – user31729 Sep 9 '17 at 6:30

If letters or strings should be read and compared later with xstring macros \read should be used instead of \readline, the latter detokenizing (i.e. changing the catcodes to 12 for all characters except of character 32 which will get catcode 10 the content, which is not wanted here. \read maintains the catcodes, so characters belonging to 'letter' will have this category later on still.

Also explicit \space must be used instead of empty {} or { } in order to match the content.

\documentclass[12pt]{article}
\usepackage{xstring}
\begin{document}
\newdimen\qsum
\immediate\openin\Karnameh=Karnameh
\closein\Karnameh
\StrBetween[6,7]{\a}{\space}{\space}[\itstr]%
\fbox{\itstr}

\IfStrEq{\itstr}{Q}{\global\qsum=1pt}{\global\qsum=5pt}

qsum=\the\qsum
\end{document}


Please note that \read will also grab the \endlinechar here as well, but let us ignore this for a moment.

This will give 1pt for the \qsum dim register.

File Karmaneh

StNumber LastName FirstName Dotssss D Q Q Q A A A Q A Examm A Q A A Q Q A A Examm Q Q Q A A A A Q A Q Examm Examm ExCr


Update

If \readline is a 'must', use an explicit \detokenize for the Q character that is handled to the \IfStrEq macro.

\IfStrEq{\itstr}{\detokenize{Q}}{\global\qsum=1pt}{\global\qsum=5pt}