2020-06-16

Org Mode

1. Show subtree in separate buffer

Use org-tree-to-indirect-buffer command (C-c C-x b)

2. Remove paragraph indent while exporting to latex

#+begin_export latex
\setlength{\parindent}{0pt}
#+end_export

3. Set org babel header arg for all blocks in file

Write the following at start of the file to add header arguments :session *Python* :results output :tangle solution.py to all python source blocks in the file.

#+PROPERTY: header-args:python :session *Python* :results output :tangle solution.py

This works because org source blocks header can be specified in property drawers (See info manual org#Using Header Arguments). And #+PROPERTY: is just a way to specify properties for a file.

header-args property applies to all source blocks while header-args:prop applies to prog language source blocks.

4. Use svg image for html and pdf for LaTeX

#+MACRO: img (eval (format "[[file:%s.%s]]" $1 (if (org-export-derived-backend-p org-export-current-backend 'latex) "pdf" "svg")))

Add this line in the org file to create a macro. Then use it as follows (don't add the extension at the end):

{{{img(path/to/image)}}}

5. Source block without babel backend

When using source blocks that don't have a babel backend add the parameter :exports code to prevent org-mode from complaining about the backend being not found. Because even though the :eval no-export is present, org babel still tries to parse the params and do some stuff with the source block.

So instead of

#+begin_src json
  {
    "name" : "Bibek Panthi",
    "species" : "Homo sapiens"
  }
#+end_src

do

#+begin_src json :exports code
  {
    "name" : "Bibek Panthi",
    "species" : "Homo sapiens"
  }
#+end_src

6. Literate DevOps

https://howardism.org/Technical/Emacs/literate-devops.html

You can run org babel sh source blocks on remote systems using :dir parameter.

Consider the following:

#+begin_src sh :dir /etc :results raw :wrap verbatim
head passwd
#+end_src

This is same as:

#+begin_src sh :results raw :wrap verbatim
head /etc/passwd
#+end_src

The :dir parameter accepts anything that emacs accepts for opening files, including paths opened by tramp. For example, if you have a server server.com and you open some file by M-x find-file /ssh:server.com:/home/bpanthi977/test.org. Then in similar way, you can specify :dir parameter to work on a remote directory:

#+begin_src sh :dir /ssh:server.com:/etc :wrap verbatim :results raw
head passwd
#+end_src

#+RESULTS:
#+begin_verbatim
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
#+end_verbatim

7. Show headings in beamer

Add the following to the top of org file:

#+begin_export latex
\\AtBeginSection[]{
  \\begin{frame}
  \\vfill
  \\centering
  \\begin{beamercolorbox}[sep=8pt,center,shadow=true,rounded=true]{title}
    \\usebeamerfont{title}\\insertsectionhead\\par%
  \\end{beamercolorbox}
  \\vfill
  \\end{frame}
}
#+end_export

8. Add section break after each section

#+begin_export latex
\let\oldsection\section
\renewcommand\section{\clearpage\oldsection}

\clearpage
#+end_export

9. No section numbering in latex export

#+BEGIN_EXPORT latex
\let\oldsection\section
\renewcommand\section{\clearpage\oldsection*}

\clearpage
#+END_EXPORT

10. Colorful Listings

#+begin_export latex
\usepackage{listings}
\usepackage{color}
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}


\lstdefinestyle{mystyle}{
    backgroundcolor=\color{backcolour},
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\ttfamily\footnotesize,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    keepspaces=true,
    numbers=left,
    numbersep=5pt,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=2,
    upquote=true
}

\lstset{style=mystyle}
#+end_export

11. Agenda

12. Show image in org buffer but not in exports

For this you can wrap the image link/attachment inside a drawer. And then configure emacs to not export that drawer.

For example:

#+OPTIONS: d:(not "noexport" "logbook")

:noexport:
attachment:image.png
:end:

It can be configured globally by setting the value of org-export-with-drawers. It is a list of drawers to be exported or if the list starts with not, it the list of drawers to not export.

(setq org-export-with-drawers '(not "noexport" "logbook"))

The default value of org-export-with-drawers is (not "LOGBOOK") that's why in above examples we keep that value too and also specify noexport drawers to be not exported.

Alternatively, you can specify d:nil to prevent explorting any drawers.

13. Tips for writing latex

Use org-cdlatex-mode

From: https://karthinks.com/software/latex-input-for-impatient-scholars/#snippets-snap-easy

The goal is to write LaTeX (and especially math) in a way that mirrors our stream of thought, where our eyes, fingers and the cursor all glide over the clutter of the markup. The above described process isn't quite there yet, but it's close. In summary,

  • Turn on prettify-symbols-mode to enhance readability.
  • Enter symbols with grave (`) and modify text or macros with the quote (').
  • Use TAB to jump forward to the next “point of interest” when typing.
  • Insert environment and float templates with CDLaTeX's commands or YaS snippets. env <tab> for an arbitrary environment (with completion).
  • Use YaS' auto-expanding snippets (or abbrev-mode abbrevs) to type without breaking the flow. Generally useful, but especially handy for math operators.
  • Preview your work with preview- commands. preview-at-point is quite handy.
  • When everything else fails, use the slower but fully document aware AucTeX commands: LaTeX-environment (C-c C-e) and TeX-insert-macro (C-c C-m).

References

Backlinks


Found this interesting? Subscribe to new posts.
Any comments? Send an email.