[Tex/LaTex] Adding \comment or \begin{comment} to \AtBeginDocument

commentsgroupinghooksmacros

For Partial compilation commands/environment?, I would like to add \begin{comment} or \comment at the start of the document (so I could turn it off elsewhere in the code).

So, even if in the larger scope of things it is useless – in this MWE, as an exercise, I'd like the whole text to be blanked, basically, conditionally through the command line (the result would be a blank page, or even no pdf).

EDIT: However, I'd want it done in such a way, so that I could arbitrarily add \end{comment} / \begin{comment} later on it the document, and have only that snippet compile.

\documentclass[11pt]{book}
\usepackage{lipsum}

\ifx\doskip\relax
  \typeout{DOSKIP}
  \usepackage{etoolbox}
  \usepackage{comment}
  % https://tex.stackexchange.com/questions/14135/how-to-automatically-add-text-immediately-after-begindocument
  %\AtBeginDocument{\comment}         % ! Extra \endgroup.
  %\AtBeginDocument{\begin{comment}}  % Runaway argument? ! File ended while scanning use of \next.
  %\AfterEndPreamble{\comment}        % ! Extra \endgroup.
  %\AfterEndPreamble{\begin{comment}} % Runaway argument? ! File ended while scanning use of \next.
  %\AtEndDocument{\endcomment}%{\end{comment}}
  % desperate try (also fails w/ ! Extra \endgroup.):
  \protected\def\startcomment{\expandafter\comment}
  \AtEndPreamble{
\startcomment
  } % ! Extra \endgroup.
\fi

\begin{document}

\chapter{Some chapter}
\section{Section One}
\lipsum[1-3]

% later I might want to use here:
% \end{comment}
% \lipsum[5] % this would be typeset
% \begin{comment} % from this point on, again blanked

\end{document}

The idea is that the document would be "blanked" if Latex is called on the command line via
pdflatex "\let\doskip\relax\input{test.tex}"
… however, clearly there are some grouping errors; otherwise without the intervention, just using pdflatex test.tex, works fine.

Any ideas how this could be implemented?


EDIT2: closer, but not yet there: I've seen in kpsewhich comment.sty "Other 'comment' environments are defined by … \excludecoment{versionb} ; These environments are used as \begin{versiona} … \end{versiona} with the opening and closing commands again on a line of their own. This is not a LaTeX environment: for an included comment, the \begin and \end lines act as if they don't exist. In particular, they don't imply grouping …"

So in the below example, which uses \excludecomment{versionb}, the begin{document} is succesfully overridden, the first part is skipped, then comment is interrupted, \lipsum[5] is typeset, and document compiles without error – but beyond that, it is very difficult to properly close either non-interrupted (fully blanked doc) or continue with blanking after once it's been interrupted:

\documentclass[11pt]{book}
\usepackage{lipsum}
\usepackage{trace}

\ifx\doskip\relax
  \typeout{DOSKIP}
  \usepackage{etoolbox}
  \usepackage{comment}
  \excludecomment{versionb}
  %\AtEndPreamble{ % here causes ! LaTeX Error: Missing \begin{document}.
  \AtBeginDocument{%
    % \expandafter\begin{versionb}\relax
    \begin{versionb}
  } % 
  %\AtEndDocument{\end{versionb}} % ! LaTeX Error: \begin{document} ended by \end{versionb}.
\fi

% \traceon
\begin{document}

\chapter{Some chapter}
\section{Section One}
\lipsum[1-3]

\end{versionb}
\lipsum[5]
% \begin{versionb}

%% \end{versionb} % can this be implicit, as per \AtEndDocument?
\end{document}
%%ENNDDD

A \typeout can be added, to this macro in comment.sty:

 \gdef\ProcessCommentLine#1^^M{\def\test{#1}
      \typeout{test: \meaning\test, \meaning\expandafter \csname End\CurrentComment Test\endcsname}

… so if there isn't a \end{versionb} before the \lipsum[5], we can see that the problem occurs because lines are attempted to be read beyond end of file:

...
test: macro:->\end{document}, \expandafter\end{versionb}
test: macro:->%%ENNDDD, \expandafter\end{versionb}
test: macro:->, \expandafter\end{versionb}
)
Runaway argument?
! File ended while scanning use of \next.
<inserted text> 
                \par 
<*> \let\doskip\relax\input{test.tex}

So, I'm still in need of help on how to make this code work for all circumstances…

Best Answer

You can use the following code somewhere in the preamble after \documentclass.

\def\doskipA{\begin{document}\end{document}}
\ifx\doskip\relax \expandafter\doskipA\fi
Related Question