ChatGPT解决这个技术问题 Extra ChatGPT

Inline code in org-mode

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 ...

was going to ask similar question. thanks :D
In markdown we can make a code block by a enclosing lines of code in two lines of `````, which is quite neat IMO. Is there a neat way to do this in org-mode? I don't want to see +BEGIN_SRC... everytime.

T
Tom Regner

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

https://i.stack.imgur.com/WEEyW.png

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.


This is the best answer here, I think. The relevant part in the manual is 14.1 Structure of code blocks.
And the manual says things like “live code blocks require...” What does "live" mean here? Also, I whish the docs had more examples instead of focusing mainly on formal syntax definitions (but that is not a problem with org-mode only; it is more or less a general issue in my opinion).
Live code blocks can be evaluated in org-mode (or during export) and their results get inserted into the file. I use this for UML diagrams with plant-uml and sometimes with Clojure code blocks.
Nice, but we still can't get syntax highlighting this way, can we? And I find it strange that [:exports code] is required, because the docs at orgmode.org/manual/exports.html#exports say that it is the default. (But without it, nothing gets exported when I export the org contents to markdown, for example.)
I don't think of this as 'inline code', but rather a 'code block'. For example, I think of inline code as a file name, a short (< 1 line) code string, usually embedded in other text.
F
François Févotte

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.


Thanks. Having the keywords is being half-way there. If I had known that org-mode talks about this in terms of monospace and emphasis I would have found this myself. :( I was looking for inline and embedded, to no avail.
Yes, I recall having been disturbed by the "emphasis" term myself too, when looking for a feature allowing to hide org markup. Once you know the terminology, you immediately find org-hide-emphasis-markers, but I've searched for ages before finally finding it almost accidentally...
actually the manual just says ~code~ i.e. surrounded with ~.
This answer just makes code look monospaced, it doesn't actually endow it with the executable properties a real BEGIN_SRC block has. @Tom's answer below is better if you want that.
I found ~ ugly. Is there a workaround to use the grave accent instead?
R
Raoul HATTERER

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

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)

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.

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).