![使用 \Writetofile 时包答案出现错误](https://linux22.com/image/411360/%E4%BD%BF%E7%94%A8%20%5CWritetofile%20%E6%97%B6%E5%8C%85%E7%AD%94%E6%A1%88%E5%87%BA%E7%8E%B0%E9%94%99%E8%AF%AF.png)
编译此文件
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{answers}
\Newassociation{sol}{Solucao}{solucoes}
\newtheorem{exer}{Exercício}
\begin{document}
\section{Exercícios}
\Opensolutionfile{solucoes}
\Writetofile{solucoes}{\protect\section{Soluções}}
\begin{exer}
Determina
\begin{sol}
O resultado é
\end{sol}
\end{exer}
\Closesolutionfile{solucoes}
\input{solucoes}
\end{document}
我在日志文件中看到以下内容
./exemplo.tex:12: Bad space factor (0).
<recently read> \@savsf
l.12 ...le{solucoes}{\protect\section{Soluções}}
I allow only values in the range 1..32767 here.
在输出文件中它出现一个\nobreak
。
我正在使用 TeX Live 2020/W32TeX。
我该如何解决这个问题?
答案1
这是一个在错误的地方answers.sty
执行的错误。\let\protect\string
这是一个解决办法。
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{answers}
\Newassociation{sol}{Solucao}{\jobname-solucoes}
\newtheorem{exer}{Exercício}
\makeatletter
\renewcommand{\Writetofile}[2]{%
\@bsphack
\Iffileundefined{#1}{}{%
\Ifopen{#1}{%
{%
\begingroup
%\let\protect\string %%% <----- removed
\Ifanswerfiles{%
\protected@iwrite{\@nameuse{#1@file}}{\afterassignment\let@protect@string}{#2}%
}{}%
\endgroup
}%
}{}%
}%
\@esphack
}
\def\let@protect@string{\let\protect\string}
\makeatother
\begin{document}
\section{Exercícios}
\Opensolutionfile{\jobname-solucoes}
\Writetofile{\jobname-solucoes}{\protect\section{Soluções}}
\begin{exer}
Determina
\begin{sol}
O resultado é
\end{sol}
\end{exer}
\Closesolutionfile{\jobname-solucoes}
\input{\jobname-solucoes}
\end{document}