我需要比较两个字符(或字符串),但我不能:
请问你能帮帮我吗!!
\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}
答案1
使用\def\bit{\StrMid{\word}{5}{5}}
,宏\bit
是不是0 或 1,但它是印刷0 或 1。
您应该使用可选参数\StrMid
(几个函数共用xstrings
):
\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}
答案2
expl3
使用字符串范围函数的解决方案
\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}
请注意,这使用了“真实”的 TeX 字符串(IE\midstr
/的结果\Mistr
仅为类别代码 12 的标记,空格除外,其类别代码为 10)。