[Tex/LaTex] Automatically Detect Foreign Language for Hyphenation

babelhyphenationlanguages

Is there a LaTeX package that automatically detects which foreign language a particular part of a multi-language TeX document is so that it can hyphenate it appropriately, or do I have to manually specify the foreign language, using, e.g., the Babel package's command \foreignlanguage{}?

Best Answer

No, but there are things you can do that makes it less of a burden. It's not that common that a document switches language just like that, since that can be confusing not only for LaTeX, but for readers as well. Often the foreign text is divided from the text around it in some way that you have markup for anyway. Then let that markup do the language switch for you as well!

One simple example is italicized foreign words. Say you are writing in English about something Swedish and therefore use some foreign words in your text.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[swedish,american]{babel}
\newcommand\swe[1]{\foreignlanguage{swedish}{\emph{#1}}}

\begin{document}
When celebrating \swe{Valborgsmässoafton} (Walpurgis Night) the Swedes
\dots
\end{document}

The point is that you would have needed markup for those words anyway. Another common example is quotes in a foreign language. The package csquotes has some readymade commands for that. You can with advantage use these in your own shorter macros that are for the languages you happen to use, like \swe above.

Related Question