TransWikia.com

Package for live inline rendering for markdown/org mode text with css styling?

Emacs Asked on January 10, 2021

Currently, I use Typora, a markdown editor,for all my personal notes and documentation. After seeing the capabilities of emacs in a colleague’s spacemacs setup, I am very interested in switching from writing personal documentation in markdown to org-mode. But, are there are emacs packages/plugins that can match the following two Typora features?

  1. Is there an emacs plugin for live, inline rendering of org-mode or markdown text with css themes? By “inline” rendering, I mean markup is rendered in the same window that you type in (here’s a video of how that works in Typora.)

  2. Is there a way to render images inline in org-mode documentation?

One Answer

The answer to question 2 is yes! If you have an image cat.png in the same folder as you org file, you can insert it using simply [[./cat.png]] and toggle inline display with C-c C-x C-v. Here's a link to the relevant org documentation.

If you want inline images to be displayed by default, as suggested in this answer you can add the following line to your init file:

(setq org-startup-with-inline-images t)

Question 1 is not as simple. Strictly speaking, I don't think you can use CSS to control the display of text in an Emacs buffer. However, there are extensive visual customisation options for Emacs in general and for org-mode in particular which allow us to get an aesthetic similar to that of Typora. For an example of what is possible, see the screenshot below by Abhinav Tushar, described in detail here, or the following Reddit thread.

org-mode demo by Abhinav Tushar

Below I will try to provide an org-mode customisation that makes it visually similar to the Typora example. First, here's how the example text looks in a vanilla Emacs 27 setup (no customisations) on Ubuntu 20.04.

org-mode in vanilla Emacs 27

As you can see, we have hyperlinks by default as well as boldface and italic text. However, emphasis markers (*, /) are still displayed, there is no word wrap, a fixed-width font is used throughout and the title is the same size as the main text.


Basic customisation: no emphasis markers, word wrap and clean UI

We start with simple settings that don't require any additional packages: we hide emphasis markers such as * or /, enable word wrapping by default in org-mode and hide the toolbar, menu bar and scroll bar.

;; hide emphasis markers
(setq org-hide-emphasis-markers t)

;; word wrap
(with-eval-after-load 'org       
  (add-hook 'org-mode-hook #'visual-line-mode))

;; disable toolbar, menu bar and scroll bar
(tool-bar-mode -1)
(menu-bar-mode -1)
(toggle-scroll-bar -1) 

Emacs now looks as follows:

org with first customisations


Changing fonts, removing asterisks, increasing line spacing and changing list markers

The most obvious difference between our example and Typora is now the font. The font in the Typora example looks like a clone of the classic Palatino. Due to Palatino's popularity, some version of this font is available in most systems. However, to make this answer as general as possible, we will use the TeX Gyre Pagella font, a Palatino clone developed primarily with LaTeX in mind and which therefore has good math support. It's available in PS Type 1 and OTF formats and free to download and use.

After we install Pagella on our system, we can configure org-mode to use it for text while using a monospaced font for code, tables, etc. (I tend to use Adobe's Source Code Pro, which is also free and available on GitHub). We could do this by changing org-mode's settings ourselves but it's easier to have the mixed-pitch package handle this for us.

When using external packages, it's convenient to set MELPA as the default repository and have use-package take care of the package configuration, so we start by adding the following lines to the start of our init file:

;; set up MELPA package repo
(require 'package)
(add-to-list 'package-archives
             '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)

;; use-package to simplify package loading
(unless (package-installed-p `use-package)
  (package-refresh-contents)
  (package-install `use-package))
(eval-when-compile
  (require 'use-package))

We can now add the relevant code for changing fonts:

;; set fixed-width font
(set-face-font 'default "Source Code Pro-12")

;; set variable-width font
(set-face-font 'variable-pitch "TeX Gyre Pagella-13")

;; set org-mode to use variable width fonts smartly
(use-package mixed-pitch
  :ensure t
  :hook (text-mode . mixed-pitch-mode))

To improve the aesthetics further, we can make the header font larger and bold, hide the asterisk using the org-bullets package, use black squares (Unicode character U+25AA) instead of hyphens as list markers (following Diego Zamboni) and increase the line spacing:

;; increase header font size and weight
(custom-set-faces
  '(org-level-1 ((t (:height 1.3 :weight bold))))
  '(org-level-2 ((t (:height 1.2 :weight bold))))
  '(org-level-3 ((t (:height 1.1 :weight bold)))))

;; hide asterisks in headers
(use-package org-bullets
  :ensure t
  :config
  (add-hook 'org-mode-hook (lambda () (org-bullets-mode 1)))
  (setq org-bullets-bullet-list '("u200b")))

;; change list markers from hyphens to squares
(font-lock-add-keywords 'org-mode
                        '(("^ *\([-]\) "
                           (0 (prog1 () (compose-region (match-beginning 1) (match-end 1) "▪"))))))

;; increase line spacing
(setq-default line-spacing 0.5)

This gives us the following appearance:

org with new font


Finishing touches: emulating Typora's Focus Mode

Typora provides two interesting modes for displaying text which can be used either jointly or separately: with Focus Mode, paragraphs other than the current one (where the cursor is) are displayed in a dim grey colour; and with Typewriter Mode, the cursor is always centred vertically on the page.

We can emulate the behaviour of Typora's Focus Mode with the focus package. After installing and loading the package, we can turn on focus-mode with M-x focus-mode. For Typewriter Mode, things are more complicated: the centered-cursor-mode package implements this behaviour but is still under development and is not available on the MELPA repository. However, you can modify Emacs settings to achieve a similar functionality if you wish.

Going beyond these two modes, I would also recommend the lovely olivetti package: it increases the text's side margins, making it easier and more pleasant to read. We add the following lines to our init file:

;; focus mode
(use-package focus
  :ensure t)

;; olivetti for wider margins
(use-package olivetti
  :ensure t)

Finally, we can set a new theme such as doom-tomorrow-day (this has the advantage of changing the colour of the text dimmed by focus to grey rather than dark red as in the default theme) and remove the modeline:

;; hide modeline
(setq-default mode-line-format nil) 

;; change theme
(use-package doom-themes
  :ensure t
  :config
  ;; Global settings (defaults)
  (setq doom-themes-enable-bold t    ; if nil, bold is universally disabled
        doom-themes-enable-italic t)  ; if nil, italics is universally disabled
  (load-theme 'doom-tomorrow-day t)  ; theme
  (doom-themes-org-config))  ; corrects (and improves) org-mode's native fontification

Our Emacs now looks like this:

final Emacs look

And that's it! As you can see, Emacs can do most of what you might want out of a text editor and more. ;)

Correct answer by George Costanza on January 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP