I am writing a document on phonetics using the media9 package to include pronunciations of some of the words in the document. The words that are to be pronounced are in Danish, and thus their spellings include the non-ASCII characters æøå, which LaTeX has never really been comfortable with. What I need is a command pronounce{word}, which writes out word in the document and links to the sound file, if one such exists. If word includes æ, ø, å, or a space, the command first has to ASCIIfy those characters to ae, oe, ae, and - and then link to the sound file with the ASCIIfied name.

\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[
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}


However, I get lots of errors, primarily from xstring. I did try putting \expandafters everywhere, and I did try some of the other expandmodes from xstring. Didn't help much though. Anybody who can see the error?

• \StrSubstitute is not expandable, so you can't use it in the argument of \IfFileExists, for instance. See section 3.2 of the manual. In \asciify, you should assign the end result to a control sequence and use that in the place where you are using \asciify now. – Stephan Lehmke Nov 29 '13 at 17:42

There are two problems with your MWE.

The first is that it looks like you're using the wrong encoding for the non-ASCII characters.

Change

\usepackage[latin1]{inputenc}


to

 \usepackage[utf8]{inputenc}


xstring should not have problems anymore.

The second is that there's a problem of expansion. The following should work. But you can get around this by using a macro to store the expansion of the last substitution and calling that macro later. (This is per the expansion instructions in section 3.2 of the manual.)

\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[
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}

• In fact, latin1 does work and is just a less rich alternative to utf8. However, your other fix solved everything. Thanks! :-) – Gaussler Nov 29 '13 at 18:16
• @Gaussler That's interesting because I can't get the MWE to compile using latin1. But, I'm glad to hear that the rest of the answer worked for you. – A.Ellett Nov 29 '13 at 18:19
• Might be because you pasted it into a document that was encoded in utf8 or something like that. What matters is that everyone is happy. :-) – Gaussler Nov 29 '13 at 18:59