Verbatim text with colored background

3,254 views
Skip to first unread message

Ashish Revar

unread,
Jan 29, 2011, 12:39:58 AM1/29/11
to latexus...@googlegroups.com
Hello there,
I am trying to make verbatim text may differ from the other text.
For that, I have defined some commands like below,

\usepackage{color}
\definecolor{gray}{gray}{0.5}

\fcolorbox{gray}{gray}%
  {\color{white} 

\begin{verbatim}
euca-bundle-image -i <kernel file> --kernel true
euca-upload-bundle -b <kernel bucket> -m /tmp/<kernel file>.manifest.xml
euca-register <kernel-bucket>/<kernel file>.manifest.xml
\end{verbatim}
 }

But this shows an error, I want something like this, black text, gray BG and black border.
Latex.jpg
-- 
Any package or code for this?

Thanks and Regards,
Ashish Revar,
Nirma University,
ashis...@gmail.com
9909298846


Latex.jpg

Ashish Revar

unread,
Jan 29, 2011, 2:08:55 AM1/29/11
to latexus...@googlegroups.com
Hello there,

I have found out one probable solution for me but I want more. Here is the code which I am using right now.

\definecolor{gray}{gray}{0.5}

\makeatletter
\def\inverseverbatim{%
  \color{white}\scriptsize
  \def\verbatim@processline{%
    {\setbox0=\hbox{\the\verbatim@line}%
    \hsize=\wd0 \the\verbatim@line\par}}%
  \@minipagetrue
  \@tempswatrue
  \@totalleftmargin\z@
  \setbox0=\vbox\bgroup \verbatim
}
\def\endinverseverbatim{%
  \endverbatim
  \unskip\setbox0=\lastbox
  \egroup
  \colorbox{gray}{\box0}%
} \makeatother

\begin{inverseverbatim}
      auto eth0
\end{inverseverbatim}

So, here what about the border color and all?
Is it proper as per the rules in LaTeX?
--
Latex.jpg

Ashish Revar

unread,
Jan 29, 2011, 2:20:32 AM1/29/11
to latexus...@googlegroups.com
Hello there,

Sorry for posting again without waiting for any answer from others.
I have found out another way to do this,

\usepackage{verbatim,framed}
\usepackage[usenames,dvipsnames]{color}

\newenvironment{colorverbatim}[1][Gray]%
{%
\definecolor{shadecolor}{named}{#1}%
% supress ugly vertical space which is inserted by
% environment above and below text:
\topsep=0ex\relax
% start shaded and verbatim:
\shaded \verbatim \color{White}
}%
{%
\endverbatim
\endshaded
}%

\begin{colorverbatim}
   auto eth0
\end{colorverbatim}

This environment gives me the output I want, but still black border is needed.
How to do this?
Latex.jpg

Gildas Cotomale

unread,
Jan 29, 2011, 4:36:53 AM1/29/11
to latexus...@googlegroups.com
> I am trying to make verbatim text may differ from the other text.
> For that, I have defined some commands like below,
> \usepackage{color}
> \definecolor{gray}{gray}{0.5}

Sound ok exept that i won't use a name that is also a keyword and that
is meanless (as you are using grayscale, gray by it's own is a
nonsense; \definecolor{gray50}{gray}{0.5} or
\definecolor{halfgray}{gray}{0.5} sound better imho) My suggestion :
\definecolor{BoxBackground}{gray}{0.1} % 10%
\definecolor{BoxForeground}{gray}{0.9} % 90%
\definecolor{BoxBorderframe}{gray}{0.8} % 80%
\fcolorbox{BoxBorderframe}{BoxBackground}{
\color{BoxForeground} % the default text color (black) is not changed,
we had to specify the desired one if we want something else (then no
need with the default black excep if the hole is chanfed previousely)
... now comes your text :)
}

> \fcolorbox{gray}{gray}%
>   {\color{white}
> \begin{verbatim}
> euca-bundle-image -i <kernel file> --kernel true
> euca-upload-bundle -b <kernel bucket> -m /tmp/<kernel file>.manifest.xml
> euca-register <kernel-bucket>/<kernel file>.manifest.xml
> \end{verbatim}
>  }
>
> But this shows an error, I want something like this, black text, gray BG and black border.

I didn't test it but maybe color package don't know the color name
"white" ? I used to use those names too, but I always instruct the
package that I want to use some basic/common friendly names so:
\usepackage[usenames,dvipsnames]{color} %
http://en.wikibooks.org/wiki/LaTeX/Colors#The_68_standard_colors_known_to_dvips
Please consider reading the package documentation (command line: texdoc color)


You may consider using the package Fancy Verbatim too (also read the
documentation)
\usepackage{fancyvrb}
\usepackage{color}
% don't forget to define your needed colors..
...
\colorbox{BoxBackgroud}{ % this trick is required because there's no
option provided for that, but you may redefine some internals used by
Verbatim if you wisth ;)
\begin{Verbatim}[frame=single, fillcolor=BoxBackground,
formatcom=\color{BoxForeground}]
% the frame use the same color as the text, otherwise add the option
rulecolor=\color{BoxBorderframe} in the list too.
\end{Verbatim}
% beware: "Verbatim" (capital v) instead of "verbatim"
}


Another solution.. listing package
\usepackage{fancyvrb}
\usepackage{color}
% don't forget to define your needed colors..
...
\lstset{language=bash, backgroundcolor=\color{BoxBackgroud},
rulecolor=\color{BoxBorderframe} }
\textcolor{BoxForeground}{ % use this trick if you don't want to use
defautlt text color and don't have time to tweek each class
ofwords/tokens used by the language ;)
\begin{lstlisting}[frame=single]
% put your listing here
\end{lstlisting}
}


Best regards

--
http://www.math.harvard.edu/computing/latex/color.html
http://mirror.ctan.org/macros/latex/contrib/xcolor/xcolor.pdf
http://www.ctan.org/tex-archive/macros/latex/contrib/fancyvrb/fancyvrb.pdf
http://kom.aau.dk/group/02gr506/diverse/guide%20to%20fancyvrb.pdf
ftp://ftp.tex.ac.uk/tex-archive/macros/latex/contrib/listings/listings.pdf

Peter Flynn

unread,
Jan 29, 2011, 6:56:12 AM1/29/11
to latexus...@googlegroups.com
On Sat, Jan 29, 2011 at 7:20 AM, Ashish Revar <ashis...@gmail.com> wrote:
Hello there,

Sorry for posting again without waiting for any answer from others.
I have found out another way to do this,

\usepackage{verbatim,framed}
\usepackage[usenames,dvipsnames]{color}

\newenvironment{colorverbatim}[1][Gray]%
{%
\definecolor{shadecolor}{named}{#1}%
% supress ugly vertical space which is inserted by
% environment above and below text:
\topsep=0ex\relax
% start shaded and verbatim:
\shaded \verbatim \color{White}
}%
{%
\endverbatim
\endshaded
}%

\begin{colorverbatim}
   auto eth0
\end{colorverbatim}

This environment gives me the output I want, but still black border is needed.
How to do this?

Use fancyvrb and fancybox. It means an extra pair of lines but gives you much better control.
Also, use xcolor, not color

\documentclass{article}
\usepackage{fancyvrb,fancybox}
\usepackage[svgnames]{xcolor}
\newenvironment{colframe}{%
  \begin{Sbox}
    \begin{minipage}{.8\columnwidth}
}{%
  \end{minipage}
  \end{Sbox}
  \begin{center}
    \fcolorbox{black}{LightSteelBlue}{\TheSbox}
  \end{center}
}
\begin{document}
\begin{colframe}
\begin{Verbatim}{frame=single}
   auto eth0
\end{Verbatim}
\end{colframe}
\end{document}

The problem with the verbatim environment is that the line \end{verbatim} must occur exactly as-is in the document, not in a macro. The verbatim package tries to overcome this restriction, but adds some of its own. The Verbatim (capital V) environment from fancyvrb still has that restriction, but has options for a frame and a lot of other bells and whistles. The fancybox package provides the Sbox environment which lets you capture material into a box which you can then frame and colour yourself as the \TheSbox.

///Peter

Ashish Revar

unread,
Jan 29, 2011, 12:35:57 PM1/29/11
to latexus...@googlegroups.com
Thank you all,
But I am having report class not article.

As well as I have tried this fancyverb package but this shows error in itemize...somehow, which can't figure out, why?
It prompts the error with the line that already defined or somewhat which means \item is being conflicted by using this fancyverb package in report class.

Any guess to use this in report class?

--
You received this message because you are subscribed to the Google Groups "LaTeX Users Group" group.
To post to this group, send email to latexus...@googlegroups.com.
To unsubscribe from this group, send email to latexusersgro...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/latexusersgroup?hl=en.

Peter Flynn

unread,
Jan 29, 2011, 5:28:48 PM1/29/11
to latexus...@googlegroups.com
On Sat, Jan 29, 2011 at 5:35 PM, Ashish Revar <ashis...@gmail.com> wrote:
Thank you all,
But I am having report class not article.

I don't understand the problem. What I wrote works in any class. Just copy and paste it into a report.
 
As well as I have tried this fancyverb package but this shows error in itemize...somehow, which can't figure out, why?

Because I make the box too wide (.85\columnwidth). I changed it (see below) so that it automatically adapts to fit the available space (I hope). I added the calc package so it could work this out.
 
It prompts the error with the line that already defined or somewhat which means \item is being conflicted by using this fancyverb package in report class.

Nothing to do with it.
 
Any guess to use this in report class?

The error has nothing to do with the report class.

\documentclass{report}
\usepackage{fancyvrb,fancybox,calc}

\usepackage[svgnames]{xcolor}
\newenvironment{colframe}{%
  \begin{Sbox}
    \begin{minipage}
      {\columnwidth-\leftmargin-\rightmargin-2\fboxsep-2\fboxrule-4pt}

}{%
  \end{minipage}
  \end{Sbox}
  \begin{center}
    \fcolorbox{black}{LightSteelBlue}{\TheSbox}
  \end{center}
}
\begin{document}
This is an example of using it in a report
\begin{itemize}
  \item To make networking start on boot, type
    \begin{colframe}
      \begin{Verbatim}{frame=single}
   $ auto eth0
\end{Verbatim}
    \end{colframe}
  \item To get rid of a bogus \texttt{eth0} device left over from a
    wired connection when your wireless fires up \texttt{eth1}, type

    \begin{colframe}
      \begin{Verbatim}{frame=single}
   $ ifconfig eth0 down
\end{Verbatim}
    \end{colframe}
  \item Happy \LaTeX ing!
\end{itemize}
\end{document}

 ///Peter


Ashish Revar

unread,
Jan 31, 2011, 1:19:11 AM1/31/11
to latexus...@googlegroups.com
Thanks Peter,

For colored background and all, we are using fancyverb and others packages. If I want the same thing for itemize (Lists) or maybe for any matrix or array, then what to do?

Is there any document related to this?
As I want to study the SBox and Minipage which you have coded in last reply.
Can you please explain that code if possible?

--
You received this message because you are subscribed to the Google Groups "LaTeX Users Group" group.
To post to this group, send email to latexus...@googlegroups.com.
To unsubscribe from this group, send email to latexusersgro...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/latexusersgroup?hl=en.

Peter Flynn

unread,
Jan 31, 2011, 5:45:20 AM1/31/11
to latexus...@googlegroups.com
On Mon, Jan 31, 2011 at 6:19 AM, Ashish Revar <ashis...@gmail.com> wrote:
Thanks Peter,

For colored background and all, we are using fancyverb and others packages. If I want the same thing for itemize (Lists) or maybe for any matrix or array, then what to do?

The same:

\begin{colframe}
  \begin{itemize}
    \item foo
    \item bar
    \item blort
  \end{itemize}
\end{colframe}

Is there any document related to this?
As I want to study the SBox and Minipage which you have coded in last reply.
Can you please explain that code if possible?

///Peter

Ashish Revar

unread,
Apr 19, 2011, 4:46:35 AM4/19/11
to latexus...@googlegroups.com
Hello there,

I finally found out the problem which was preventing my file to successful output. In our institute format textwidth  was predefined using below command.

\renewcommand{\textwidth}{6in}

So, I have then commented that line and now it is working. But I want to know that, what was the problem here? 

On Tue, Apr 19, 2011 at 11:01 AM, Ashish Revar <ashis...@gmail.com> wrote:
Hello there,
 
Solution given by Peter was working fine in my previous report. But this time I am using this with my final report it making some conflicts. Below are the packages which I have used in report.

\documentclass[12pt,a4paper]{report}

\usepackage[pdftex]{graphicx}
\DeclareGraphicsExtensions{.pdf,.jpeg,.png}
\usepackage{amsthm,amssymb} % for mathematical formulas
\usepackage{rotating} %used to have landskape figures
\usepackage{graphicx}
\usepackage{enumerate}
\usepackage{epsfig}

%\usepackage{algorithms}
%\usepackage{algorithm}
%\usepackage{algorithmic}
\usepackage{pseudocode}
%\usepackage{fancybox}
%\usepackage{mathpazo} %for good font

\usepackage{subfigure}
%\usepackage{slashbox}
\usepackage{pict2e}
\usepackage{makeidx}
%\usepackage{subfig}
\usepackage{multirow}
\usepackage{nomencl}
   % to have links to figures and citations in PDF version.
\usepackage{hyperref}
\usepackage{setspace}

\usepackage{sectsty,fix-cm}
\usepackage[svgnames]{xcolor}

\usepackage{fancyvrb,fancybox,calc}

\newenvironment{colframe}{%
  \begin{Sbox}
    \begin{minipage}
      {\columnwidth-\leftmargin-\rightmargin-2\fboxsep-2\fboxrule-4pt}

}{%
  \end{minipage}
  \end{Sbox}
  \begin{center}
    \fcolorbox{black}{LightSteelBlue}{\TheSbox}
  \end{center}
}

I have another tex file which is included like this way...
\flushbottom
\include{ProposedArch}
\flushbottom

which contains the code like it...
\begin{Verbatim}{frame=single}
   $ ifconfig eth0 down
\end{Verbatim}
    \end{colframe}

but it shows the error,
! Missing number, treated as zero.
<to be read again>
\let
1.5 \begin{colframe}
?

What is the problem? Which packages are being crashed with each other? I have no idea, I have done minimal examples. 


///Peter

--
You received this message because you are subscribed to the Google Groups "LaTeX Users Group" group.
To post to this group, send email to latexus...@googlegroups.com.
To unsubscribe from this group, send email to latexusersgro...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/latexusersgroup?hl=en.
--
Thanks and Regards,
Ashish Revar,
Nirma University,
ashis...@gmail.com



--
Thanks and Regards,
Ashish Revar,
Nirma University,
ashis...@gmail.com

Ashish Revar

unread,
Apr 19, 2011, 1:31:34 AM4/19/11
to latexus...@googlegroups.com
\newenvironment{colframe}{%
  \begin{Sbox}
    \begin{minipage}
      {\columnwidth-\leftmargin-\rightmargin-2\fboxsep-2\fboxrule-4pt}

}{%
  \end{minipage}
  \end{Sbox}
  \begin{center}
    \fcolorbox{black}{LightSteelBlue}{\TheSbox}
  \end{center}
}

I have another tex file which is included like this way...
\flushbottom
\include{ProposedArch}
\flushbottom

which contains the code like it...
\begin{Verbatim}{frame=single}
   $ ifconfig eth0 down
\end{Verbatim}
    \end{colframe}

but it shows the error,
! Missing number, treated as zero.
<to be read again>
\let
1.5 \begin{colframe}
?

What is the problem? Which packages are being crashed with each other? I have no idea, I have done minimal examples. 

On Mon, Jan 31, 2011 at 4:15 PM, Peter Flynn <angleb...@gmail.com> wrote:

///Peter

--
You received this message because you are subscribed to the Google Groups "LaTeX Users Group" group.
To post to this group, send email to latexus...@googlegroups.com.
To unsubscribe from this group, send email to latexusersgro...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/latexusersgroup?hl=en.



--
Thanks and Regards,
Ashish Revar,
Nirma University,
ashis...@gmail.com
Reply all
Reply to author
Forward
0 new messages