Let's say I write a LaTeX document that usually is compiled with xelatex (and polyglossia), but I'd like to have pdflatex (and babel) as a fallback (when I send it to someone who might not have xelatex installed). For this I'd write \textspanish{}
in the document and I came up with the following redefinition (wrapped basically in if pdflatex
):
\newcommand{\textspanish}[2][]{\foreignlanguage{spanish}{#2}}
This approach seems to work fine for most languages, but for some reason babel has already a \textspanish
command defined. So I tried the same with \renewcommand
but this went into infinite recursion or something (TeX capacity exceeded, sorry [grouping levels=255]
) for a reason I don't understand.
Sample document (works with xelatex, fails with pdflatex):
\documentclass[english,]{article}
\usepackage{lmodern}
\usepackage{ifxetex,ifluatex}
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\usepackage[shorthands=off,spanish,english]{babel}
\newcommand{\textspanish}[2][]{\foreignlanguage{spanish}{#2}}
\else
\usepackage{polyglossia}
\setmainlanguage[]{english}
\setotherlanguage[]{spanish}
\fi
\begin{document}
Hello \textspanish{Hola}
\end{document}
\textspanish
, which exactly does the same as you want to obtain.