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'm trying to center a the content of a figure that has verbatim text in it, but the verbatim text is always right-aligned:

\begin{figure}
  \centering
  \begin{verbatim}
  some
  text
  here
  \end{verbatim}
  \caption{I wish this were centered}
\end{figure}
share|improve this question
4  
Welcome to TeX.sx! A verbatim environment is always typeset on full length. You may want to look at the fancyvrb package and its BVerbatim environment. –  egreg Feb 14 '13 at 9:49
    
Thanks @egreg! I will check that out. –  cam Feb 14 '13 at 12:45
    
@cam Don't forget to select the answer if it worked for you. Points for having their answer selected helps motivate people to continue to answer questions. If it didn't work, perhaps you can provide additional details, or submit your own answer if you came up with another solution. –  sh1ftst0rm May 16 '13 at 15:02
    
@egreg - I tried BVerbatim, but it seems that boxwidth=auto doe not work correctly but always yields the full page width. I have to specify an explicit dimension in order for the centering to work. Any clues on this? I would prefer BVerbatim if this worked. –  Emit Taste Nov 5 '13 at 20:13

1 Answer 1

up vote 8 down vote accepted

The verbatimbox package also does this. Here's an example done that way (Fig. 1).

UPDATE: In Fig. 2, it is shown how you can import multiple verbatim snippits into a single figure (or table) using the myverbbox environment, wherein you give unique names to the snippits. Also, the optional argument can be used to condition the verbatim text, in this case, to \small.

\documentclass{article}
\usepackage{verbatimbox}
\begin{document}

\begin{verbbox}
  some \verbatim
  text here
\end{verbbox}
\begin{figure}[ht]
  \centering
  \theverbbox
  \caption{I wish this were centered}
\end{figure}

\begin{myverbbox}[\small]{\valph}\alpha\end{myverbbox}
\begin{myverbbox}[\small]{\vbet}\beta\end{myverbbox}
\begin{figure}[ht]
\centering
\begin{tabular}{ll}
\valph & $\alpha$\\
\vbet  & $\beta$
\end{tabular}
\caption{Some greek macros for math mode}
\end{figure}
\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.