babel + xdef 编译错误

babel + xdef 编译错误

以下代码会引发错误

Incomplete \iffalse; all text was ignored after line 7

当我启用babel包裹。

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

为了提供一些我想要完成的背景信息,请参见下面更详细的示例:

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

\begin{document}

\xdef\myanswers{}
\newcommand{\myappend}[1]{\xdef\myanswers{\myanswers #1}}
\newcommand{\mycorrect}{\label{risp:\theqcnt}}

\newenvironment{questions}
{\section*{Questions}\vspace{2em}}
{\noindent\textbf{Answers: }\myanswers}

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

答案1

你永远不应该在 latex 输入中使用\edef\xdef,你只能在知道所涉及的标记时在受控情况下使用它们。你可以\protected@edef在此处使用或附加到宏的地方使用

\makeatletter
\newcommand{\myappend}[1]{\g@addto@macro\myanswers{#1}}
\makeatother

答案2

您只想扩展\theqcnt;因此使用\protected@xdef以避免出现无法生存的命令问题\edef,但会扩展\myanswers一次并且不是 \label

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

\begin{document}

\def\myanswers{}
\makeatletter
\newcommand{\myappend}[1]{%
  \protected@xdef\myanswers{\unexpanded\expandafter{\myanswers} #1}}
\makeatother
\newcommand{\mycorrect}{\protect\label{risp:\theqcnt}}

\newenvironment{questions}
  {\section*{Questions}\vspace{2em}}
  {\noindent\textbf{Answers: }\myanswers}

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

相关内容