# Import several parts of file at once

As a follow up to this Question

Situation

I would like to create an excerpt from a script that I have TeX'ed as a compendium of the most important statements (theorems & definitions).

I seek to import parts of a file in a way to keep the original numbering (as in the above mentioned question), which is why the MWE imports all, but does not print certain parts.

In order to make the import easier, I would prefer just one (or two) type(s) of tag(s): %<*include> ... %</include> (& %<*exclude> ... %</exclude>). In the file for the shortened version I need a way to go through all tags and display some (while hiding others).

This would mean a lot less work! Is there a way to achieve this by importing with a loop, macro or maybe some other package ?

QUESTION

# Is it possible to have a loop or some macro import all tagged parts of a file while excluding others?

Note: A similar unanswered question does not take into account the numbering and thus different import

MWE by touhami (slightly altered)

first file (with tags)

\documentclass{amsart}
\usepackage{lipsum}

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}
\theoremstyle{definition}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{example}[theorem]{Example}
\begin{document}
%<*tag>
\lipsum[1]
Here we go
%</tag>
%<*atag>
\begin{definition}
some bla bla
\end{definition}
%</atag>
\begin{theorem}
more bla bla
\end{theorem}
\begin{definition}
some bla bla
\end{definition}
%<*btag>
\begin{definition}
some bla bla
\end{definition}
\begin{theorem}
more bla bla
\end{theorem}
%</btag>
%<*ctag>
\lipsum[1-2]
%</ctag>
%<*dtag>
\begin{lemma}
more bla bla
\end{lemma}
\begin{theorem}
more bla bla
\end{theorem}
%</dtag>
%<*etag>
\lipsum[1-2]
\begin{example}
some bla bla
\end{example}
%</etag>
\begin{theorem}
more bla bla
\end{theorem}
\end{document}


final file

\documentclass{amsart}
\usepackage{lipsum}

\usepackage{catchfilebetweentags}
\newtoks\temptoken

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}
\theoremstyle{definition}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{example}[theorem]{Example}
\begin{document}
\lipsum[1]
\CatchFileBetweenTags\temptoken{equ}{tag}% capture part1
\setbox0=\vbox{\the\temptoken}%  skip part1
\CatchFileBetweenTags\temptoken{equ}{atag}% capture part2
\the\temptoken % display part2

\CatchFileBetweenTags\temptoken{equ}{btag}% capture part3
\setbox0=\vbox{\the\temptoken}%  skip part3
\CatchFileBetweenTags\temptoken{equ}{ctag}% capture part4
\setbox0=\vbox{\the\temptoken}%  skip part4

\CatchFileBetweenTags\temptoken{equ}{dtag}% capture part5
\the\temptoken%  display part5
\end{document}


(the odd numbering is intended!)

Here is a solution.

Here we need one tage tag and we use newenvironment mtexclude: this environment does nothing for the original file and for the newfile it save its content to a box \mtbox so the content is skiped.

file.tex (original file)

\documentclass{amsart}
\usepackage{lipsum}

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}
\theoremstyle{definition}
\newtheorem{definition}{Definition}
\newtheorem{example}{Example}
\newenvironment{mtexclude}{}{}
\begin{document}
First bla bla
%<*tag>
\section{Foo}
\subsection{Foo bar}
I need this
\begin{mtexclude}% begin of part to skiped
\section{Baz}
\subsection{Baz bar}
no need for this
\end{mtexclude}%   end
\section{End}
\subsection{End bar}
and need this \dots
\begin{mtexclude}% begin of part to skiped
\section{Baz 2}
\subsection{Baz bar 2}
another part to be skiped
\end{mtexclude}%   end
The end
%</tag>
Last bla bla
\end{document}


newfile.tex

\documentclass{amsart}
\usepackage{lipsum}

\usepackage{catchfilebetweentags}
\newtoks\temptoken
\newbox\mtbox
\newenvironment{mtexclude}{\setbox\mtbox\vbox\bgroup}{\egroup}

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}
\theoremstyle{definition}
\newtheorem{definition}{Definition}
\newtheorem{example}{Example}

\begin{document}
\lipsum[1]
\CatchFileBetweenTags\temptoken{file}{tag}
\the\temptoken

\end{document}

• Tried; Works just fine! If I am right then this is doing basically the same thing as in your answer to the previous question, correct? Are \bgroup and \egroup commands to specify the range of effect for the preceding commands ? Also, I have structured the complete version in chapter files. I suppose the best way to use this is by just defining an environment per file or would you recommend something else? – gr4nt3d Mar 5 '16 at 13:52
• 1) yes it's slightly different 2) \bgroup and \egroup specify the range skipped here needed to define the environment 3) you need only one evironment per document (not per file) – touhami Mar 5 '16 at 14:36
• I did use this method on my actual work now. It seems like the exclude environment does not harmonize with figure environments. Even though including figures is not a problem, placing them in excludes is. – gr4nt3d Mar 12 '16 at 13:36
• @hillbilly my english is not so good please be more clear or better start a new question. – touhami Mar 12 '16 at 14:18
• please see the question regarding the figure environment error here: tex.stackexchange.com/questions/298651/… – gr4nt3d Mar 12 '16 at 14:59