环境中包含部分

环境中包含部分

我正在尝试将 touhami 的解决方案改编为如何从 tex 文件中仅打印出语句(定理、引理等)?这样我也可以打印章节标题。

我的第一个猜测是使用\pretocmd{\section}{\begin{myenv}}{}{}\apptocmd{\section}{\end{myenv}}{}{}。但根据环境开始或结束于标题定义修补似乎不是一个好主意\section

因此我尝试采用 Christian Hupfer 的解决方案来解决这个问题:

\documentclass{article}
\usepackage{lipsum}
\usepackage{etoolbox}
\usepackage{titlesec}

\usepackage{atbegshi}
\newcommand{\handlethispage}{}
\AtBeginShipout{\handlethispage}

\newtheorem{xtheorem}{Theorem}
\newtheorem{xlemma}{Lemma}


\titleformat{\section}{}{\thesection}{0em}{}[]

\newif\ifonly % uncomment next line
\onlytrue   
\ifonly
\usepackage{answers}
\Newassociation{lemma}{mtlemma}{mtfile}
\Newassociation{theorem}{mttheorem}{mtfile}
\Newassociation{sectionenv}{mtsectionenv}{mtfile}
\renewenvironment{mttheorem}{\begin{xtheorem}}{\end{xtheorem}}
\renewenvironment{mtlemma}{\begin{xlemma}}{\end{xlemma}}
\renewenvironment{mtsectionenv}{}{}

