# babel + xdef compile error

The following code throws the error

Incomplete \iffalse; all text was ignored after line 7


when I enable the babel package.

\documentclass[a4paper,12pt]{book}
\usepackage[latin1]{inputenc}
%\usepackage[italian]{babel} % uncomment me
\begin{document}
\label{testlabel}
\xdef\mytest{}
\xdef\mytest{\mytest \ref{testlabel}}
\end{document}


To provide a bit of context of what I'm trying to accomplish, see the more verbose example below:

\documentclass[a4paper,12pt]{book}
\usepackage[latin1]{inputenc}
%\usepackage[italian]{babel} % enabling this results in an error
\usepackage{enumerate}

\begin{document}

\newcommand{\mycorrect}{\label{risp:\theqcnt}}

\newenvironment{questions}
{\section*{Questions}\vspace{2em}}

\newcounter{qcnt}
\setcounter{qcnt}{1}
\newenvironment{question}[3]
{\noindent\textbf{Question \theqcnt:}
\begin{enumerate}[(a)]
\item #1
\item #2
\item #3
\end{enumerate}
\myappend{\arabic{qcnt}\ref{risp:\theqcnt} }
}
{\vspace{2em}\stepcounter{qcnt}}

\begin{questions}
\begin{question}{first\mycorrect}{second}{third}\end{question}
\begin{question}{first}{second\mycorrect}{third}\end{question}
\begin{question}{first}{second}{third\mycorrect}\end{question}
\end{questions}

\end{document}

• @DavidCarlisle true. I'll delete that comment (and alos this one). – Gonzalo Medina Apr 17 '13 at 15:05

You should never use \edef or \xdef on latex input, you can only use them in controlled situations when you know the tokens involved. You can use the \protected@edef or here where you are appending to a macro use

\makeatletter
\makeatother


You want to expand only \theqcnt; so use \protected@xdef to avoid problems with commands that don't survive \edef, but expand \myanswers only once and not \label.

\documentclass[a4paper,12pt]{book}
\usepackage[latin1]{inputenc}
\usepackage[italian]{babel} % enabling this results in an error
\usepackage{enumerate}

\begin{document}

\makeatletter
\newcommand{\myappend}[1]{%
\makeatother
\newcommand{\mycorrect}{\protect\label{risp:\theqcnt}}

\newenvironment{questions}
{\section*{Questions}\vspace{2em}}

\newcounter{qcnt}
\setcounter{qcnt}{1}

\newenvironment{question}[3]
{\noindent\textbf{Question \theqcnt:}
\begin{enumerate}[(a)]
\item #1
\item #2
\item #3
\end{enumerate}
\myappend{\arabic{qcnt}\ref{risp:\theqcnt} }%
}
{\vspace{2em}\stepcounter{qcnt}}

\begin{questions}
\begin{question}{first\mycorrect}{second}{third}\end{question}
\begin{question}{first}{second\mycorrect}{third}\end{question}
\begin{question}{first}{second}{third\mycorrect}\end{question}
\end{questions}

\end{document}