I am currently trying to set up a Raku script in Org Mode. However, the Raku major mode does not define a comment syntax. Is there a way to tell the Org Mode comment header argument what comment syntax to use as a header argument?
For example, this code block
* This should be a comment
#+BEGIN_SRC raku :comments org
lorem ipsum
#+END_SRC
when compiled prompts me about not having a defined comment syntax.
For reference, in the Raku Major mode (the official one), comments are properly highlighted like this
# This is a comment
constant Str this = "is not.";
From what I can figure out by reading the Org Mode documentation, the :comments header argument is only meaningful when also using the :tangle header argument. It relates to how contextual comments get added to the tangled code.
https://orgmode.org/manual/Extracting-Source-Code.html#Extracting-Source-Code
The following code block shows three of the comment insertion methods that are available:
* Tangled Header Comment
#+begin_src raku :results output :comments org :tangle yes :shebang #!/usr/bin/env raku
say 'hi';
#+end_src
#+RESULTS:
* Link Comments
#+begin_src raku :results output :comments link :tangle yes :shebang #!/usr/bin/env raku
say 'hello';
#+end_src
* Both Link and Org Comments
#+begin_src raku :results output :comments both :tangle yes :shebang #!/usr/bin/env raku
say 'hello';
#+end_src
After tangling (with Ctrl-c Ctrl-v t) the resulting .raku file looks like this:
#!/usr/bin/env raku
# Tangled Header Comment
say 'hi';
# [[file:~/git/play.org::*Link Comments][Link Comments:1]]
say 'hello';
# Link Comments:1 ends here
# Both Link and Org Comments
# [[file:~/git/play.org::*Both Link and Org Comments][Both Link and Org Comments:1]]
say 'hello';
# Both Link and Org Comments:1 ends here
I don't think this behaviour has anything to do with raku-mode which I installed from Melpa. It does require a Raku backend for Org Babel. I am using ob-raku.el which I think I got from here:
https://github.com/tmtvl/ob-raku
Related
I have a python source block that gets the number of variables (columns) and cases in a pandas' dataframe.
Minimal example:
#+begin_src python :exports none :session :results output
df = pd.DataFrame({'a': [1, 2, 3],
'b': [4, 5, 6]})
df_len_columns = len(df.columns)
df_len_cases = len(df.index)
#+end_src
What I would like to do now is use the value of those variables in inline source code like this:
The number of variables is src_python{df_len_columns} and the number of cases is src_python{df_len_cases}.
But this throws the following error:
NameError: name 'df_len_columns' is not defined
Notice that I'm using the session argument :session thinking that it would be part of the same session and that it would work. I also search online extensively but couldn't find a solution to this particular question (most questions are about inline code for tables and inline code formatting).
Is there anyway to actually use these variables inline?
It looks like you need to tell the inline source code to refer to your session using a header argument:
src_python[:session]{df_len_columns}
The general form is src_<language>[<header arguments>]{<body>}. Possible header arguments are listed in the org manual: Specific Header Arguments.
Note: The value is substituted when the org file is exported via org-export-dispatch.
If I understood correctly you want to link several source blocks to the same process.
It is as simple as:
#+BEGIN_SRC ipython :results output :session testing
a = 5
#+END_SRC
#+RESULTS:
#+BEGIN_SRC ipython :results value :session testing
a
#+END_SRC
#+RESULTS:
: # Out[6]:
: : 5
In this example the two source blocks are linked.
You can also tangle all the source blocks into one .py file.
using M-x org-babel-tangle-file, Every time you run that command the file will update with the new tangle.
When writing a literate program in Org mode, exporting is analogous to weaving in earlier literate programming tools such as cweb or noweb. Those tools would add a code block name to the woven (exported) output. In Org mode, it would look something like this:
Org file:
#+NAME: mycodeblock
#+BEGIN_SRC language
[Source code here]
#+END_SRC
Exported output:
<mycodeblock>=
[Source code here]
I am wondering if there is any support in Org mode for exporting names of code blocks in this style. If not, is there any way to at least output the name of the code block as a label of some kind?
I have seen hints that names of code blocks can be exported, but I have failed to find the exact syntax.
You could experiment with:
;; template used to export the body of code blocks
(setq org-babel-exp-code-template
;; (concat "\n=%name=:\n"
org-babel-exp-code-template)
;; )
)
Though, that's not as nice as the results of NuWeb / NoWeb; see http://lists.gnu.org/archive/html/emacs-orgmode/2009-12/msg00170.html for a comparison of them vs Org (though the PDF links aren't accessible anymore).
Org-mode has a great feature to include source code like this:
#+begin_src java -n
/**
* #param foo
*/
public static void doBar(Baz ba)
{
Collection<String> strings = ba.getStrings(true);
return strings;
}
#+end_src
The -n option shows line numbers.
There's a +n option to have the numbering continue from the last block.
Is there any option to set the starting number? This would be useful for source code snippets where you want the line numbers to correspond to the full file.
This has been added in Org Mode 9. It's now possible to pass numbers to both -n and +n.
So, for example, you could make the example start on line 12 by using the following header:
#+begin_src java -n 12
...
#+end_src
I agree with jco. A long answer to the question could read like the following. But except from the screenshot it's just copy and paste from the manual.
As documented in the Section Literal Examples you can add line numbers to the source blocks and example blocks:
Both in example and in src snippets, you can add a -n switch to the
end of the BEGIN line, to get the lines of the example numbered. The
-n takes an optional numeric argument specifying the starting line number of the block. If you use a +n switch, the numbering from the
previous numbered snippet will be continued in the current one. The +n
can also take a numeric argument. The value of the argument will be
added to the last line of the previous block to determine the starting
line number.
You can also refer to line numbers:
In literal examples, Org will interpret strings like (ref:name) as
labels, and use them as targets for special hyperlinks like [[(name)]]
(i.e., the reference name enclosed in single parenthesis). In HTML,
hovering the mouse over such a link will remote-highlight the
corresponding code line, which is kind of cool.
An example for demonstrating both features is
#+BEGIN_SRC emacs-lisp -n -r
(save-excursion (ref:sc)
(goto-char (point-min))) (ref:jump)
#+END_SRC
In line [[(sc)]] we remember the current position.
[[(jump)][Line (jump)]] jumps to point-min.
It would produce
This answer is out of date, see the other ones
No implemented options that I know of.
A very hacky solution I just tested would be to define yourself the counter org uses for +n to work.
It goes :
#+begin_src emacs-lisp :exports results
;; we need it not be a buffer-local value
(setq-default org-export-last-code-line-counter-value 42)
#+end_src
As the result of being a dirty hack, it only works once, though, for the very first block, but I'm not sure how ±n behaves .
It is evaluated (because of :exports results, without actually exporting anything when I tried) before numbering any other blocks, so it can be anywhere in your buffer.
I guess it wouldn't be very hard to implement, or that it could be done better by someone who actually knows elisp, but I hope it'll help.
I'm getting started with org-mode and there's something I'd like to do that seems like it should be possible, but I'm having trouble figuring out.
Let me describe the scenario: I have some SQL code that I want to execute on a remote server. I currently have a python script that takes SQL code as a string and does this for me. Without org-mode, my work flow would be to start with a file like so:
echo "SELECT name, grade FROM students" >> basic_query.sql
and then I'd run:
$ python run_query.py basic_query.sql
To do this is in the org-mode setting, I could create a code block for the SQL:
#+NAME: basic_query
#+BEGIN_SRC SQL
SELECT name, grade FROM students
#+END_SRC
And then I'd have a code block for the python calling function:
#+BEGIN_SRC python :export results
import sql_helper
query_status = sql_helper.run_query(<<basic_query>>)
#+END_SRC
Which I might use to create a table, process further, plot etc. Note the << >> thing is not right, obviously---it's just an abuse of notation to indicate what I'm trying to do.
If you have set up emacs/org-mode so that python code is enabled ((python . t) in org-babel-do-load-languages), you are almost there, I changed your example to
#+NAME: basic_query
#+BEGIN_SRC SQL
SELECT name, grade FROM students
#+END_SRC
#+BEGIN_SRC python :export results :noweb yes :tangle yes
import sql_helper
query = """
<<basic_query>>
"""
query_status = sql_helper.run_query(query)
#+END_SRC
My python is a little rusty, but at least if I tangle this to
import sql_helper
query = """
SELECT name, grade FROM students
"""
query_status = sql_helper.run_query(query)
python does no longer complain about the syntax, but about the missing module sql_helper...
When I do C-c C-e l to export an Org file to LaTeX it produces a document with a particular preamble. Instead of this particular preamble I would like it to use a preamble of my choice. Say that I want it to use the following preamble:
% Don't forget to qpdf --linearize the final copy
\RequirePackage[l2tabu,orthodox]{nag}% Old habits die hard. All the same, there are commands, classes and packages which are outdated and superseded. nag provides routines to warn the user about the use of those.
\immediate\write18{sh ./vc}
\input{vc}% Version control macros (for \VCDateISO in \date) http://www.ctan.org/pkg/vc
\documentclass[a4paper,12pt]{article}% pt? doublepage?
%\usepackage{geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}% Latin Modern (derivate of Knuth's CM)
\usepackage{fixltx2e}% \textsubscript and bugfixes for LaTeX
\usepackage{microtype}
\usepackage[strict=true]{csquotes}% Context-sensistive quotes. \enquote "" \enquote* ''. Use the integrated commands \textcquote and \blockcquote rather than biblatex internal commands to get contex sensistive quotes for them too. s/babel/autostyle in new version.
\usepackage[bookmarks,pdfborder={0 0 0}]{hyperref}% links and pdfinfo. MUST BE LOADED LAST!
\hypersetup{% Setup for hyperref
pdftitle = {[Title from #+TITLE]},
pdfauthor = {[Author from #+AUTHOR]}
}
I know that you can manipulate which packages are used on a per file basis as described in the manual but I want this preamble to be used for all files unless ) specify otherwise. The preamble I want to use includes the following:
deactivated packages (such as geometry above)
packages loaded by RequirePackage
input macros
\immediate\write18 macros
comments after usepackage macros
a hypersetup macro that recognizes #+TITLE and #+AUTHOR from Org-mode files
Deactivated packages (such as geometry above)
Org-mode recognizes LaTeX syntax inside LaTeX code-blocks, as well as when including LaTeX files in the content. (See Quoting LaTeX code.)
Packages loaded by RequirePackage
As above.
Input macros
As above.
\immediate\write18 macros
I believe this should also be as above, however there is an alternate method of dealing with this. If you create a source code block of type sh with the command within it, Org will evaluate it on export and produce the desired behaviour. You have to enable sh as a babel language type for it to work however.
(require 'ob-shell)
You can also include sh as one of the languages loaded by babel by adding it to org-babel-load-languages
(acons 'sh 't org-babel-load-languages)
Then use a code block similar to the following to run your ./vc
#+name: Test
#+begin_src sh :results output silent :exports results
./vc
#+end_src
As long as this comes before your \input{vc} line it should run the code and then include it. Simply follow the code-block with
#+LATEX: \input{vc}
And your content should be included.
Comments after usepackage macros
If the code is within a LaTeX block it should recognize it as LaTeX.
A hypersetup macro that recognizes #+TITLE and #+AUTHOR from Org-mode files.
This will have to be included within each document rather than separate. The following will provide what you desire for your macros. It will not be within the preamble, however it will end up at the top of the document and the export does behave as expected (however it will not behave as expected if added through #+INCLUDE: from org.
#+begin_latex
\hypersetup{% Setup for hyperref
pdftitle = {{{{TITLE}}}}, %Org macro to take from #+TITLE
pdfauthor = {{{{AUTHOR}}}} %Org macro to take from #+AUTHOR
}
#+end_latex
Creating your own Latex export class
If you follow the instructions in the worg tutorials (See Org Latex Export) you can create your own export-class. If you want to have full control over the packages in the preamble you would simply need to:
(add-to-list 'org-export-latex-classes
'("<CLASS NAME>"
"\\documentclass{article}
[NO-DEFAULT-PACKAGES]
[NO-PACKAGES]"
<insert desired sectioning configuration>))
You can also add in your desired packages between the \\documentclass and [NO-DEFAULT-PACKAGES] lines. The alternative would be to add them to the file itself using:
#+LATEX_CLASS: <CLASS NAME>
#+LATEX_HEADER: \usepackage{package}
...
As a third option, you can simply create a custom .sty file with the desired packages etc and include it as a single #+LATEX_HEADER:.
This doesn't answer your question, but it does allow you to do what you want.
(defun headless-latex ()
"exports to a .tex file without any preamble"
(interactive)
(org-export-as-latex 3 nil nil nil t nil)
)
This function exports the content of your ORG-mode file without any preamble. You can then \input it into a file with your desired preamble. Further reading.
I use a different method to get things done :
Define a class (I call it per-file-class for some strange reason. You can call it something else). Put this code in your .emacs :
;; per-file-class with minimal packages
(unless (find "per-file-class" org-export-latex-classes :key 'car
:test 'equal)
(add-to-list 'org-export-latex-classes
'("per-file-class"
"\\documentclass{article}
[NO-DEFAULT-PACKAGES]
[EXTRA]"
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")
("\\subparagraph{%s}" . "\\subparagraph*{%s}"))))
Use this class in your org file :
#+LaTeX_CLASS: per-file-class
#+LaTeX_CLASS_OPTIONS: [10pt, a4paper]
I'm not allowed to comment (bad reputation or something ;-)) so I will post an answer. A previous answer had a very nice snippet of code for a headless export function. That code needs updating for later versions of org:
(defun headless-latex ()
"exports to a .tex file without any preamble"
(interactive)
(org-latex-export-as-latex nil nil nil t nil)
)