3

I am trying to adapt touhami's solution to How to print out only statements (theorems, lemmata, etc) from a tex file? so that I can also print the section titles.

My first guess would have been to use \pretocmd{\section}{\begin{myenv}}{}{} and \apptocmd{\section}{\end{myenv}}{}{}. But accordingly to Environment begin or end in Heading definition it does not seem to be a good idea to patch \section

So I tried to adapt Christian Hupfer solution to the same question this way :

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

However, it still does not work. The mtfile.tex contains the following lines, in which the blank spaces are probably (part of ?) the problem :

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

Any idea of what is wrong ?

  • It does not work with patching the section because titlesec changes the section command. – user31729 Feb 23 '17 at 11:21
  • Oh, I forgot to mention that I included titlesec in my example because I will need it later and that I thought it could be the reason of some problems... However : if I remove titlesec, the problem is still here. – mvienney Feb 23 '17 at 11:40
  • I'll take a look later on. – user31729 Feb 23 '17 at 11:46
3

Here is a solution. For (sub)sections like commands one needs

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

for parts

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

and for chapters

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

We also need to disable counters by adding

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

and

\AtEndDocument{%
\let\refstepcounter\mtrefstepcounter

If table of contents (toc) is used one need to type

\Writetofile{mtfile}{\string\tableofcontents}

Example

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

Update for use with titlesec

First we need to replace

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

with

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

if commands \part and \chapter are changed too we need to do some changes with

\ttl@part@i for part in article class

\ttl@top@i for chapter and \ttl@page@i for part in book class.

and we need to replace

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

with

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

and

\AtEndDocument{%
\let\refstepcounter\mtrefstepcounter

with

\AtEndDocument{%
\let\stepcounter\mtstepcounter

Example

\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}
  • Thank you ! You forgot \usepackage{answers} in your example. – mvienney Feb 23 '17 at 16:36
  • However, this solution does not work if using titlesec. – mvienney Feb 23 '17 at 16:38
  • i'll try to see what happen with titlesec. – touhami Feb 23 '17 at 16:53
  • I understand that many things are changing with titlesec, but I don't understand what. If anyone has a reference about this (the documentation of titlesec does not seem to help), it would be very helpful. – mvienney Feb 23 '17 at 18:32
  • Suggestions on what to do if there is an \appendix command in the file? The section numbering is then A, B, C, instead of 1, 2, 3... – Joel Feb 5 '18 at 3:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.