Text Formattingcolors in verbatim: only some words of a line

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
cfdprogrammer
Posts: 6
Joined: Sun Apr 19, 2009 4:58 pm

colors in verbatim: only some words of a line

Post by cfdprogrammer »

Hi there,

I am trying to color only certain words within a verbatim environment. I know how to color the entire verbatim or certain lines, but what I need is a little different: if a line contains 2 words, I only want one of the two to be colored but wouldn't know how to do that.

I post what I tried, but with no success (I am using the color package):

Code: Select all

{\color{red} {string1}} string2 string3
where only string1 must be colored.

Thank you very much in advance
All the best
cfd

Recommended reading 2024:

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

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

User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

colors in verbatim: only some words of a line

Post by localghost »

A verbatim environment doesn't accept any control sequences. Perhaps you can tell a little bit more about the result you want to get. Probably there are some other solutions.


Best regards and welcome to the board
Thorsten¹
cfdprogrammer
Posts: 6
Joined: Sun Apr 19, 2009 4:58 pm

colors in verbatim: only some words of a line

Post by cfdprogrammer »

Hi there, thanks for replying so promptly;

I do not necessarily need to use verbatim, but just some environment that allows the type writer font to be used and that, at the same time, allows coloring of certain strings only

Imagine that the line

Code: Select all

string1 string2 string3
is written with a type-writer type font, and that string1 only is red

thank you again
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

colors in verbatim: only some words of a line

Post by localghost »

You could use the alltt package which provides an environmentwith the same name. Or just use the switches \ttfamily and then \rmfamily. In the case you want to format programming code, take a look at the listings package.

Code: Select all

\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{babel}
\usepackage{alltt}
\usepackage{xcolor}
\usepackage{lmodern}

\parindent0em

\begin{document}
  \begin{alltt}
    \textcolor{red}{example} enumerate
  \end{alltt}

  \ttfamily
  \textcolor{red}{example} enumerate
  \rmfamily
\end{document}
cfdprogrammer
Posts: 6
Joined: Sun Apr 19, 2009 4:58 pm

Re: colors in verbatim: only some words of a line

Post by cfdprogrammer »

Great! Thank you very much, it does exactly what I need

All the best
Post Reply