1

I need to compare two char (or string), but I can't:

could you help me, please!!

\documentclass[a4paper,openright,twoside]{book}
\usepackage{xstring,ifthen}

\begin{document}
\def\cero{\StrMid{01}{1}{1}}
\def\uno{\StrMid{01}{2}{2}}

\def\word{101010101}
\def\bit{\StrMid{\word}{5}{5}}


this is cero: \cero \\
this is one: \uno


this is the word: \word \\
this is the 5th bit: \bit

.... but I cant't compare them!!

\ifthenelse{\equal{\bit}{\uno}}
    {this is 1}
    {this is 0}
\end{document}
2

With \def\bit{\StrMid{\word}{5}{5}}, the macro \bit is not either 0 or 1, but it's the instructions for printing either 0 or 1.

You should use the optional argument to \StrMid (common to several xstrings functions):

\documentclass[a4paper,openright,twoside]{book}
\usepackage{xstring,ifthen}

\begin{document}
\StrMid{01}{1}{1}[\cero]
\StrMid{01}{2}{2}[\uno]

\def\word{101010101}
\StrMid{\word}{5}{5}[\bit]

this is cero: \cero

this is one: \uno

this is the word: \word

this is the 5th bit: \bit

\ifthenelse{\equal{\bit}{\uno}}
    {this is 1}
    {this is 0}

\end{document}

enter image description here

2

A solution using expl3's string range function

\documentclass{book}
\usepackage{expl3}
\usepackage{ifthen}
\ExplSyntaxOn
\cs_new_eq:NN \midstr \str_range:nnn
\cs_new_eq:NN \Midstr \str_range:Nnn
\ExplSyntaxOff
\begin{document}
\def\cero{\midstr{01}{1}{1}}
\def\uno{\midstr{01}{2}{2}}

\def\word{101010101}
\def\bit{\Midstr{\word}{5}{5}}


this is cero: \cero \\
this is one: \uno


this is the word: \word \\
this is the 5th bit: \bit

.... but I cant't compare them!!

\ifthenelse{\equal{\bit}{\uno}}
    {this is 1}
    {this is 0}

\end{document}

Note that this uses 'real' TeX strings (i.e. the results from \midstr/\Mistr are tokens of category code 12 only, except for spaces which are catcode 10).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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