352

I am editing a LaTeX paper with Emacs. Sometimes I want to make a block of texts less obvious (or less important to see). Instead of totally hiding them, they should still be there. Is there an easy way to embed them in something so that it changes color (to grey for instance)?

By the way, besides changing color, are there other ways to easily make a block of texts look less important?

SoftTimur
  • 19,767
  • 1
    The traditional way to de-emphasize blocks of text in an editor is called text folding (or code folding, for software). http://stackoverflow.com/questions/1085170/how-to-achieve-code-folding-effects-in-emacs – Camille Goudeseune May 03 '17 at 17:30
  • 4
    The question wasn't clear, but the answers seem to be focused on changing the color of the output text, not the color of the text in the editor. – Teepeemm May 30 '18 at 13:14

4 Answers4

427

You can use the xcolor package (which can be installed via tlmgr install xcolor). It provides \textcolor{<color>}{<text>} as well as \color{<color>} to switch the color for some give text or until the end of the group/environment. You can get different shades of gray by using black!x as a color where x is a number from 0 to 100, taken as a percentage with 100 being black.

\usepackage{xcolor}
\begin{document}

This is a sample text in black. \textcolor{blue}{This is a sample text in blue.}

\end{document}

Martin Scharrer
  • 262,582
  • 5
    great, \color{} is what i need, thank you very much! – SoftTimur May 01 '11 at 18:13
  • Just one thing, what if my block of code is embedded in \begin{align} and \end{align}, which is under math mode. \textcolor... or \color... work on a single word, but not on a block of words... – SoftTimur May 01 '11 at 23:27
  • 15
    No, \textcolor{green}{a couple of words} colors the entire second argument. \color{green} is a declaration, which changes the text color of everything that follows (in that scope, or group as its called in TeX). It is analogous to \textbf versus \bfseries. \color should also work in math mode. But some of the amsmath environments do rather complicated things in order to work (reading the contents several times, for example). But then you could try `{\color{green} \begin{align} blabla \end{align} } (note the outer braces). – Villemoes May 02 '11 at 00:40
  • 1
    @SoftTimur: Like Villemoes said, \color should work in mathmode. It would be best if you give an example so we can see where the problem lies. You could edit your question for this. – Martin Scharrer May 02 '11 at 08:39
  • I am using this right now and the color is not changing back even though I a using the form that should color only a few words. – Elliot Apr 22 '13 at 18:10
  • I had to throw braces around the entire thing and skip the inner braces to get it to work. – Elliot Apr 22 '13 at 18:29
  • @Elliot: This is not normal. Which class are you using? In which context are you using the macro, i.e. inside normal text or a table or a different one? – Martin Scharrer Apr 22 '13 at 20:17
  • 8
    The following color names are defined: red, green, blue, cyan, magenta, yellow, black, gray, white, darkgray, lightgray, brown, lime, olive, orange, pink, purple, teal, violet. See the package documentation. – Matsmath Oct 07 '18 at 12:16
  • @SoftTimur Dr. Scharrer's accepted answer is probably a better suggestion since \textcolor is usually a better choice unless you are setting a color for a large amount of text, such as the whole document. Please see this post from TeX guru and and an expert on the subject David Carlisle. – nam Nov 01 '19 at 18:28
53

The following seems simpler:

\documentclass{amsproc}
\usepackage[colorlinks]{hyperref}
\begin{document}
    This is {\color{red} highlighted}, and this is not.
\end{document}
Tom Bombadil
  • 40,123
Glasby
  • 701
27

All the above answers are excellent for general purpose

If you want to highlight a syntax for a programming language we can use minted package, it provides excellent colored highlighted text.

To use this package you need to have python installed on your system (you can install python using anaconda package it's great choice) in addition to a package called pygments (to install this package use pip install pygments) it's a python library used to highlight programming language syntax

I'll give a simple example of how to use it

\usepackage{minted}
\usemintedstyle{vim} %note here you can use different highlighting
                      %styles see the final note below

\begin{document}

\begin{minted}{python}
import matplotlib.pyplot as plt
import numpy as np

T=1
delta_T=T/200
alpha=0.5
fc=40/T
A_m=1
t=[i for i in np.arange(-5,5,1/200)]
t_arr=np.array(t)
N=len(t)

g_T=[]
for i in range(N):
    if (abs(t[i])!=(T/2*alpha)):
        g_T.append(np.sinc(t[i])*(np.cos(np.pi*alpha*t[i]/T)/
            (1- 4*alpha**2 *(t[i])**2 /(T**2))))
    else:
        g_T.append(0)
\end{minted}

\end{document}

This code will produce the following This the output of the above code

Note you need to enable " -shell-escape " option with either LaTeX or pdfLaTeX

latex -shell-escape foo.tex

or

pdflatex -shell-escape foo.tex

If you want to get different highlights, on the command line use

pygmentize -L styles

You'll get different styles of highlighting

CHEERS, Hizzani

Mohammad Hizzani
  • 523
  • 1
  • 5
  • 11
11

Use the package

\usepackage{xcolor}

and right after that write this line given below :

\newcommand{\myRed}[1]{\textcolor{red}{#1}}

Now wherever you want to highlight some text use \myRed{}.

When you compile, you will get text in \myRed in red color.

Leucippus
  • 1,636