我正在开发一个系统,用于在排版文档的不同位置提供练习提示。当前的方法是基于将提示环境的内容保存到文件中。这种方法可行,但在用于覆盖多行的逐字内容时会出现问题。问题似乎是在将内容写入文件之前,提示环境主体中的换行符被删除了。
运行问题末尾给出的代码后,我希望以下内容位于文件“hintfile.tex”中
Example
d
with
multiple lines.
运行代码后文件内容如下
Example d with multiple lines.
以下是示例代码
\documentclass{article}
\ExplSyntaxOn
% Open a file for writing
\iow_new:N \g_hintfile_iow
\iow_open:Nn \g_hintfile_iow {hintfile.tex}
% Create an environment that saves the body
% to the opened file
\NewDocumentEnvironment{hint}{O{}+b}{%
\iow_now:Nn \g_hintfile_iow { #2 }
}{}
% Create command to close the file
\NewDocumentCommand{\closehintandsolutionfile}{}{
\iow_close:N \g_hintfile_iow
}
\ExplSyntaxOff
\begin{document}
Hello world
\begin{hint}
Example
d
with
multiple lines.
\end{hint}
\begin{verbatim}
Example
d
with
multiple lines.
\end{verbatim}
\section{From external file}
\closehintandsolutionfile
\input{hintfile}
\end{document}
答案1
类似这样的方法可能对您有用,您可以将内容存储在内存或外部文件中:
% arara: pdflatex
% arara: clean: { extensions: [ aux, log] }
\documentclass[10pt]{article}
\usepackage{scontents}
\newenvsc{hint}[store-env=hint,print-env=false]
\begin{document}
Hello world
% hiden and write
\begin{hint}[write-env=hint1.txt]
Example
d
with
multiple lines.
\end{hint}
\begin{verbatim}
Example
d
with
multiple lines.
\end{verbatim}
\section{From memory}
\typestored[1]{hint}
\section{From file}
\input{hint1.txt}
\end{document}
我建议你阅读一下文档。Saludos
答案2
类似这样的事?
\documentclass{article}
\usepackage{xparse} % added
\ExplSyntaxOn
% Open a file for writing
\iow_new:N \g_hintfile_iow
\iow_open:Nn \g_hintfile_iow {hintfile.tex}
% Create an environment that saves the body
% to the opened file
\NewDocumentEnvironment{hint}{O{}+b}{%
\iow_now:Nn \g_hintfile_iow {\setlength{\parindent}{0pt} #2 }
}{}
% Create command to close the file
\NewDocumentCommand{\closehintandsolutionfile}{}{
\iow_close:N \g_hintfile_iow
}
\ExplSyntaxOff
\begin{document}
Hello world
\begin{hint}
Example \\
d \\
with \\
multiple lines.
\end{hint}
\begin{verbatim}
Example
d
with
multiple lines.
\end{verbatim}
\section{From external file}
\closehintandsolutionfile
\input{hintfile}
\end{document}