3

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.

Naser
  • 31
  • Welcome to TeX.SE. Please provide a compilable document not just inline fragments –  Sep 09 '17 at 06: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 09 '17 at 06:22
  • 4
    That's not a compilable document. It's a line. A document begins with \documentclass{article} \usepackage{xstring} \begin{document}...\end{document} –  Sep 09 '17 at 06:23
  • Related: https://tex.stackexchange.com/questions/59565/on-testing-two-fully-expanded-character-strings-for-equality (But please tell me someone where can I find the question about MWE... I can never find it when need it) – koleygr Sep 09 '17 at 06: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? –  Sep 09 '17 at 06:30
  • \documentclass[12pt]{article} \usepackage{xstring} \usepackage{xy} \usepackage{color} \renewcommand{\baselinestretch}{1} \oddsidemargin=0.5cm \evensidemargin=0.5cm \textwidth=16cm \topmargin=0cm \textheight=21.6cm

    \begin{document} \newdimen\qsum \newread\Karnameh\immediate\openin\Karnameh=Karnameh \readline\Karnameh to \a \StrBetween[6,7]{ \a }{ }{ }[\itstr] itstr=$|\itstr|$ \IfStrEq{\itstr}{Q}{\global\qsum=1pt}{\global\qsum=5pt} qsum=\the\qsum\end{document}

    – Naser Sep 09 '17 at 06:41
  • Karnameh is a file that contains one line, which is: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 – Naser Sep 09 '17 at 06:44
  • I tried \space, same problem. – Naser Sep 09 '17 at 06:56
  • I used geometry package, no success. Do I need to change or enter any other parameters? – Naser Sep 09 '17 at 07:06
  • Try to change from \readline to \read and use \space as suggested above –  Sep 09 '17 at 07:08
  • @Naser: Please consider to accept my solution then. –  Sep 10 '17 at 09:48

1 Answers1

6

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 
\newread\Karnameh
\immediate\openin\Karnameh=Karnameh 
\read\Karnameh to \a 
\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

enter image description here

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}