我有一篇很长的文档,里面有很多方程、定理、定义等。它们位于不同的环境中,例如对于定义,我有
\newtheorem{definition}{Definition}
\begin{definition}
...
\end{defintion}
我想要做的是制作一个文档,其中只包含主文件中出现的所有定义(定义环境中出现的内容),不包含任何其他文本。有办法做到这一点吗?
答案1
您可以定义definition
环境,以便它在抓取可保存在辅助文件中以供另一个文件输入的文本后调用“真实”环境。
写出操作是在排版之后进行的,因此我们可以使用\thedefinitioninner
,它将具有正确的值。
主文件bedge.tex
\documentclass{book}
\usepackage{amsthm}
\theoremstyle{definition}
\newtheorem{definitioninner}{Definition}[section]
\ExplSyntaxOn
% the definition environment grabs the environments
% contents, including possibly the optional argument
\NewDocumentEnvironment{definition}{o +b}
{
\bedge_definition:nn { #1 } { #2 }
}
{}
% allocate a stream for writing the definition texts
\iow_new:N \g_bedge_definition_iow
% and open it
\iow_open:Nn \g_bedge_definition_iow { \c_sys_jobname_str.defn }
% and close it at the end
\AtEndDocument{\iow_close:N \g_bedge_definition_iow}
% the main command
\cs_new_protected:Nn \bedge_definition:nn
{% typeset the definition and then write the contents
\tl_if_novalue:nTF { #1 }
{% no optional argument
\begin{definitioninner} #2 \end{definitioninner}
\__bedge_definition_write:en { \thedefinitioninner } { #2 }
}
{% optional argument
\begin{definitioninner}[#1] #2 \end{definitioninner}
\__bedge_definition_write:enn { \thedefinitioninner } { #1 } { #2 }
}
}
% no optional argument
\cs_new_protected:Nn \__bedge_definition_write:nn
{
\iow_now:Nn \g_bedge_definition_iow { \saveddefinition{#1}{#2} }
}
\cs_generate_variant:Nn \__bedge_definition_write:nn { e }
% optional argument
\cs_new_protected:Nn \__bedge_definition_write:nnn
{
\iow_now:Nn \g_bedge_definition_iow { \saveddefinition{#1}[#2]{#3} }
}
\cs_generate_variant:Nn \__bedge_definition_write:nnn { e }
\ExplSyntaxOff
\begin{document}
\mainmatter
\chapter{First}
\section{Test}
\begin{definition}
This is an unnamed definition.
\end{definition}
\begin{definition}[Foo]
This is a named definition.
\end{definition}
\section{Another}
\begin{definition}[Foo]
This is a named definition.
\end{definition}
\chapter{Second}
\section{Test}
\begin{definition}
This is an unnamed definition.
\end{definition}
\begin{definition}[Foo]
This is a named definition.
\end{definition}
\section{Another}
\begin{definition}[Foo]
This is a named definition.
\end{definition}
\end{document}
二级文件bedge-defn.tex
\documentclass{article}
\usepackage{amsthm}
\theoremstyle{definition}
\newtheorem*{definition}{Definition \thisdefinitionnumber}
\newcommand{\thisdefinitionnumber}{} % initialize
\NewDocumentCommand{\saveddefinition}{m o +m}{%
\renewcommand{\thisdefinitionnumber}{#1}%
\IfNoValueTF{#2}
{\begin{definition}#3\end{definition}}
{\begin{definition}[#2]#3\end{definition}}%
}
\begin{document}
All the definitions in the main document follow.
\bigskip
\input{bedge.defn}
\end{document}
如果我在第一个文件上运行 LaTeX,bedge.defn
则会生成一个名为的文件(名称与第一个文件相同)
\saveddefinition {1.1.1}{This is an unnamed definition.}
\saveddefinition {1.1.2}[Foo]{This is a named definition.}
\saveddefinition {1.2.1}[Foo]{This is a named definition.}
\saveddefinition {2.1.1}{This is an unnamed definition.}
\saveddefinition {2.1.2}[Foo]{This is a named definition.}
\saveddefinition {2.2.1}[Foo]{This is a named definition.}
在辅助文件上运行 LaTeX 的结果是