13

I am using the verbatim environment with

\begin{verbatim}
XXXX
\end{verbatim}

I would like the option of redefining the verbatim environment so that nothing between \begin{verbatim} and \end{verbatim} is printed. So I can print a long version of the paper with the verbatim information, and a short version with verbatim information omitted. I would be grateful if someone could tell me how to do this.

1

4 Answers 4

12

The comment package allows you to (re)define comment environments. So, include the following in your document preamble:

\usepackage{comment}% http://ctan.org/pkg/comment
\excludecomment{verbatim}

which should exclude verbatim environment content from your document. If you want to include all the verbatim content again (for the longer version of your article, comment out the \excludecomment{verbatim} line.

For a more piece-meal approach to removing verbatim content, the following is possible:

enter image description here

\documentclass{article}
\usepackage{comment}% http://ctan.org/pkg/comment
\begin{document}

\begingroup
\excludecomment{verbatim}
A
\begin{verbatim}
&^_%\{}
\end{verbatim}
B
\endgroup

A
\begin{verbatim}
&^_%\{}
\end{verbatim}
B

\end{document}

Another mild alternative to comment would be to gobble the environment contents using environ. The following is not heavily tested, but works:

enter image description here

\documentclass{article}
\usepackage{environ}% http://ctan.org/pkg/environ
\makeatletter
\newcommand{\voidenvironment}[1]{%
  \expandafter\providecommand\csname env@#1@save@env\endcsname{}%
  \expandafter\providecommand\csname env@#1@process\endcsname{}%
  \@ifundefined{#1}{}{\RenewEnviron{#1}{}}%
}
\makeatother
\begin{document}

\begingroup
\voidenvironment{verbatim}%
A
\begin{verbatim}
&^_%\{}
\end{verbatim}
B
\endgroup

A
\begin{verbatim}
&^_%\{}
\end{verbatim}
B

\end{document}

Note the difference in output. After verbatim is gobbled, there's technically only a line-break (space) between A and B.

3
  • Excellent @Werner! This seems to work nicely and I can use it to get rid of proofs as well.
    – WAJ
    Commented Apr 2, 2013 at 6:44
  • The first part of the answer seem not to work with beamer document class even the fragile frame option is presented. Especially when working with proof environment \excludecomment{proof}
    – Say OL
    Commented Sep 9, 2022 at 13:48
  • @SayOL: That's because beamer processes the frames differently than one would think. You'll have to redefine proof using the environ-like approach for it to work properly in beamer.
    – Werner
    Commented Sep 15, 2022 at 17:52
4

For the shorter version of your paper, you could load the comment package and replace all instances of {verbatim} with {comment}. That way, you won't have to deal with issues that arise from characters such as & and # occurring in the verbatim/comment areas.

3
  • 1
    Haha.You beat me by 16 seconds! Commented Apr 2, 2013 at 2:22
  • Thank you. That seems to work fine. Would it be possible for me to redefine the verbatim environment to be the comment environment? Then I would only to have to do that once at the beginning of the file instead of replacing every instance.
    – WAJ
    Commented Apr 2, 2013 at 2:49
  • 1
    @WAJ I think it's not possible due to the "voodoo" that verbatim does; but perhaps someone will prove me wrong. Commented Apr 2, 2013 at 3:44
3

I come long after the battle but here is a way which just imitates the way verbatim is done in source2e.

\documentclass{article}
\usepackage{amsthm}
\newtheorem{theo}{Theorem}

\makeatletter
\let\original@proof\proof

% Plagiarizing verbatim set-up:
\begingroup 
    \catcode `|=0 \catcode `[= 1 \catcode`]=2 
    \catcode `\{=12 \catcode `\}=12 \catcode`\\=12 
    |long|gdef|suppressed@proof #1\end{proof}[|end[proof]]
|endgroup
\def\makeproofsinvisible
  {\def\proof
      {\catcode`\{=12 \catcode `\}=12 \catcode`\\=12 \suppressed@proof }}
\def\makeproofsvisible{\let\proof\original@proof }
\makeatother

\begin{document}

\begin{theo}[Unkn.]
  Dog exists.
\end{theo}

\begin{proof}
  First step: actually, we don't really exist.
\end{proof}

\makeproofsinvisible

\begin{proof}
  Second step is censored.
\end{proof}

\makeproofsvisible

\begin{proof}
  Third Step: hence Dog exists.
\end{proof}


\end{document}

invisible proofs

1
  • The reason this answer deals with the proof environment is that I was working on this other question when it got closed as duplicate.
    – user4686
    Commented May 15, 2013 at 17:44
1

The answer is from mine previous similar question Comment out lines without using % and comment enviroment which was answered by @egreg.

Seems to working properly with \begin{equation}.... But unfortunately it does not work well with \begin{verbatim}...

However, it is a good idea if finally corrected the bug. It will generally useful in the future

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

\newif\ifprolog
\long\def\startprolog#1\stopprolog{%
  \ifprolog
  \par
  \begingroup
  \let\\\par
   #1 
  \par\medskip
  \endgroup
  \fi}

\prologtrue % comment out for omitted the inside \startprolog \stopprolog content
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

\begin{document}

\lipsum[1]

\startprolog
\begin{equation}
 k = \frac{1}{2} m u^2
\end{equation}
\stopprolog

\lipsum[1]

\startprolog
\begin{verbatim}
> eqn:=diff(y(t),t,t)+4*y(t)=0:
> inits:=y(0)=1,D(y)(0)=-1:
> dsolve({eqn,inits},y(t));
\end{verbatim}
\stopprolog

\lipsum[2]

\end{document}

You must log in to answer this question.

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