[Tex/LaTex] Use of templates in org mode of emacs

emacsorg-modetemplates

How can I use following template in an org mode on latex. This will help me understand the org mode latex. Kindly explain the changes you make in the template so that I understand the org mode better (Teach me fishing!)
Regards

\documentclass{article}
\usepackage{eso-pic}
\usepackage{fancyhdr}
\usepackage{blindtext} % just for the example
\usepackage[headsep=3cm,top=5cm]{geometry}
\usepackage{datatool}
% create sample csv file
\begin{filecontents*}{test.csv}
Id,myauthor,mydate,myaddress
1,Sebastian,12.12.2012,XYZ road,XYZ city
2,Rose,12.12.2013,XYZ road,abc city
\end{filecontents*}
% load data
\DTLloaddb{mydata}{test.csv}
\AddToShipoutPicture{%
  \AtTextUpperLeft{%
    \makebox(420,75)[lt]{%
      \footnotesize%
      \begin{tabular}{@{}*{3}{p{4.5cm}}@{}}%
      \textbf{Author}\newline\myauthor&%
      \textbf{Date of birth}\newline\mydate&%
      \textbf{Address}\newline\myaddress%
      \end{tabular}%
}}}
\pagestyle{fancy}
\newcommand{\myauthor}{user34083}
\newcommand{\mydate}{December 26, 1997}
\newcommand{\myaddress}{26 Washington Ave., Manhattan, New York.\newline United States of America}
% Define a command that fetches data for the row with the ID
% given in the argument
\newcommand*{\fetchdata}[1]{%
% fetch the first matching row
   \dtlgetrowforvalue{mydata}{\dtlcolumnindex{mydata}{Id}}{#1}%
% Lookup the required values from this row
   \dtlgetentryfromcurrentrow{\myauthor}{\dtlcolumnindex{mydata}{myauthor}}%
   \dtlgetentryfromcurrentrow{\mydate}{\dtlcolumnindex{mydata}{mydate}}%
   \dtlgetentryfromcurrentrow{\myaddress}{\dtlcolumnindex{mydata}{myaddress}}%
}

% fetch data for Id=2
\fetchdata{2}

\begin{document}

\blinddocument % just for the example
\end{document}

Best Answer

I have only tested this in versions pre-8.0.
I would highly recommend to read the org-mode manual for LaTeX exporting capabilities. It is very good: org-export-latex.

org-mode for Emacs has the optional LaTeX customization by using this:

#+LaTeX_HEADER: \newcommand\myaddress{...}

You can put as many header information lines as you please which in turn can build your custom LaTeX file on the fly.

#+LaTeX_HEADER: \newcommand\myaddress{...}
#+LaTeX_HEADER: \newcommand\myauthor{...}

Note that you can also use the above command to add packages.

Classes

Choosing the class for your org-file determines the packages that is used by a certain setup in your .emacs file.
To choose the org-class you do:

#+LaTeX_CLASS: article

To change the settings of the org-class article you add this to your .emacs file (example):

(add-to-list 'org-export-latex-classes
        '("article"
          "\\documentclass[10pt,article,oneside]{memoir}"
          ("\\chapter{%s}" . "\\chapter*{%s}")
          ("\\section{%s}" . "\\section*{%s}")
          ("\\subsection{%s}" . "\\subsection*{%s}")       
          ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
          ("\\paragraph{%s}" . "\\paragraph*{%s}")
          ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
        )

You can also do:

(add-to-list 'org-export-latex-classes
         '("book"
           "\\documentclass[10pt]{memoir}"
           ("\\chapter{%s}" . "\\chapter*{%s}")
           ("\\section{%s}" . "\\section*{%s}")
           ("\\subsection{%s}" . "\\subsection*{%s}")       
           ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
           ("\\paragraph{%s}" . "\\paragraph*{%s}")
           ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
         )

which will let you do:

#+LaTeX_CLASS: article

Default packages for all classes

For customization of the default packages you use, you can do something like:

;; Default LaTeX export packages
(add-to-list 'org-export-latex-packages-alist '("" "amsmath"))

which will add the amsmath package to all your classes.

You can test with this small document and the above settings:

#+OPTIONS: TeX:t LaTeX:t
#+LaTeX_CLASS: article
#+LaTeX_HEADER: \usepackage{biblatex}
#+LaTeX_HEADER: \newcommand\myaddress{...}

* Hello
Hello to you
** Next time

* Not again

Which will give you something like this:

% Created 2013-12-18 Wed 10:42
\documentclass[10pt,article,oneside]{memoir}
\usepackage{amsmath}\usepackage{biblatex}
\newcommand\myaddress{...}

\title{test}
\author{Nick}
\date{18 December 2013}

\begin{document}

\maketitle