2

I am currently using the code

 \newsavebox{\FVerbB}
\newenvironment{code}
 {\VerbatimEnvironment
 \hspace{-0.25cm}
  \begin{lrbox}{\FVerbB}
  \hspace{-0.29cm}
  \begin{BVerbatim}}
 {\end{BVerbatim}
 \hspace{-0.29cm}
  \end{lrbox}
  \fcolorbox{blue!0}{gray!30}{\usebox{\FVerbB}}
  }

I have decreased the horizontal size of the box by adding hspace{-0.29cm}. I would like to do something similar for the vertical size of the box...i.e. I would like to include a vspace{-0.1cm} somewhere in this code, but I can't figure out where to put it. Any suggestions? Thanks so much in advance!

Update: the suggestions for the vertical resizing have worked brilliantly. But I have run into an issue with the box extending too far out in front of the enclosed text. Below is an example.

Current Output

I don't like that there is no space before the word "is". Any thoughts on how to fix this? Thanks so much!

2
  • If I try, I get that the background is too narrow. Can you explain more precisely your goal?
    – egreg
    Commented Jul 28, 2014 at 14:45
  • If you just want to vertically shift the box (as opposed to resizing it), the following substitution will achieve that: \fcolorbox{blue!0}{gray!30}{\raisebox{0.1cm}{\usebox{\FVerbB}}}. If you want to actually resize a box, the \addvbuffer macro of the verbatimbox package can do that (under certain constraints), but it is not clear, as egreg notes, what exactly you are trying to accomplish. Commented Jul 28, 2014 at 15:07

1 Answer 1

2

You have some trailing spaces in your code. However, I suppose you are looking for something like this:

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{xcolor}
\newenvironment{code}
 {\VerbatimEnvironment
  \begin{SaveVerbatim}{VerbEnv}}
 {\end{SaveVerbatim}%
   \fboxsep=1pt   
   \fcolorbox{black}{gray!30}{\BUseVerbatim{VerbEnv}}}
\begin{document}
\begin{code}
fog
\end{code}

\end{document}

enter image description here

Use \colorbox instead of \fcolorbox if you do not want a frame.

2
  • Thank you so much for this reply @Herbert! Your code worked wonderfully. I do have two questions though: (1) when I use the text box, any spacing that comes immediately after it becomes erased, i.e. <foo in box> some text is just <foo in box>some text. (2) Latex gives me an error if I write \begin{code} foo \end{code}. I have to put \begin{code} and \end{code} on separate lines. Do you know why this is? Thanks so much for the help though - I'm still new to Latex and Stackexchange, so please forgive me if I have made some obvious mistakes.
    – user56010
    Commented Jul 28, 2014 at 17:01
  • use \colorbox{gray!30}{\BUseVerbatim{VerbEnv}}\space}. That depends on how verbatim is read. LaTeX looks for a line with end{...}. If you want inline code then use \verb or \lstinline from package listings
    – user2478
    Commented Jul 28, 2014 at 17:29

You must log in to answer this question.

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