361

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?

2
  • 1
    The traditional way to de-emphasize blocks of text in an editor is called text folding (or code folding, for software). stackoverflow.com/questions/1085170/… Commented May 3, 2017 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
    Commented May 30, 2018 at 13:14

4 Answers 4

435

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}
9
  • 5
    great, \color{<color>} is what i need, thank you very much!
    – SoftTimur
    Commented May 1, 2011 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
    Commented May 1, 2011 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
    Commented May 2, 2011 at 0: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. Commented May 2, 2011 at 8:39
  • 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
    Commented Oct 7, 2018 at 12:16
55

The following seems simpler:

\documentclass{amsproc}
\usepackage[colorlinks]{hyperref}
\begin{document}
    This is {\color{red} highlighted}, and this is not.
\end{document}
4
  • 3
    I think your solution is exactly what @Martin Scharrer is suggesting? Commented Oct 16, 2015 at 9:50
  • 2
    No. @Martin Scharrer suggested using the xcolor package, whereas this answer suggests using the hyperref package. Commented Oct 26, 2016 at 9:14
  • As a side note: This solution does not change colour of a text in an equation unless xcolor package is used. Commented Sep 6, 2017 at 14:53
  • 10
    I'm pretty sure \color is not a hyperref command, but that hyperref loads color or xcolor on its own.
    – Teepeemm
    Commented May 30, 2018 at 13:13
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

1
  • izzani this is awesome! :) Commented Mar 8 at 0:33
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.

You must log in to answer this question.

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