3

I have defined my own environment - lets call it 'myenv' - which shows and hides its content based on some global settings. I'm using the \comment and \endcomment commands from the verbatim package, which work really well for me.

However, i've encountered a strange behaviour when enclosing my environment with a box like this (i reduced myenv to this minimal example):

\documentclass[10pt,a4paper]{article}
\usepackage{verbatim}

%define my environment
\newenvironment{myenv}{%
    Start:
    \comment
}{%
    \endcomment
    :End
}

\begin{document}
    %\parbox{5cm}{                 % <- this is where it gets strange
         \begin{myenv}
             Content
         \end{myenv}    
    %}                             % <- this is where it gets strange
\end{document}

When i enable the parbox-lines, i get a list of errors which i don't really understand:

Argument of \verbatim@ has an extra }. }
Paragraph ended before \verbatim@ was complete. }
Extra }, or forgotten \endgroup. }
You can't use `\end' in internal vertical mode. \end{document}
Missing } inserted. \end{document}
Emergency stop.

Something seems to break with the begin and end-points...


This also happens with other boxes:

For the \mbox the error-list is a bit different:

Argument of \verbatim@ has an extra }. }
Paragraph ended before \verbatim@ was complete. }
Extra }, or forgotten \endgroup. }
Emergency stop.

The \fbox has another error-signature yet again:

Argument of \verbatim@ has an extra }. }
Paragraph ended before \verbatim@ was complete. }
Extra }, or forgotten \endgroup. }
Extra }, or forgotten \endgroup. }
Emergency stop.

The TeX-Primitives \hboxand \vbox work fine, as does the minipage-environment.

So my question is: What is going wrong here and how to resolve this?

3
  • 5
    you can not have verbatim (or similar) commands in the argument of another command. Commented Jul 25, 2018 at 13:39
  • You can use other forms of "commenting", for example, placing the content inside a (discarded) box. However, the problem with such an approach is that any counters and lengths (or globally-set variables) that are modified in the comment will carry through to the rest of the document. verbatim approaches avoid that, but as David points out, have their own limitations. Commented Jul 25, 2018 at 13:44
  • Thanks! So minipage works because it is an environment and \parbox doesn't because it gets the content as a regular argument. What about \hbox and \vbox however?
    – Blaubär
    Commented Jul 25, 2018 at 13:46

1 Answer 1

4

Verbatim like objects are not allowed in arguments to other commands. Since comment is implemented the same as verbatim, it shares the limitation.

Thus you can't have comment in \parbox, \mbox, \fbox and so on.

To the contrary, it can be in minipage, because this environment doesn't collect its contents as an argument.

The same for the primitives \hbox and \vbox. The fact that the syntax is similar to commands with argument is not relevant; indeed,

\hbox\bgroup ... \egroup
\vbox\bgroup ... \egroup

are allowed (which aren't for ordinary macros); the { (explicit or implicit) starts the business of collecting tokens to make up a (horizontal or vertical) list and the final } (explicit or implicit) builds the box from the list formed so far.

Basically, \parbox{<dimen>}{<tokens>} is

\vbox{\hsize=<dimen><tokens>}

but the <tokens> are absorbed as an argument. Instead,

\begin{minipage}{<dimen>}

does \vbox\bgroup\hsize=<dimen>, while

\end{minipage}

provides the trailing \egroup.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .