Generaladd color to verbatim environments

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
stephentucker
Posts: 2
Joined: Wed Jul 30, 2008 1:04 am

add color to verbatim environments

Post by stephentucker »

Hi list,

I was wondering if anyone knew of a way to add colors to two successive verbatim text blocks without adding an extra blank line in between. For instance, the spacing I desire is the output of:

Code: Select all

\begin{verbatim}
line 1
line 2
\end{verbatim} 
\begin{verbatim}
line 3
line 4
\end{verbatim}
But I want to add color to these two verbatim environments. The two ways I know how are
1. (using \usepackage[usenames]{color})

Code: Select all

{\color{red}\begin{verbatim}
line 1
line 2
\end{verbatim} }
\color{blue}{\begin{verbatim}
line 3
line 4
\end{verbatim}}
and
2. (here, using \usepackage{fancyvrb} also)

Code: Select all

\begin{Verbatim}[formatcom=\color{red}]
line 1
line 2
\end{Verbatim} 
\begin{Verbatim}[formatcom=\color{blue}]
line 3
line 4
\end{Verbatim}
But in both of these cases, an extra blank line is added in between the two colored sections of text in the output. I wonder if anyone knows how to work around it, to get the same spacing as with the two successive, uncolored verbatim environments?

Thanks very much!

Stephen

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

add color to verbatim environments

Post by Juanjo »

Try this, which uses the fancyvrb package:

Code: Select all

\renewcommand{\FancyVerbFormatLine}[1]{%
  \ifnum\value{FancyVerbLine}<5\color{red}#1%
  \else\color{blue}#1\fi}
\begin{Verbatim}
line 1
line 2
line 3
line 4
line 5
line 6
line 7
\end{Verbatim}
In the second line of code, replace 5 by the number of the first line of the blue block.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
stephentucker
Posts: 2
Joined: Wed Jul 30, 2008 1:04 am

Re: add color to verbatim environments

Post by stephentucker »

Yes!!!
Thank you - I couldn't even figure out if I was supposed to poke around in the 'color' or 'fancyvrb' package.
But this is excellent - I can script some text processing to combine the two successive verbatim environments and invoke the renewcommand for each section with the appropriate number of lines.
Thank you!!!
samuel roberson
Posts: 1
Joined: Wed Feb 23, 2011 3:15 pm

Re: add color to verbatim environments

Post by samuel roberson »

A nice bit of code. Thanks!
bleagos
Posts: 5
Joined: Mon Mar 07, 2011 12:27 pm

add color to verbatim environments

Post by bleagos »

Hey,
I was just wondering how you would choose specific lines to be red...like just the 5th and 7th line, is there anyway to do this similar to the previous code, something like this:

Code: Select all

\renewcommand{\FancyVerbFormatLine}[1]{%
  \ifnum\value{FancyVerbLine}=5 or 7\color{red}#1%
  \else\color{black}#1\fi}
Last edited by Stefan Kottwitz on Fri May 27, 2011 10:57 pm, edited 1 time in total.
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

add color to verbatim environments

Post by Juanjo »

Assuming that black is the default color, just type

Code: Select all

\renewcommand{\FancyVerbFormatLine}[1]{%
  \ifnum\value{FancyVerbLine}=5\color{red}#1%
  \else%
  \ifnum\value{FancyVerbLine}=7\color{red}#1%
  \else #1\fi\fi}
If black is not the default color, replace the last line by

Code: Select all

   \else\color{black}#1\fi\fi}
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
bleagos
Posts: 5
Joined: Mon Mar 07, 2011 12:27 pm

Re: add color to verbatim environments

Post by bleagos »

That is wicked, thanks :)
mirlaine
Posts: 1
Joined: Fri May 27, 2011 4:50 am

add color to verbatim environments

Post by mirlaine »

Hi,

I am using a command like

Code: Select all

\renewcommand{\FancyVerbFormatLine}[1]{%
  \ifnum\value{FancyVerbLine}=5\color{red}#1%
  \else%
  \ifnum\value{FancyVerbLine}=7\color{red}#1%
  \else #1\fi\fi}
however, only line 7 is red colored. I am using beamer in order to produce a presentation. Any explanation is welcome.


Thanks in advance.
Last edited by Stefan Kottwitz on Fri May 27, 2011 10:57 pm, edited 1 time in total.
Post Reply