Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a frame/boarder round my Verbatim environment however the frame is extended to the page side, thus alot of wasted space. The following is my code:

    \begin{Verbatim}[frame=single]
   s=0 
   for j = 1 : i -1 
      s = s + aij * akj
   end 
   aki = (aki - s) / aii 

    \end{Verbatim}
share|improve this question
    
I would like the frame to fit around the code. –  Joshua Feb 28 at 9:19
1  
Rather than clarifying in comments, please edit your question to include the information. A complete, compilable, minimal example would also be nice. –  Torbjørn T. Feb 28 at 9:27
    
And minimal as in something we can copy and cimpile ourself without having to add anything. Where does Verbatim come from, which class is used .... –  daleif Feb 28 at 9:37

3 Answers 3

up vote 4 down vote accepted

The Verbatim environment from the fancyvrb has not an options to do that.

But you can use the similar environment boxedverbatim from the moreverb package. Remember anyway to eliminate the spaces at the beginning of lines.

MWE

\documentclass{article}
\usepackage{moreverb}

\begin{document}

\begin{boxedverbatim}
s=0
for j = 1 : i -1
  s = s + aij * akj
end
aki = (aki - s) / aii
\end{boxedverbatim}

\end{document}  

Output

enter image description here

share|improve this answer

If you wish to add spice to your verbatim later, here is an option with tcolorbox. This box can break across pages.

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{listings,breakable}

\newtcblisting{myverbatim}{
      arc=3mm,
      top=0mm,
      bottom=0mm,
      left=0mm,
      right=0mm,
      boxrule=0.5pt,
      colback=gray!30,
      listing only,
      hbox,
      breakable
}


\begin{document}

\begin{myverbatim}
s=0
for j = 1 : i -1
  s = s + aij * akj
end
aki = (aki - s) / aii
\end{myverbatim}

\end{document}

enter image description here

share|improve this answer

The verbatimbox package can also do this. The nice thing here is that the boxed content is not immidately printed, so that it can then be inserted anywhere a LaTeX box can go. Here it is inside an \fbox, but it can be placed inside a table, a footnote, a section title, etc.

There is also a different environment myverbbox in which you provide the name of the recalling macro (different than \theverbbox).

\documentclass{article}
\usepackage{verbatimbox}

\begin{document}

\begin{verbbox}
s=0
for j = 1 : i -1
  s = s + aij * akj
end
aki = (aki - s) / aii
\end{verbbox}
\fbox{\theverbbox}

\end{document}  

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.