Inline code in org-mode

EmacsMarkdownOrg Mode

Emacs Problem Overview


Markdown allows for embedded code. How can this be done in org-mode?

I know about source-code blocks:

#+begin_example
blah-blah
#+end_example

But what I want is something like this (obviously, with the right syntax, which I do not know):

This is `embeded code`.

Can this be done in org-mode? Not possible to find that in the documentation ...

Emacs Solutions


Solution 1 - Emacs

While monospaced is good enough for most cases, inline code blocks have the form src_LANG[headers]{your code}. For example, src_xml[:exports code]{<tag>text</tag>}.

Edit: Code highlighting of inline code is certainly possible, albeit with patching org.el itself: The answer given here https://stackoverflow.com/a/20652913/594138 works as advertised, turning

- Inline code src_sh[:exports code]{echo -e "test"}

Into

enter image description here

in html-export. And the winning answer in this post, https://stackoverflow.com/a/28059832/594138, achieves the same without the need to patch org.el, but you will have to adapt it if you don't like the optics during editing.

Solution 2 - Emacs

You can enclose the text within = or ~ signs to have it typeset in monospaced font and export it verbatim (which means it is not processed for org-specific syntax):

This is =verbatim text= or ~code~.

You'll find all information about org-mode markup elements in the relevant section of the manual.

Solution 3 - Emacs

I wrote a function which I hope will be useful to help manage the code inline.

  1. You put this code in your init file
(defun org-insert-inline-code()
  "This function insert inline code `src_lang{inline code}' \nYour buffer must contain  '#+PROPERTY: header-args:lang    :exports code' where `lang` can be python or an other programming language."
  (interactive  (if (use-region-p)
		    (progn
		      (setq start (region-beginning))
		      (setq end (region-end))
		      (goto-char start)
                      (if (re-search-backward "^#\\+PROPERTY: header-args:[^[:blank:]]*" 1 t 1)
                          (progn
                            (forward-char 24)
                            (setq org-inline-lang (word-at-point))
		            (goto-char start)
		            (insert (concat "src_" org-inline-lang "{"))
		            (goto-char (+ 11 end))
		            (insert "}")                            
                            )))
		  (progn
                    (setq start (point))
                    (if (re-search-backward "^#\\+PROPERTY: header-args:[^[:blank:]]*" 1 t 1)
                        (progn
                          (forward-char 24)
                          (setq org-inline-lang (word-at-point))
		          (goto-char start)
		          (insert (concat "src_" org-inline-lang "{} "))
		          (backward-char 2)
                            ))))))

(define-key org-mode-map (kbd "C-M-,") 'org-insert-inline-code)
  1. You put this kind of PROPERTY in the org-file
#+PROPERTY: header-args:python    :exports code

The required [:exports code] is given that way and the programming language can be identify by the function too.

  1. Insert the code in line with C-M-, (the function then search back to read the language in the PROPERTY line and insert the correct command).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionblueFastView Question on Stackoverflow
Solution 1 - EmacsTom RegnerView Answer on Stackoverflow
Solution 2 - EmacsFrançois FévotteView Answer on Stackoverflow
Solution 3 - EmacsRaoul HATTERERView Answer on Stackoverflow