使用 \Writetofile 时包答案出现错误

使用 \Writetofile 时包答案出现错误

编译此文件

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

相关内容