Emacs Org Mode code blocks with and without colors

Emacs Org Mode code blocks with and without colors

sharon-pittaway-98257-unsplash.jpg

Figure 1: Photo by Sharon Pittaway on Unsplash

1 Coloring the code blocks in Emacs Org Mode

Emacs Org Mode is an excellent environment to develop any kind of argument.
If you need to display blocks of source codes in Org Mode, it may be very useful to know that there are two basic versions for displaying them.

The first one is the monochromatic visualisation mode.
In order to obtain this visualisation you must just wrap your code block into the tags #+BEGIN_SRC and #+END_SRC.

This is already a good way of displaying source codes.

But you can get a prettier look by highlighting the syntax of the various languages with a nice color scheme.

Org Mode can do it for you.

In order to get the colorful visualisation mode you must have installed the htmlize library in your Emacs configuration.

2 How to install the htmlize library

Code block syntax highlighting in Org-mode needs the htmlize library.
This library should already be installed in the most recent Emacs releases.
But, if not, you can easy install it via MELPA (assuming that you have already configuret it MELPA repository).

First of all hit M-x inside Emacs and follow one of the methods above.

The easiest method is the following one: Hit the keyboard as follows (RET is for the Return key): list-packages RET, search for htmlize library by scrolling or using I-search command (C-s), press RET followed by i to select the library for the subsequent installation, press x to install it.
It's easy, isn't it?

But the speedest method is another one: Hit the keyboard as follows: package-install RET htmlize RET, that's all.

Now you have installed everything you need and you can get the colored syntax by adding to the opening tag the name of the specific language.

So, if you want to color an HTML code you must write +-BEGIN_SRC html, if you want to color a LaTeX code you must write +-BEGIN_SRC latex, if you want to color a Ruby code you must write +-BEGIN_SRC ruby, and so on.

3 Colored Code Examples

To give an idea of what I mean you can see the following examples.

3.1 First example: HTML code with and without colored syntax.

Below is a small fragment of HTML code with colored syntax followed by the same block without colored syntax:

<h1>Title of you web page</h1>
<h2>First chapter</h2>
Some text inside the first chapter
<h2>Second chapter</h2>
Some text inside the second chapter
<ol>
    <li>Firs ordered list item</li>
    <li>Second ordered list item</li>
</ol>
<h1>Title of you web page</h1>
<h2>First chapter</h2>
Some text of the first chapter
<h2>Second chapter</h2>
Some text of the second chapter
<ol>
    <li>Firs ordered list item</li>
    <li>Second ordered list item</li>
</ol>

3.2 Second example: LaTeX code with and without colored syntax.

Now you can see a small fragment of LaTeX code with colored syntax followed by the same block without colored syntax:

\section{First Section}
Some text inside the first section
\subsection{First Subsection}
Some test inside the firse subsection
\begin{itemize}
\item{First item}
\item{Second item}
\end{itemize}
\section{First Section}
Some text inside the first section
\subsection{First Subsection}
Some test inside the firse subsection
\begin{itemize}
\item{First item}
\item{Second item}
\end{itemize}

3.3 Third example: Ruby code with and without colored syntax.

And finally you find a small fragment of Ruby code with colored syntax followed by the same block without colored syntax:

m = 0
loop do
  m += 1
  print "Ruby!"
  break if m == 30
end
m = 0
loop do
  m += 1
  print "Ruby!"
  break if m == 30
end

Thank you for your attention.

Author: Franco Pasut

Created: 2019-03-30 sab 20:40

Comments

  1. Source code here refers to any code typed in Org mode documents. Org can manage source code in any Org file once such code is tagged with begin and end markers. Working with source code begins with tagging source code blocks.
    Archive Your Work Sessions in the cloud for free

    ReplyDelete

Post a Comment

Popular posts from this blog

Vim: Cut, Copy and Paste to and from the system clipboard

Vim: searching for text containing a slash or a question mark