195

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?

  • 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/… – Camille Goudeseune May 3 '17 at 17:30
  • 1
    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
223

You can use the xcolor package. 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.

  • 2
    great, \color{<color>} is what i need, thank you very much! – SoftTimur May 1 '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 1 '11 at 23:27
  • 7
    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 2 '11 at 0:40
  • @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 2 '11 at 8:39
  • 2
    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 7 '18 at 12:16
30

The following seems simpler:

\documentclass{amsproc}
\usepackage[colorlinks]{hyperref}
\begin{document}
    This is {\color{red} highlighted}, and this is not.
\end{document}
  • 2
    I think your solution is exactly what @Martin Scharrer is suggesting? – Tom Bombadil Oct 16 '15 at 9:50
  • 1
    No. @Martin Scharrer suggested using the xcolor package, whereas this answer suggests using the hyperref package. – martinweiss Oct 26 '16 at 9:14
  • As a side note: This solution does not change colour of a text in an equation unless xcolor package is used. – KutalmisB Sep 6 '17 at 14:53
  • 4
    I'm pretty sure \color is not a hyperref command, but that hyperref loads color or xcolor on its own. – Teepeemm May 30 '18 at 13:13
8

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.