11

I have a document of the form

\begin{document}
some text
\begin{myenv}
....
\end{myenv}
some other stuff
\end\document

I would like to set a boolean \newif\if@test in the header to control the output, such that if I set \@testtrue, the document would end just after \end{myenv} and not display some other stuff. My attempt is to put

\if@test \enddocument

at the end of the declaration of myenv. But this does not work, LaTeX complains that begin{myenv} is ended by \enddocument ...

How could I do that?

Werner
  • 603,163
Loic Rosnay
  • 8,167

3 Answers3

11

A simple solution is to use \stop to stop further compilation.

\documentclass{article}
\newenvironment{myenv}{}{\stop}
\begin{document}
some text
\begin{myenv}
Some other text
\end{myenv}
some other stuff
\end{document}

This is a TeX command redefined in LaTeX ltmiscen.dtx.

yannisl
  • 117,160
3

To comment some phrases of your document I recommend the package comment.

On the other hand I recommend the package etoolbox with the hook \AfterEndEnvironment.

Here an example:

\documentclass{article}%
\usepackage{etoolbox}
\newbool{myenv:enddocument}
\booltrue{myenv:enddocument}
\AfterEndEnvironment{myenv}{%
\ifbool{myenv:enddocument}%
  {\def\tempa{\end{document}}}%
  {\let\tempa\relax}%
  \tempa%
}
\newenvironment{myenv}%
{\par\noindent}%
{\par}
\begin{document}

some text
\begin{myenv}
....
\end{myenv}
some other stuff
\end{document}
Marco Daniel
  • 95,681
  • to comment part of my text, i usually use either a command \comm defined by {} or an environment defined with \NewEnviron{comm}{}. Thanks for the interesting \AfterEndEnvironment – Loic Rosnay Jan 18 '12 at 19:52
  • 1
    \ifbool{myenv:enddocument}{\end{document}}{} – Ahmed Musa Jan 18 '12 at 21:13
2

The following works due to the minimal grouping within your environment:

enter image description here

\documentclass{article}
\newenvironment{myenv}{Start}{Finish}% \begin{myenv}...\end{myenv}
\makeatletter
\newif\if@test% New condition \@test<true><false>
\@testtrue% \@testfalse
\g@addto@macro{\myenv}
  {\if@test\g@addto@macro{\endmyenv}{\aftergroup\enddocument}\fi}
\makeatother
\begin{document}
some text
\begin{myenv}
   ...
\end{myenv}
some other stuff
\end{document}​

\g@addto@macro works similar to etoolbox's \appto command. \aftergroup waits for the current group to finish before executing \enddocument.

Werner
  • 603,163