15.2 Structure of Code Blocks
Org offers two ways to structure source code in Org documents: in a source code block, and directly inline. Both specifications are shown below.
A source code block conforms to this structure:
#+NAME: <name>
#+BEGIN_SRC <language> <switches> <header arguments>
<body>
#+END_SRC
Do not be put-off by having to remember the source block syntax. Org mode offers a command for wrapping existing text in a block (see Structure Templates). Org also works with other completion systems in Emacs, some of which predate Org and have custom domain-specific languages for defining templates. Regular use of templates reduces errors, increases accuracy, and maintains consistency.
An inline code block conforms to this structure:
src_<language>{<body>}
or
src_<language>[<header arguments>]{<body>}
β#+NAME: <name>
ββ
Optional. Names the source block so it can be called, like a function, from other source blocks or inline code to evaluate or to capture the results. Code from other blocks, other files, and from table formulas (see The Spreadsheet) can use the name to reference a source block. This naming serves the same purpose as naming Org tables. Org mode requires unique names. For duplicate names, Org modeβs behavior is undefined.
β#+BEGIN_SRC
β β¦ β#+END_SRC
ββ
Mandatory. They mark the start and end of a block that Org requires. The β#+BEGIN_SRC
β line takes additional arguments, as described next.
β<language>
ββ
Mandatory. It is the identifier of the source code language in the block. See Languages, for identifiers of supported languages.
β<switches>
ββ
Optional. Switches provide finer control of the code execution, export, and format (see the discussion of switches in Literal Examples).
β<header arguments>
ββ
Optional. Heading arguments control many aspects of evaluation, export and tangling of code blocks (see Using Header Arguments). Using Orgβs properties feature, header arguments can be selectively applied to the entire buffer or specific sub-trees of the Org document.
β<body>
ββ
Source code in the dialect of the specified language identifier.