2

Suppose that I have a long tex file with a lot of things. In order to read the logic structure only, I want to print only text inside \begin{theorem}..\end{theorem} and \begin{lemma}..\end{lemma}.

Sure, I can embrace all the other parts incide something like \show{...} and then define \show[1] as %1 when I want to see all content and as {} if I want to see only statements.

But I am looking for a more elegant solution. =) Something like the preview package http://www.pirbot.com/mirrors/ctan/macros/latex/contrib/preview/preview.pdf

1 Answer 1

5

Here is a solution based on atbegshi and answers packages.

The idea is to use answers that write contents of theorem, lemma to an external file (mtfile.tex), discard all pages (all contents of the main file https://tex.stackexchange.com/a/267555/71471) and then use the external file to output only theorems.

We use a newif test \onlytrue for only theorem and \onlyfalse for normal use.

Auxiliary environments used to save the original definitions of theorem and lemma.

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

Complete code.

\documentclass{article}
\usepackage{lipsum}

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

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

\newif\ifonly % uncomment next line
%\onlytrue   
\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}
\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-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}
6
  • That's wickedly clever. The only solutions I could come up with, are much more complicated and likely less reliable. Feb 7, 2016 at 20:06
  • @NikitaKalinin you're welcom
    – touhami
    Feb 7, 2016 at 21:22
  • That's a really nice solution ! Do you think of an easy solution to keep the sectionning commands of the original document (i.e. sections, chapters, etc) ? It seems that \Newassociation only works with environments, and I don't see how to do.
    – mvienney
    Feb 22, 2017 at 17:50
  • @mvienney i think yes, i'll try :-)
    – touhami
    Feb 22, 2017 at 18:24
  • @mvienney please ask new question.
    – touhami
    Feb 23, 2017 at 13:39

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.