2

Why is defining a new name for environment for a command produces different result than when using the environment directly without defining a name for it?

Here are 2 examples, one uses a name for the env. and one does it. The second one is missing the first line in the verbatim text. Same text is used in both examples.

enter image description here

Code is below:

\documentclass{article}
\usepackage{fancybox}

\newenvironment{myStuff}%
{\VerbatimEnvironment\begin{Sbox}\begin{BVerbatim}}%
{
\end{BVerbatim}\end{Sbox}\fbox{\TheSbox}
}    
\begin{document}        
\begin{myStuff}
line 1
line 2
line 3
\end{myStuff}
\end{document}

second example

\documentclass{article}
\usepackage{fancybox}

\begin{document}    

\begin{Sbox}\begin{BVerbatim}%
line 1
line 2
line 3
\end{BVerbatim}
\end{Sbox}\fbox{\TheSbox}
\end{document}

Texlive 2014

3 Answers 3

5

Your problem is that % after \begin{BVerbatim} in the second example. In the first example, the line \begin{myStuff} has a normal line ending (no %, so an end-of-line is seen), whereas as you've written it in the second example the % means that \begin{BVerbatim} 'runs on' into the line 1 line. I've not traced it through but I strongly suspect that \begin{BVerbatim} ignores the rest of the content of the 'current' line at the start of the environment. As a result, the % means that line 1 is ignored in the second case. Simply removing it solves the problem

\documentclass{article}
\usepackage{fancybox}

\begin{document}    

\begin{Sbox}\begin{BVerbatim}
line 1
line 2
line 3
\end{BVerbatim}
\end{Sbox}\fbox{\TheSbox}
\end{document}
3

Here is a solution with package fancyvrb:

\documentclass{article}
\usepackage{fancyvrb}    
\newenvironment{myStuff}
  {\VerbatimEnvironment
   \begin{SaveVerbatim}{VerbEnv}}
  {\end{SaveVerbatim}\fbox{\BUseVerbatim{VerbEnv}}}

\begin{document}        
\begin{myStuff}
line 1
line 2
line 3
\end{myStuff}
\end{document}
5
  • Thanks, but actually all the options being passed via [frame=single] and others like it, has no effect. Actually without [frame=single] it still works as is. Another thing I tried is \begin{SaveVerbatim}[frame=single,framerule=2pt]... and the line thickness did not change. I think all these options do not work for some reason like they do with normal \begin{Verbatim}[frame=single,framerule=2pt,samepage...] and I can't figure out why. I wish they did.
    – Nasser
    Aug 27, 2014 at 7:13
  • fyi, I have found another issue with this, which I am not able to resolve. May be you can. What happens is this: If the verbatim text is too long, the fbox does not overflow to the next page, but gets chopped off at the end of the page, and any remaining verbatim text is lost. i.e. if I just use \begin{Verbatim}{frame=single}... this problem do not happen, and the frame continue to next page, for as many pages as needed for large verbatim text. But not with the above. I'll post screen show below to show the difference.
    – Nasser
    Aug 27, 2014 at 10:34
  • screen shot !Mathematica graphics
    – Nasser
    Aug 27, 2014 at 10:39
  • for such listings you should use package listings.
    – user2478
    Aug 27, 2014 at 10:42
  • Ok, will look at listing. But this all works fine with \begin{Verbatim}[frame=single]. I do not want to include text from files. The verbatim text is already inside the latex document. Only reason I was looking at fancybox here, is to make the box fit tight around the text, which \begin{Verbatim}[frame=single] does not do and has no option to do that. I assumed listing works only with reading text from files, but will check on it now. As long as I can get a frame that fits tight around the text, and does not get chopped at the end of the page, then I can use listing instead. Thanks.
    – Nasser
    Aug 27, 2014 at 10:53
1

Here is a solution with the package verbatimbox.

\documentclass{article}
\usepackage{verbatimbox}
\begin{document}
\begin{verbbox}
line1
line2
line3
\end{verbbox}
\fbox{\theverbbox}
\end{document}
2
  • thanks. But verbatimbox has the same issue I wrote about in my comment to Herbert above. For verbatim that is too long, the box gets chopped off and I lose verbatim text if the is longer than one page. This problem do not happen with \begin{Verbatim}[frame=single]
    – Nasser
    Aug 27, 2014 at 10:49
  • @Nasser I see. Thanks for the clarification. Aug 27, 2014 at 11:28

You must log in to answer this question.

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