我想将所有定理的主体复制到一个新的 latex 文件中。但是我遇到了错误。请帮帮我!
\documentclass[12pt,a4paper,oneside]{book}
\usepackage{amsmath,mathrsfs,amsfonts,amssymb,etoolbox,environ}
\newtheorem{theo}{Theorem}
\newtheorem{theox}{Theorem}
\newenvironment{copyTheorem}{
\newwrite\copyfile
\immediate\openout\copyfile=xTheorem.tex
}{%
\immediate\closeout\copyfile
}
\AtBeginEnvironment{theo}{
\immediate\write\copyfile{\string\begin{theox}}
\immediate\write\copyfile{\unexpanded\expandafter{\BODY}}
\immediate\write\copyfile{\string\end{theox}}
}%end ex
\begin{document}
\begin{copyTheorem}
\begin{theo}
contents 1111111 111111
\end{theo}
\begin{theo}
contents 22222222 222222
\end{theo}
\end{copyTheorem}
\end{document}
答案1
该extract
包可以提取环境。
此 MWE
\documentclass[12pt,a4paper,oneside]{book}
\usepackage{amsmath,mathrsfs,amsfonts,amssymb,etoolbox,environ}
\usepackage[
active, % switch on extracting
generate=copyfile, % name of the output .tex file
extract-env=theo % name of the environment
]{extract}
\newtheorem{theo}{Theorem}
\newtheorem{theox}{Theorem}
\begin{document}
\begin{theo}
contents 1111111 111111
$\alpha$
\end{theo}
\begin{theo}
contents 22222222 222222
\end{theo}
\end{document}
生成此提取文件:
%%
%% This is file, `copyfile.tex',
%% generated with the extract package.
%%
%% Generated on : 2020/03/17,20:43
%% From source : environment_outputB.tex
%% Using options: active,generate=copyfile,extract-env=theo
%%
\documentclass[12pt,a4paper,oneside]{book}
\begin{document}
\begin{theo}
contents 1111111 111111
$\alpha$
\end{theo}
\begin{theo}
contents 22222222 222222
\end{theo}
\end{document}
该软件包还可以将前言写入目标文件,以便使用 latex 进行编译。还可以提取命令。
关于使用 newwrite 的问题仍然没有答案。
原始回应:
没有答案,因为我还不够了解。
它看起来像是\BODY
由命令定义的\NewEnviron
(适用于文档作者):
平均能量损失
\documentclass[12pt,a4paper,oneside]{book}
\usepackage{amsmath,mathrsfs,amsfonts,amssymb,etoolbox,environ}
%\usepackage{etoolbox,environ}
%\newtheorem{theo}{Theorem}
%\newtheorem{theox}{Theorem}
\NewEnviron{theo}{ \immediate\write\copyfile{\string\begin{theox}}
\immediate\write\copyfile{\unexpanded\expandafter{\BODY}}
\immediate\write\copyfile{xxx}
\immediate\write\copyfile{\string\end{theox}}}[]
%\NewEnviron{theox}{Theorem}[]
\newenvironment{copyTheorem}{
\newwrite\copyfile
\immediate\openout\copyfile=xTheorem.tex
}{%
\immediate\closeout\copyfile
}
%%\AtBeginEnvironment{theo}{%
%% \immediate\write\copyfile{\string\begin{theox}}
%%% \immediate\write\copyfile{\unexpanded\expandafter{\BODY}}
%%% xxx \BODY xxx
%% \immediate\write\copyfile{\expandafter\detokenize\expandafter{\BODY}}
%% \immediate\write\copyfile{xxx}
%% \immediate\write\copyfile{\string\end{theox}}
%%}%end ex
\begin{document}
\begin{copyTheorem}
\begin{theo}
contents 1111111 111111
$\alpha$
\end{theo}
\begin{theo}
contents 22222222 222222
\end{theo}
\end{copyTheorem}
\end{document}
该xTheorem.tex
文件的内容为:
\begin{theox}
contents 1111111 111111 $\alpha $
xxx
\end{theox}
\begin{theox}
contents 22222222 222222
xxx
\end{theox}
现在,environ
文档中对软件包作者说:“现在,amsmath 为我们定义了 \collect@body。但该软件包可能未加载,我们不想仅为了这一个宏而加载整个软件包。”等等
因此看起来应该可以使用“长”版本编写宏\Collect@Body
。
答案2
您可以使用xparse
。
\documentclass[12pt,a4paper,oneside]{book}
\usepackage{amsmath,mathrsfs,amssymb}
\usepackage{xparse} % better than environ
\newtheorem{theoreminner}{Theorem}
\ExplSyntaxOn
% allocate a write stream and the boolean variable
\iow_new:N \g_jack_theorem_iow
\bool_new:N \g_jack_theorem_write_bool
% user level commands
\NewDocumentCommand{\writetheorems}{m}
{% #1 is the file name
% open it
\iow_open:Nn \g_jack_theorem_iow { #1 }
% remember to close it
\AtEndDocument { \iow_close:N \g_jack_theorem_iow }
% enable writing
\bool_gset_true:N \g_jack_theorem_write_bool
}
\NewDocumentEnvironment{theorem}{o+b}
{
% typeset the theorem
\IfNoValueTF{#1}{\begin{theoreminner}}{\begin{theoreminner}[#1]}
#2
\end{theoreminner}
% possibly write it out
\bool_if:NT \g_jack_theorem_write_bool
{% writing is enabled
% first the \begin part
\IfNoValueTF{#1}
{
\iow_now:Nn \g_jack_theorem_iow { \begin{theorem} }
}
{
\iow_now:Nn \g_jack_theorem_iow { \begin{theorem}[#1] }
}
% write the body
\iow_now:Nn \g_jack_theorem_iow { #2 }
% the \end part
\iow_now:Nn \g_jack_theorem_iow { \end{theorem} }
}
}{}
\ExplSyntaxOff
\writetheorems{\jobname-thm}
\begin{document}
\begin{theorem}[Someone]
contents 1111111 111111
\end{theorem}
\begin{theorem}
contents 22222222 222222
\end{theorem}
\end{document}
文件-thm
\begin {theorem}[Someone]
contents 1111111 111111
\end {theorem}
\begin {theorem}
contents 22222222 222222
\end {theorem}