2

Insert the following text in an org buffer with electric-indent-mode on.

#+begin_src python s=''' #+1 #+1''' #+end_src 

add a newline to s, and the buffer will become:

#+begin_src python s=''' ,#+1 ,#+1 ''' #+end_src 

seems the extra "," is added for no good reason.

2
  • I can verify this in Emacs 29.3. The lines are also indented, but I assumed that is just StackExchange formatting. Commented Jul 17, 2024 at 5:02
  • 3
    The comma is the escape symbol, otherwise it would clash with Org-Mode syntax (lines starting with #+). It disappears when you edit the source code (C-c ') or do an export. Commented Jul 17, 2024 at 5:42

2 Answers 2

2

TL;DR: This is default org-mode parsing. It isn't going to affect your code when you run it and won't show up when you export either, so it's safe to ignore it if you want.

To prevent commas, you could advise your indent function to act differently in org source-mode blocks. It seems like it's part of the parsing process, so it might be easier to just do a pass at the end to remove them. That is not advisable however: it can lead to problems with exporting.

Removing the commas might make a difference to other functions/programs parsing your code, but I have tried exporting and executing the code both with and without the commas and I didn't notice a difference. This might be true for the OP's simple case, but it is not true in general.

Why is this happening? It is standard for org-mode to treat any line starting with #+ as a keyword option. It prepends the comma to avoid org parsing the line as a keyword option.

This is also true for headlines, i.e. lines beginning with *: see this reddit thread

Also note that this behavior looks to be specific to source blocks. I tried the same thing with #+begin_quote and #+end_quote and it did not prepend a comma. But if you include things that look like headlines in a quote block (or any other block for that matter), the export will break.

I'm not sure why you need #+1, but this will not happen if you use # +1.

Another possibility is to just hide the commas, possibly as part of a syntax highlight pass or something.

6
  • 2
    #+ does not signal a heading to Org mode: it signals a keyword option - see e.g. Summary of In-Buffer Settings. For the comma behavior, see Literal examples in the Markup for Rich Contents section. As this last reference makes clear, this is NOT specific to source blocks. Please fix your answer and use refs to the manual if possible, using links to Emacs SE questions only for additional support and clarification. Commented Jul 17, 2024 at 13:26
  • 2
    I have tried exporting and executing the code both with and without the commas and I didn't notice a difference: there is certainly a difference - see my answer. Commented Jul 17, 2024 at 14:21
  • 1
    Your answer, manual links, and examples are very helpful and I will link to them as well as clarify how I exported. I exported using org-html-export-as-html and there is no difference in the raw html output or the syntax-highlighting in the browser. I also tested lines * and ,* with no difference in export output or code execution ouput. Granted, this is a source block and not a example or quote, but the behavior does seem to contradict at least that part of the manual. Commented Jul 17, 2024 at 16:05
  • 1
    Did you HTML-export the OP's example or my example? There is certainly a difference in my example. Even if there is no difference in the OP's example however, you cannot generalize from one example: you can only illustrate, and that, in itself, does not mean that the manual is wrong: it only means that the example you tried was too simple to be a complete illustration. Commented Jul 17, 2024 at 16:23
  • 1
    It is important that accepted answers in particular are as correct as possible. Since you have not corrected the inaccuracies I pointed out, I did a correction pass to limit the damage. But you should really edit your answer and fix it properly. Commented Jul 20, 2024 at 16:29
2

As Bram Schoenmakers points out in a comment, the commas are escape characters that protect Org mode from misinterpreting things in a block that look like, but are not really, Org mode constructs. See Literal Examples in the Org mode manual.

@bikemule's answer is generally correct, except for a few inaccuracies, as noted in my comment beneath his answer. He also does not provide references to the manual, which is the authoritative arbiter of such matters (modulo bugs of course: the ultimate arbiter is the code but that should be a last resort). I could fix up his answer, but I will let him do that and, instead, provide an example in the form of an Org mode file that contains instructions of what to do, in order to demonstrate the differences.

Here's the example:

#+TITLE: Literal example #+AUTHOR: A.U. Thor * One section But it contains a literal example with un-escaped constructs that Org mode parses as its own: #+begin_example This is supposed to be a literal example, but it contains something that looks like a keyword option: #+AUTHOR: R.O. Htua and it contains something that looks like an Org mode headline: ,** Example #+end_example Now export this to PDF and check. Then delete the two commas and export again. Notice the difference? 

Follow the instructions on the last line to export the file twice: once with the commas included and once with the commas deleted. Here are screenshots of what the resulting PDF files look like, first with the commas included:

PDF output - with commas to escape problematic lines

and then with the commas deleted:

PDF output - with commas deleted

Note the fact that the latter contains a (spurious) subsection, the example block has been mangled and there are TWO authors!


References: in addition to the link above, the following links are useful:

  • Markup for Rich Content - this chapter contains the Literal Examples section that I linked to above, but it also contains much useful information about how an Org mode buffer in Emacs can change its appearance.

  • Summary of In-Buffer Settings - this section describes many (but maybe not all) keyword options (i.e. those signaled by #+<keyword> in the Org mode file).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.