![转换为 ASCII 字符后加载文件](https://linux22.com/image/279993/%E8%BD%AC%E6%8D%A2%E4%B8%BA%20ASCII%20%E5%AD%97%E7%AC%A6%E5%90%8E%E5%8A%A0%E8%BD%BD%E6%96%87%E4%BB%B6.png)
我正在编写一份关于语音的文档,使用media9
软件包来包含文档中某些单词的发音。要发音的单词是丹麦语,因此它们的拼写包括非 ASCII 字符æøå
,而 LaTeX 对此一直不太适应。我需要一个命令,它在文档中pronounce{word}
写出并链接到声音文件(如果存在)。如果包含、、或空格,则命令首先必须将这些字符 ASCII 化为、、和,然后链接到具有 ASCII化名称的声音文件。word
word
æ
ø
å
ae
oe
ae
-
\documentclass{memoir}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath,amssymb,geometry,graphicx,amstext}
\usepackage{media9}
\newcommand\listen[2]{ % links to the file #1, then writes out #2
\includemedia[
addresource=#1,
transparent,
flashvars={
source=#1
&autoPlay=true
&hideBar=true
},
]{#2}{APlayer.swf}
}
\usepackage{xstring}
\newcommand*\asciify[1]{% converts æøå and space to ASCII characters and "-"
\StrSubstitute{#1}{æ}{ae}[\tempone]%
\StrSubstitute{\tempone}{ø}{oe}[\temptwo]%
\StrSubstitute{\temptwo}{å}{aa}[\tempthree]%
\StrSubstitute{\tempthree}{ }{-}%
}
\newcommand*\pronounce[1]{% the final command that is the issue here
\IfFileExists{\asciify{#1}.mp3}{% only include the file if it exists
\listen{\asciify{#1}.mp3}{#1}% ASCIIfy the word, then include the file and write out the original word
}{%
#1% else, just write out the word
}%
}
\begin{document}
\pronounce{være}
\end{document}
但是,我收到很多错误,主要是来自xstring
。我尝试过将 放在\expandafter
任何地方,也尝试过 中的其他一些扩展模式xstring
。但没有什么帮助。有谁能看到这个错误?
答案1
您的 MWE 有两个问题。
首先,您似乎对非 ASCII 字符使用了错误的编码。
改变
\usepackage[latin1]{inputenc}
到
\usepackage[utf8]{inputenc}
xstring
应该不会再有问题了。
第二个是存在扩展问题。下面的方法应该可以工作。但是你可以通过使用宏来存储最后一次替换的扩展并稍后调用该宏来解决这个问题。(这是根据手册第 3.2 节中的扩展说明。)
\documentclass{memoir}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath,amssymb,geometry,graphicx,amstext}
\usepackage{xstring}
\usepackage{media9}
\newcommand\listen[2]{ % links to the file #1, then writes out #2
\includemedia[
addresource=#1,
transparent,
flashvars={
source=#1
&autoPlay=true
&hideBar=true
},
]{#2}{APlayer.swf}
}
\def\mytemp{}%%
\newcommand*\asciify[1]{% converts æøå and space to ASCII characters and "-"
\StrSubstitute{#1}{æ}{ae}[\tempone]%
\StrSubstitute{\tempone}{ø}{oe}[\temptwo]%
\StrSubstitute{\temptwo}{å}{aa}[\tempthree]%
\StrSubstitute{\tempthree}{ }{-}[\mytemp]%
}
\newcommand*\pronounce[1]{% the final command that is the issue here
\asciify{#1}%%
\IfFileExists{\mytemp.mp3}{% only include the file if it exists
\listen{\mytemp}{#1}% ASCIIfy the word, then include the file and write out the original word
}{%
#1% else, just write out the word
}%
}
\begin{document}
Hello
\pronounce{være}
\end{document}