如果子字符串包含变音符号或其他非 ASCII 字符,则似乎IfSubStringInString
会出现问题,如下例所示:
\usepackage[utf8]{inputenc}
\usepackage{substr}
\begin{document}
\IfSubStringInString{Muller}{Müller}{Müller}{no Müller}
\IfSubStringInString{ller}{Müller}{Müller}{no Müller}
\IfSubStringInString{Müller}{Müller}{Müller}{no Müller}
\end{document}
输出结果如下:
no Müller
Müller
ller2
üüllerMüllerMüllerno Müller
显然,前两次呼叫IfSubStringInString
成功,但第三次呼叫失败。
有办法解决这个问题吗?
答案1
是。使用xstring
:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xstring}
\begin{document}
\IfSubStr{Muller}{Müller}{Müller}{no Müller}
\IfSubStr{ller}{Müller}{Müller}{no Müller}
\IfSubStr{Müller}{Müller}{Müller}{no Müller}
\end{document}
答案2
当您在 选项ü
下输入时utf8
,LaTeX 会将其转换为\IeC{\"u}
。该substr
包使用\protected@edef
来存储要查找的字符串和子字符串,但后来它使用了\edef
和 ,\IeC
并且也\"
不能在那里使用。
一种可能的策略是“去标记化”此时获得的标记列表,这些列表无论如何都代表我们必须处理的字符串。好吧,采用这种策略Müller
和M\IeC{\"u}ller
将被视为相同,但这似乎不是一个问题,因为\IeC
永远不应直接在文档中使用。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{substr}
\makeatletter % fix the strings so \edef makes no harm later on
\renewcommand\su@ExpandTwoArgs[3]{%
\protected@edef\su@SubString{#1}%
\protected@edef\su@String{#2}%
% ADDITION: detokenize the strings
\edef\su@SubString{\detokenize\expandafter{\su@SubString}}%
\edef\su@String{\detokenize\expandafter{\su@String}}%
% END OF ADDITION
\expandafter\expandafter\expandafter#3%
\expandafter\expandafter\expandafter{%
\expandafter\su@SubString\expandafter
}\expandafter{\su@String}%
}
\makeatother
\begin{document}
\IfSubStringInString{Muller}{Müller}{Müller}{no Müller}
\IfSubStringInString{ller}{Müller}{Müller}{no Müller}
\IfSubStringInString{Müller}{Müller}{Müller}{no Müller}
\end{document}