\let\LaTeXStandardSection\section
\makeatletter
\newcommand{\unstarredsection@noopt}[1]{%
\unstarredsection@opt[#1]{#1}%
}%

\newcommand{\unstarredsection@opt}[2][]{%
\begin{sectionenv}\relax%
\LaTeXStandardSection{#1}%
\end{sectionenv}
  % 
}%

\newcommand{\unstarredsection}{%
\@ifnextchar[{\unstarredsection@opt}{\unstarredsection@noopt}
}%

\newcommand{\starredsection}[1]{%
\LaTeXStandardChapter*{#1}
}%

\renewcommand{\section}{%
\@ifstar{\starredsection}{\unstarredsection}%
}%


\makeatother

\Opensolutionfile{mtfile}
\AtBeginDocument{%
\let\handlethispage\AtBeginShipoutDiscard}
\AtEndDocument{%
\clearpage\pagenumbering{arabic}
\let\handlethispage\relax
\Closesolutionfile{mtfile}
\Readsolutionfile{mtfile}}
\else
\newenvironment{theorem}{\begin{xtheorem}}{\end{xtheorem}}
\newenvironment{lemma}{\begin{xlemma}}{\end{xlemma}}

\fi


\begin{document}
\lipsum[1]
\begin{lemma}
this is some bla bla in lamma
\end{lemma}

\section{First section}

\lipsum[2]
\begin{theorem}
some bla bla in lamma
\end{theorem}
\lipsum[3]
\begin{lemma}
some bla bla in lamma
\end{lemma}
\end{document}

但是,它仍然不起作用。mtfile.tex 包含以下几行,其中的空格可能是问题(部分原因?):

\begin{mtsectionenv}{}
\LaTeXStandardSection {First section}\end {sectionenv}

知道什么地方出了问题吗?

答案1

这是一个解决方案。对于像命令这样的(子)部分,需要

\pretocmd\@startsection{\Writetofile{mtfile}{\string\csname\space #1\string\endcsname}}{}{} 
\pretocmd\@sect{\Writetofile{mtfile}{[#7]{#8}}}{}{}
\pretocmd\@ssect{\Writetofile{mtfile}{*{#5}}}{}{}

零件

\pretocmd\@part{\Writetofile{mtfile}{\string\part[#1]{#2}}}{}{}
\pretocmd\@spart{\Writetofile{mtfile}{\string\part*{#1}}}{}{}

以及章节

\pretocmd\@chapter{\Writetofile{mtfile}{\string\chapter[#1]{#2}}}{}{}
\pretocmd\@schapter{\Writetofile{mtfile}{\string\chapter*{#1}}}{}{}

我们还需要通过添加来禁用计数器

\AtBeginDocument{%
\let\mtrefstepcounter\refstepcounter
\let\refstepcounter\@gobble

\AtEndDocument{%
\let\refstepcounter\mtrefstepcounter

如果使用目录(toc),则需要输入

\Writetofile{mtfile}{\string\tableofcontents}

例子

\documentclass{book}
\usepackage{lipsum}

\usepackage{atbegshi}
\newcommand{\handlethispage}{}
\AtBeginShipout{\handlethispage}

\newtheorem{xtheorem}{Theorem}
\newtheorem{xlemma}{Lemma}

\newif\ifonly % uncomment next line
%\onlytrue 

\makeatletter  
\ifonly
\usepackage{answers}
\Newassociation{lemma}{mtlemma}{mtfile}
\Newassociation{theorem}{mttheorem}{mtfile}
\renewenvironment{mttheorem}{\begin{xtheorem}}{\end{xtheorem}}
\renewenvironment{mtlemma}{\begin{xlemma}}{\end{xlemma}}
\Opensolutionfile{mtfile}
\usepackage{etoolbox}
\pretocmd\@startsection{\Writetofile{mtfile}{\string\csname\space #1\string\endcsname}}{}{} 
\pretocmd\@sect{\Writetofile{mtfile}{[#7]{#8}}}{}{}
\pretocmd\@ssect{\Writetofile{mtfile}{*{#5}}}{}{}
\pretocmd\@part{\Writetofile{mtfile}{\string\part[#1]{#2}}}{}{}
\pretocmd\@spart{\Writetofile{mtfile}{\string\part*{#1}}}{}{}
\pretocmd\@chapter{\Writetofile{mtfile}{\string\chapter[#1]{#2}}}{}{}
\pretocmd\@schapter{\Writetofile{mtfile}{\string\chapter*{#1}}}{}{}
\AtBeginDocument{%
\let\mtrefstepcounter\refstepcounter
\let\refstepcounter\@gobble
\let\handlethispage\AtBeginShipoutDiscard}
\AtEndDocument{%
\let\refstepcounter\mtrefstepcounter
\clearpage\pagenumbering{arabic}
\let\handlethispage\relax
\Closesolutionfile{mtfile}
\Readsolutionfile{mtfile}}
\else
\newenvironment{theorem}{\begin{xtheorem}}{\end{xtheorem}}
\newenvironment{lemma}{\begin{xlemma}}{\end{xlemma}}
\fi
\makeatother


\begin{document}
\ifonly\Writetofile{mtfile}{\string\tableofcontents}\else\tableofcontents\fi
\section{Foo}
\section{Foo bar}
\lipsum[1-12]
\begin{lemma}
this is some bla bla in lamma
\end{lemma}
\lipsum[1-12]
\begin{theorem}
some bla bla in lamma
\end{theorem}
\lipsum[1-12]
\begin{lemma}
some bla bla in lamma
\end{lemma}
\end{document}

更新用于titlesec

首先我们需要替换

\pretocmd\@startsection{\Writetofile{mtfile}{\string\csname\space #1\string\endcsname}}{}{} 
\pretocmd\@sect{\Writetofile{mtfile}{[#7]{#8}}}{}{}
\pretocmd\@ssect{\Writetofile{mtfile}{*{#5}}}{}{}

\pretocmd\ttl@straight@i{\Writetofile{mtfile}{\string\csname\space#1\string\endcsname\ifttl@label [#2]\else*\fi{#3}}}{}{}

如果命令\part\chapter也发生了变化,我们需要做一些修改

\ttl@part@i为文章类的一部分

\ttl@top@i针对章节和\ttl@page@i书籍类别的一部分。

我们需要更换

\AtBeginDocument{%
\let\mtrefstepcounter\refstepcounter
\let\refstepcounter\@gobble

\AtBeginDocument{%
\let\mtstepcounter\stepcounter
\let\stepcounter\@gobble
\let\Currentlabel\empty

\AtEndDocument{%
\let\refstepcounter\mtrefstepcounter

\AtEndDocument{%
\let\stepcounter\mtstepcounter

例子

\documentclass{book}
\usepackage{lipsum}
\usepackage{titlesec}
\titleformat{\section}{}{\thesection}{0em}{}[]

\usepackage{atbegshi}
\newcommand{\handlethispage}{}
\AtBeginShipout{\handlethispage}

\newtheorem{xtheorem}{Theorem}
\newtheorem{xlemma}{Lemma}

\newif\ifonly % uncomment next line
\onlytrue  
\makeatletter 
\ifonly
\usepackage{answers}
\Newassociation{lemma}{mtlemma}{mtfile}
\Newassociation{theorem}{mttheorem}{mtfile}
\renewenvironment{mttheorem}{\begin{xtheorem}}{\end{xtheorem}}
\renewenvironment{mtlemma}{\begin{xlemma}}{\end{xlemma}}
\Opensolutionfile{mtfile}
\usepackage{etoolbox}
\pretocmd\@part{\Writetofile{mtfile}{\string\part[#1]{#2}}}{}{}
\pretocmd\@spart{\Writetofile{mtfile}{\string\part*{#1}}}{}{}
\pretocmd\@chapter{\Writetofile{mtfile}{\string\chapter[#1]{#2}}}{}{}
\pretocmd\@schapter{\Writetofile{mtfile}{\string\chapter*{#1}}}{}{}
\pretocmd\ttl@straight@i{\Writetofile{mtfile}{\string\csname\space#1\string\endcsname\ifttl@label [#2]\else*\fi{#3}}}{}{}
\AtBeginDocument{%
\let\mtstepcounter\stepcounter
\let\stepcounter\@gobble
\let\Currentlabel\empty
\let\handlethispage\AtBeginShipoutDiscard}
\AtEndDocument{%
\let\stepcounter\mtstepcounter
\clearpage\pagenumbering{arabic}
\let\handlethispage\relax
\Closesolutionfile{mtfile}
\Readsolutionfile{mtfile}}
\else
\newenvironment{theorem}{\begin{xtheorem}}{\end{xtheorem}}
\newenvironment{lemma}{\begin{xlemma}}{\end{xlemma}}
\fi
\makeatother


\begin{document}
\ifonly\Writetofile{mtfile}{\string\tableofcontents}\else\tableofcontents\fi
\section{Foo}
\section{Bar}
\lipsum[1-12]
\begin{lemma}
this is some bla bla in lamma
\end{lemma}
\lipsum[1-12]
\begin{theorem}
some bla bla in lamma
\end{theorem}
\lipsum[1-12]
\begin{lemma}
some bla bla in lamma
\end{lemma}
\end{document}

相关内容