I'm only looking to format a specific string within a cell. I change that cell's format to "Markdown" but I'm not sure how to change text color of a single word.
I don't want to change the look of the whole notebook (via a CSS file).
You can simply use raw html tags like
foo <font color='red'>bar</font> foo
Be aware that this will not survive a conversion of the notebook to latex.
As there are some complaints about the deprecation of the proposed solution. They are totally valid and Scott has already answered the question with a more recent, i.e. CSS based approach. Nevertheless, this answer shows some general approach to use html tags within IPython to style markdown cell content beyond the available pure markdown capabilities.
File
> Export Notebook as...
> PDF
, as the resulting PDF document shown the colored word in black. Is there any alternative?
Commented
Nov 11, 2021 at 10:16
Similarly to Jakob's answer, you can use HTML tags. Just a note that the color
attribute of font
(<font color=...>
) is deprecated in HTML5. The following syntax would be HTML5-compliant:
This <span style="color:red">word</span> is not black.
Same caution that Jakob made probably still applies:
Be aware that this will not survive a conversion of the notebook to latex.
ipython nbconvert --to latex mynotebook.ipynb
. Alternatively, in an open notebook you can go to File > Download as... > PDF via LaTex. But I believe that color specification via html/css style may not be translated correctly into the LaTex. I don't have LaTex installed at the moment, so I can't re-verify, but I think that's the case.
An alternative way to do that, is to enter a LaTeX environment within the notebook and change color from there (which is great if you are more fluent in LaTeX than in HTML). Example:
$\color{red}{\text{ciao}}$
would display ciao
in red.
For example, if you want to make the color of "text" green, just type:
<font color='green'>text</font>
I have tested the ideas that I found in these answers in google colabs.
<span style='color:red'>green</span>
$\color{green}{\text{blue}}$
<font color='red'>orange</font>
Of these the 2nd (LaTeX $…$
, and html font) versions work. The style version does not work.
Note: the html font
element is deprecated. Therefore I am using the LaTeX version.
If none of the above suggestions works for you, try using the style
attribute.
**Notes**
<p style="color:red;">ERROR: Setting focus didn't work for me when I tried from jupyter. However it worked well when I ran it from the terminal</p>
This gives me the following result
The text color can be changed using,
<span style='color:green'> message/text </span>
<p style="font-family: Arial; font-size:1.4em;color:gold;"> Golden </p>
or
Text <span style="font-family: Arial; font-size:1.4em;color:gold;"> Golden </p> Text
<span style='color:blue '> your message/text </span>
So here it is a perfect html css style entry inside a notebook ipynb file.
Of course you can choose your favourite color here and then your text.
This is a very simple and effective trick for google colab. Use the (empty) link syntax of the markdown.
[
your_message]()
Then you'll get the blue text (underline).