2

How can I define custom paragraph blocks in org mode with a custom fontification, for example something like

#+BEGIN_MYPAR
Some text 
#+END_MYPAR

Then I want for example that this block is displayed in orange font color, one point larger than the rest of the text, bold face and indented by one tab.

How can I do this?

Edit:

The question is about how things are displayed in the emacs buffer, not in exports (though this may have been a follow up question).

1

2 Answers 2

2
+100

You reach your goal quite simple if you use source blocks instead of special blocks. Define your own major mode, say mypar-mode with the help of define-generic-mode or define-derived-mode and setup the fontification for that major mode like you want it.

Make sure that org-src-fontify-natively and org-src-preserve-indentation are both set to t such that the mypar source blocks in the Org buffer look as much as possible like the fontified source buffers.

If you have htmlize installed you get most of the wanted formatting in the HTML export for free.

A code example that I have tested with Emacs 27.1 and Org 9.3.7:

(require 'org)
(require 'cl-lib)
(define-generic-mode mypar-mode
  nil ;; comment-list
  nil ;; keyword-list
  ;; font-lock-list:
  '(("^.*$"
     ;; match-highlight:
     (0 ;; subexpression
      ;; facename:
      `(face (:inherit default :foreground "orange" :height 1.5 :weight bold) ;; we use an anonymous face
         ;; indent:
         line-prefix "\t"
         wrap-prefix "\t")
      )))
  nil ;; auto-mode-list
  '((lambda ()
      "Make `line-prefix' and `wrap-prefix' work in the source fontification buffer."
      (setq-local font-lock-extra-managed-props '(line-prefix wrap-prefix))
      ));; function-list
  "Formatting MYPAR blocks.")

(defun org+-hack-org-fontification ()
  "Make `wrap-prefix' and `line-prefix' text properties work in Org buffers."
  (setq-local font-lock-extra-managed-props (cl-union
                         '(line-prefix wrap-prefix)
                         font-lock-extra-managed-props)))

(add-hook 'org-mode-hook #'org+-hack-org-fontification)
(setq org-src-fontify-natively t
      org-src-preserve-indentation t)

At last an image how a mypar source block looks like in Org:

display of a mypar source block

1
  • Is there an option such that latex fragment previews work also in your mypar environment?
    – student
    Sep 17, 2020 at 6:37
1

First note, that your question is a bit vague. Where do you want to have this special formatting of the paragraph -- in HTML-export or LaTeX-export? Or even in the representation of the Org-buffer in Emacs?

I give you here an answer for HTML- and LaTeX-export.

The main idea for HTML-export is that MYPAR determines the style class of the text in the special block.

You can define your own style within #+HTML_HEAD meta-lines.

The main idea for LaTeX-export is that MYPAR determines the LaTeX environment where the text of the special block is put in. You can define that custom environment within #+LATEX_HEADER meta-lines.

An example:

Some text before my paragraph. Let this paragraph have some more text. It should have at least two lines.
#+BEGIN_MYPAR
Some text in my paragraph. Let this paragraph have some more text. It should have at least two lines. Even a third line would be nice to have. So we continue this text for some time.
#+END_MYPAR
Some text after my paragraph. Let this paragraph have some more text. It should have at least two lines.

* Style :noexport:
This section is just for definition of HTML and LaTeX formatting.
#+HTML_HEAD: <style>.MYPAR { color:orange; text-indent:8em; font-weight:bold; foint-size:110% }</style>
#+LATEX_HEADER: \usepackage{xcolor}
#+LATEX_HEADER: \newdimen\myparindent\myparindent8em
#+LATEX_HEADER: \newdimen\myparwidth\myparwidth\textwidth\advance\myparwidth-\myparindent
#+LATEX_HEADER: \newenvironment{MYPAR}{\par\noindent\hfill\begin{minipage}[t]{\myparwidth}\bf\color{orange}\fontsize{1em}{1.2em}\selectfont}{\par\xdef\tpd{\the\prevdepth}\end{minipage}\par\prevdepth\tpd}
See https://tex.stackexchange.com/questions/35933/indenting-a-whole-paragraph for using ~minipage~ for indenting a whole paragraph.

1
  • Thanks, but see my edit above. The question is indeed about how it is displayed in the emacs buffer.
    – student
    Aug 23, 2020 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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