org-latex.el 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839
  1. ;;; org-latex.el --- LaTeX Back-End For Org Export Engine
  2. ;; Copyright (C) 2011 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; This library implements a LaTeX back-end for Org generic exporter.
  17. ;; It introduces three new buffer keywords: "LATEX_CLASS",
  18. ;; "LATEX_CLASS_OPTIONS" and "LATEX_HEADER".
  19. ;;; Code:
  20. (eval-when-compile (require 'cl))
  21. (require 'org-element)
  22. (require 'org-export)
  23. ;;; Internal Variables
  24. (defconst org-latex-option-alist
  25. '((:date "DATE" nil org-latex-date-format t)
  26. (:latex-class "LATEX_CLASS" nil org-latex-default-class t)
  27. (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
  28. (:latex-header-extra "LATEX_HEADER" nil nil newline))
  29. "Alist between LaTeX export properties and ways to set them.
  30. See `org-export-option-alist' for more information on the
  31. structure of the value.")
  32. ;;; User Configurable Variables
  33. (defgroup org-export-latex nil
  34. "Options for exporting Org mode files to LaTeX."
  35. :tag "Org Export LaTeX"
  36. :group 'org-export)
  37. ;;;; Preamble
  38. (defcustom org-latex-default-class "article"
  39. "The default LaTeX class."
  40. :group 'org-export-latex
  41. :type '(string :tag "LaTeX class"))
  42. (defcustom org-latex-classes
  43. '(("article"
  44. "\\documentclass[11pt]{article}"
  45. ("\\section{%s}" . "\\section*{%s}")
  46. ("\\subsection{%s}" . "\\subsection*{%s}")
  47. ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
  48. ("\\paragraph{%s}" . "\\paragraph*{%s}")
  49. ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
  50. ("report"
  51. "\\documentclass[11pt]{report}"
  52. ("\\part{%s}" . "\\part*{%s}")
  53. ("\\chapter{%s}" . "\\chapter*{%s}")
  54. ("\\section{%s}" . "\\section*{%s}")
  55. ("\\subsection{%s}" . "\\subsection*{%s}")
  56. ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
  57. ("book"
  58. "\\documentclass[11pt]{book}"
  59. ("\\part{%s}" . "\\part*{%s}")
  60. ("\\chapter{%s}" . "\\chapter*{%s}")
  61. ("\\section{%s}" . "\\section*{%s}")
  62. ("\\subsection{%s}" . "\\subsection*{%s}")
  63. ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
  64. "Alist of LaTeX classes and associated header and structure.
  65. If #+LaTeX_CLASS is set in the buffer, use its value and the
  66. associated information. Here is the structure of each cell:
  67. \(class-name
  68. header-string
  69. \(numbered-section . unnumbered-section\)
  70. ...\)
  71. The header string
  72. -----------------
  73. The HEADER-STRING is the header that will be inserted into the LaTeX file.
  74. It should contain the \\documentclass macro, and anything else that is needed
  75. for this setup. To this header, the following commands will be added:
  76. - Calls to \\usepackage for all packages mentioned in the variables
  77. `org-latex-default-packages-alist' and
  78. `org-latex-packages-alist'. Thus, your header definitions should
  79. avoid to also request these packages.
  80. - Lines specified via \"#+LaTeX_HEADER:\"
  81. If you need more control about the sequence in which the header is built
  82. up, or if you want to exclude one of these building blocks for a particular
  83. class, you can use the following macro-like placeholders.
  84. [DEFAULT-PACKAGES] \\usepackage statements for default packages
  85. [NO-DEFAULT-PACKAGES] do not include any of the default packages
  86. [PACKAGES] \\usepackage statements for packages
  87. [NO-PACKAGES] do not include the packages
  88. [EXTRA] the stuff from #+LaTeX_HEADER
  89. [NO-EXTRA] do not include #+LaTeX_HEADER stuff
  90. [BEAMER-HEADER-EXTRA] the beamer extra headers
  91. So a header like
  92. \\documentclass{article}
  93. [NO-DEFAULT-PACKAGES]
  94. [EXTRA]
  95. \\providecommand{\\alert}[1]{\\textbf{#1}}
  96. [PACKAGES]
  97. will omit the default packages, and will include the #+LaTeX_HEADER lines,
  98. then have a call to \\providecommand, and then place \\usepackage commands
  99. based on the content of `org-latex-packages-alist'.
  100. If your header or `org-latex-default-packages-alist' inserts
  101. \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be replaced with
  102. a coding system derived from `buffer-file-coding-system'. See also the
  103. variable `org-latex-inputenc-alist' for a way to influence this
  104. mechanism.
  105. The sectioning structure
  106. ------------------------
  107. The sectioning structure of the class is given by the elements following
  108. the header string. For each sectioning level, a number of strings is
  109. specified. A %s formatter is mandatory in each section string and will
  110. be replaced by the title of the section.
  111. Instead of a cons cell \(numbered . unnumbered\), you can also
  112. provide a list of 2 or 4 elements,
  113. \(numbered-open numbered-close\)
  114. or
  115. \(numbered-open numbered-close unnumbered-open unnumbered-close\)
  116. providing opening and closing strings for a LaTeX environment that should
  117. represent the document section. The opening clause should have a %s
  118. to represent the section title.
  119. Instead of a list of sectioning commands, you can also specify a
  120. function name. That function will be called with two parameters,
  121. the (reduced) level of the headline, and a predicate non-nil when
  122. the headline should be numbered. It must return a format string in
  123. which the section title will be added."
  124. :group 'org-export-latex
  125. :type '(repeat
  126. (list (string :tag "LaTeX class")
  127. (string :tag "LaTeX header")
  128. (repeat :tag "Levels" :inline t
  129. (choice
  130. (cons :tag "Heading"
  131. (string :tag " numbered")
  132. (string :tag "unnumbered"))
  133. (list :tag "Environment"
  134. (string :tag "Opening (numbered)")
  135. (string :tag "Closing (numbered)")
  136. (string :tag "Opening (unnumbered)")
  137. (string :tag "Closing (unnumbered)"))
  138. (function :tag "Hook computing sectioning"))))))
  139. (defcustom org-latex-inputenc-alist nil
  140. "Alist of inputenc coding system names, and what should really be used.
  141. For example, adding an entry
  142. (\"utf8\" . \"utf8x\")
  143. will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
  144. are written as utf8 files."
  145. :group 'org-export-latex
  146. :type '(repeat
  147. (cons
  148. (string :tag "Derived from buffer")
  149. (string :tag "Use this instead"))))
  150. (defcustom org-latex-date-format
  151. "\\today"
  152. "Format string for \\date{...}."
  153. :group 'org-export-latex
  154. :type 'boolean)
  155. (defcustom org-latex-title-command "\\maketitle"
  156. "The command used to insert the title just after \\begin{document}.
  157. If this string contains the formatting specification \"%s\" then
  158. it will be used as a formatting string, passing the title as an
  159. argument."
  160. :group 'org-export-latex
  161. :type 'string)
  162. ;;;; Headline
  163. (defcustom org-latex-format-headline-function nil
  164. "Function to format headline text.
  165. This function will be called with 5 arguments:
  166. TODO the todo keyword \(string or nil\).
  167. TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
  168. PRIORITY the priority of the headline \(integer or nil\)
  169. TEXT the main headline text \(string\).
  170. TAGS the tags string, separated with colons \(string or nil\).
  171. The function result will be used in the section format string.
  172. As an example, one could set the variable to the following, in
  173. order to reproduce the default set-up:
  174. \(defun org-latex-format-headline-default \(todo todo-type priority text tags\)
  175. \"Default format function for an headline.\"
  176. \(concat \(when todo \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
  177. \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
  178. text
  179. \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
  180. :group 'org-export-latex
  181. :type 'function)
  182. ;;;; Emphasis
  183. (defcustom org-latex-emphasis-alist
  184. '(("*" . "\\textbf{%s}")
  185. ("/" . "\\emph{%s}")
  186. ("_" . "\\underline{%s}")
  187. ("+" . "\\st{%s}")
  188. ("=" . protectedtexttt)
  189. ("~" . verb))
  190. "Alist of LaTeX expressions to convert emphasis fontifiers.
  191. The key is the character used as a marker for fontification. The
  192. value is a formatting string to wrap fontified text with.
  193. Value can also be set to the following symbols: `verb' and
  194. `protectedtexttt'. For the former, Org will use \"\\verb\" to
  195. create a format string and select a delimiter character that
  196. isn't in the string. For the latter, Org will use \"\\texttt\"
  197. to typeset and try to protect special characters."
  198. :group 'org-export-latex
  199. :type 'alist)
  200. ;;;; Footnotes
  201. (defcustom org-latex-footnote-separator "\\textsuperscript{,}\\,"
  202. "Text used to separate footnotes."
  203. :group 'org-export-latex
  204. :type 'string)
  205. ;;;; Time-stamps
  206. (defcustom org-latex-active-timestamp-format "\\textit{%s}"
  207. "A printf format string to be applied to active time-stamps."
  208. :group 'org-export-latex
  209. :type 'string)
  210. (defcustom org-latex-inactive-timestamp-format "\\textit{%s}"
  211. "A printf format string to be applied to inactive time-stamps."
  212. :group 'org-export-latex
  213. :type 'string)
  214. (defcustom org-latex-diary-timestamp-format "\\textit{%s}"
  215. "A printf format string to be applied to diary time-stamps."
  216. :group 'org-export-latex
  217. :type 'string)
  218. ;;;; Links
  219. (defcustom org-latex-image-default-option "width=.9\\linewidth"
  220. "Default option for images."
  221. :group 'org-export-latex
  222. :type 'string)
  223. (defcustom org-latex-default-figure-position "htb"
  224. "Default position for latex figures."
  225. :group 'org-export-latex
  226. :type 'string)
  227. (defcustom org-latex-inline-image-extensions
  228. '("pdf" "jpeg" "jpg" "png" "ps" "eps")
  229. "Extensions of image files that can be inlined into LaTeX.
  230. Note that the image extension *actually* allowed depend on the
  231. way the LaTeX file is processed. When used with pdflatex, pdf,
  232. jpg and png images are OK. When processing through dvi to
  233. Postscript, only ps and eps are allowed. The default we use here
  234. encompasses both."
  235. :group 'org-export-latex
  236. :type '(repeat (string :tag "Extension")))
  237. ;;;; Tables
  238. (defcustom org-latex-default-table-environment "tabular"
  239. "Default environment used to build tables."
  240. :group 'org-export-latex
  241. :type 'string)
  242. (defcustom org-latex-tables-centered t
  243. "When non-nil, tables are exported in a center environment."
  244. :group 'org-export-latex
  245. :type 'boolean)
  246. (defcustom org-latex-tables-verbatim nil
  247. "When non-nil, tables are exported verbatim."
  248. :group 'org-export-latex
  249. :type 'boolean)
  250. (defcustom org-latex-table-caption-above t
  251. "When non-nil, place caption string at the beginning of the table.
  252. Otherwise, place it near the end."
  253. :group 'org-export-latex
  254. :type 'boolean)
  255. ;;;; Drawers
  256. (defcustom org-latex-format-drawer-function nil
  257. "Function called to format a drawer in LaTeX code.
  258. The function must accept two parameters:
  259. NAME the drawer name, like \"LOGBOOK\"
  260. CONTENTS the contents of the drawer.
  261. The function should return the string to be exported.
  262. For example, the variable could be set to the following function
  263. in order to mimic default behaviour:
  264. \(defun org-latex-format-drawer-default \(name contents\)
  265. \"Format a drawer element for LaTeX export.\"
  266. contents\)"
  267. :group 'org-export-latex
  268. :type 'function)
  269. ;;;; Inlinetasks
  270. (defcustom org-latex-format-inlinetask-function nil
  271. "Function called to format an inlinetask in LaTeX code.
  272. The function must accept six parameters:
  273. TODO the todo keyword, as a string
  274. TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
  275. PRIORITY the inlinetask priority, as a string
  276. NAME the inlinetask name, as a string.
  277. TAGS the inlinetask tags, as a string.
  278. CONTENTS the contents of the inlinetask, as a string.
  279. The function should return the string to be exported.
  280. For example, the variable could be set to the following function
  281. in order to mimic default behaviour:
  282. \(defun org-latex-format-inlinetask-default \(todo type priority name tags contents\)
  283. \"Format an inline task element for LaTeX export.\"
  284. \(let \(\(full-title
  285. \(concat
  286. \(when todo \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
  287. \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
  288. title
  289. \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
  290. \(format \(concat \"\\\\begin{center}\\n\"
  291. \"\\\\fbox{\\n\"
  292. \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
  293. \"%s\\n\\n\"
  294. \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
  295. \"%s\"
  296. \"\\\\end{minipage}}\"
  297. \"\\\\end{center}\"\)
  298. full-title contents\)\)"
  299. :group 'org-export-latex
  300. :type 'function)
  301. ;; Src blocks
  302. (defcustom org-latex-listings nil
  303. "Non-nil means export source code using the listings package.
  304. This package will fontify source code, possibly even with color.
  305. If you want to use this, you also need to make LaTeX use the
  306. listings package, and if you want to have color, the color
  307. package. Just add these to `org-latex-packages-alist',
  308. for example using customize, or with something like
  309. (require 'org-latex)
  310. (add-to-list 'org-export-latex-packages-alist '(\"\" \"listings\"))
  311. (add-to-list 'org-export-latex-packages-alist '(\"\" \"color\"))
  312. Alternatively,
  313. (setq org-latex-listings 'minted)
  314. causes source code to be exported using the minted package as
  315. opposed to listings. If you want to use minted, you need to add
  316. the minted package to `org-latex-packages-alist', for
  317. example using customize, or with
  318. (require 'org-latex)
  319. (add-to-list 'org-latex-packages-alist '(\"\" \"minted\"))
  320. In addition, it is necessary to install
  321. pygments (http://pygments.org), and to configure the variable
  322. `org-latex-to-pdf-process' so that the -shell-escape option is
  323. passed to pdflatex."
  324. :group 'org-export-latex
  325. :type '(choice
  326. (const :tag "Use listings" t)
  327. (const :tag "Use minted" 'minted)
  328. (const :tag "Export verbatim" nil)))
  329. (defcustom org-latex-listings-langs
  330. '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
  331. (c "C") (cc "C++")
  332. (fortran "fortran")
  333. (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
  334. (html "HTML") (xml "XML")
  335. (tex "TeX") (latex "TeX")
  336. (shell-script "bash")
  337. (gnuplot "Gnuplot")
  338. (ocaml "Caml") (caml "Caml")
  339. (sql "SQL") (sqlite "sql"))
  340. "Alist mapping languages to their listing language counterpart.
  341. The key is a symbol, the major mode symbol without the \"-mode\".
  342. The value is the string that should be inserted as the language parameter
  343. for the listings package. If the mode name and the listings name are
  344. the same, the language does not need an entry in this list - but it does not
  345. hurt if it is present."
  346. :group 'org-export-latex
  347. :type '(repeat
  348. (list
  349. (symbol :tag "Major mode ")
  350. (string :tag "Listings language"))))
  351. (defcustom org-latex-listings-options nil
  352. "Association list of options for the latex listings package.
  353. These options are supplied as a comma-separated list to the
  354. \\lstset command. Each element of the association list should be
  355. a list containing two strings: the name of the option, and the
  356. value. For example,
  357. (setq org-export-latex-listings-options
  358. '((\"basicstyle\" \"\\small\")
  359. (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
  360. will typeset the code in a small size font with underlined, bold
  361. black keywords.
  362. Note that the same options will be applied to blocks of all
  363. languages."
  364. :group 'org-export-latex
  365. :type '(repeat
  366. (list
  367. (string :tag "Listings option name ")
  368. (string :tag "Listings option value"))))
  369. (defcustom org-latex-minted-langs
  370. '((emacs-lisp "common-lisp")
  371. (cc "c++")
  372. (cperl "perl")
  373. (shell-script "bash")
  374. (caml "ocaml"))
  375. "Alist mapping languages to their minted language counterpart.
  376. The key is a symbol, the major mode symbol without the \"-mode\".
  377. The value is the string that should be inserted as the language parameter
  378. for the minted package. If the mode name and the listings name are
  379. the same, the language does not need an entry in this list - but it does not
  380. hurt if it is present.
  381. Note that minted uses all lower case for language identifiers,
  382. and that the full list of language identifiers can be obtained
  383. with:
  384. pygmentize -L lexers"
  385. :group 'org-export-latex
  386. :type '(repeat
  387. (list
  388. (symbol :tag "Major mode ")
  389. (string :tag "Minted language"))))
  390. (defcustom org-latex-minted-options nil
  391. "Association list of options for the latex minted package.
  392. These options are supplied within square brackets in
  393. \\begin{minted} environments. Each element of the alist should be
  394. a list containing two strings: the name of the option, and the
  395. value. For example,
  396. (setq org-export-latex-minted-options
  397. '((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
  398. will result in src blocks being exported with
  399. \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
  400. as the start of the minted environment. Note that the same
  401. options will be applied to blocks of all languages."
  402. :group 'org-export-latex
  403. :type '(repeat
  404. (list
  405. (string :tag "Minted option name ")
  406. (string :tag "Minted option value"))))
  407. (defvar org-latex-custom-lang-environments nil
  408. "Association list mapping languages to language-specific latex
  409. environments used during export of src blocks by the listings and
  410. minted latex packages. For example,
  411. (setq org-export-latex-custom-lang-environments
  412. '((python \"pythoncode\")))
  413. would have the effect that if org encounters begin_src python
  414. during latex export it will output
  415. \\begin{pythoncode}
  416. <src block body>
  417. \\end{pythoncode}")
  418. ;;;; Plain text
  419. (defcustom org-latex-quotes
  420. '(("fr" ("\\(\\s-\\|[[(]\\)\"" . "«~") ("\\(\\S-\\)\"" . "~»") ("\\(\\s-\\|(\\)'" . "'"))
  421. ("en" ("\\(\\s-\\|[[(]\\)\"" . "``") ("\\(\\S-\\)\"" . "''") ("\\(\\s-\\|(\\)'" . "`")))
  422. "Alist for quotes to use when converting english double-quotes.
  423. The CAR of each item in this alist is the language code.
  424. The CDR of each item in this alist is a list of three CONS:
  425. - the first CONS defines the opening quote;
  426. - the second CONS defines the closing quote;
  427. - the last CONS defines single quotes.
  428. For each item in a CONS, the first string is a regexp
  429. for allowed characters before/after the quote, the second
  430. string defines the replacement string for this quote."
  431. :group 'org-export-latex
  432. :type '(list
  433. (cons :tag "Opening quote"
  434. (string :tag "Regexp for char before")
  435. (string :tag "Replacement quote "))
  436. (cons :tag "Closing quote"
  437. (string :tag "Regexp for char after ")
  438. (string :tag "Replacement quote "))
  439. (cons :tag "Single quote"
  440. (string :tag "Regexp for char before")
  441. (string :tag "Replacement quote "))))
  442. ;;; Internal Functions
  443. (defun org-latex--caption/label-string (caption label info)
  444. "Return caption and label LaTeX string for floats.
  445. CAPTION is a secondary string \(a list of strings and Org
  446. objects\) and LABEL a string representing the label. INFO is
  447. a plist holding contextual information.
  448. If there's no caption nor label, return the empty string.
  449. For non-floats, see `org-latex--wrap-label'."
  450. (let ((caption-str (and caption
  451. (org-export-secondary-string
  452. caption 'latex info)))
  453. (label-str (if label (format "\\label{%s}" label) "")))
  454. (cond
  455. ((and (not caption-str) (not label)) "")
  456. ((not caption-str) (format "\\label{%s}\n" label))
  457. ;; Option caption format with short name.
  458. ((string-match "\\[\\([^][]*\\)\\]{\\([^{}]*\\)}" caption-str)
  459. (format "\\caption[%s]{%s%s}\n"
  460. (org-match-string-no-properties 1 caption-str)
  461. label-str
  462. (org-match-string-no-properties 2 caption-str)))
  463. ;; Standard caption format.
  464. (t (format "\\caption{%s%s}\n" label-str caption-str)))))
  465. (defun org-latex--guess-inputenc (header)
  466. "Set the coding system in inputenc to what the buffer is.
  467. HEADER is the LaTeX header string.
  468. Return the new header."
  469. (let* ((cs (or (ignore-errors
  470. (latexenc-coding-system-to-inputenc
  471. buffer-file-coding-system))
  472. "utf8")))
  473. (if (not cs)
  474. header
  475. ;; First translate if that is requested.
  476. (setq cs (or (cdr (assoc cs org-latex-inputenc-alist)) cs))
  477. ;; Then find the \usepackage statement and replace the option.
  478. (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
  479. cs header t nil 1))))
  480. (defun org-latex--find-verb-separator (s)
  481. "Return a character not used in string S.
  482. This is used to choose a separator for constructs like \\verb."
  483. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  484. (loop for c across ll
  485. when (not (string-match (regexp-quote (char-to-string c)) s))
  486. return (char-to-string c))))
  487. (defun org-latex--make-option-string (options)
  488. "Return a comma separated string of keywords and values.
  489. OPTIONS is an alist where the key is the options keyword as
  490. a string, and the value a list containing the keyword value, or
  491. nil."
  492. (mapconcat (lambda (pair)
  493. (concat (first pair)
  494. (when (> (length (second pair)) 0)
  495. (concat "=" (second pair)))))
  496. options
  497. ","))
  498. (defun org-latex--quotation-marks (text info)
  499. "Export quotation marks depending on language conventions."
  500. (mapc (lambda(l)
  501. (let ((start 0))
  502. (while (setq start (string-match (car l) text start))
  503. (let ((new-quote (concat (match-string 1 text) (cdr l))))
  504. (setq text (replace-match new-quote t t text))))))
  505. (cdr (or (assoc (plist-get info :language) org-latex-quotes)
  506. ;; Falls back on English.
  507. (assoc "en" org-latex-quotes))))
  508. text)
  509. (defun org-latex--wrap-label (element output)
  510. "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
  511. This function shouldn't be used for floats. See
  512. `org-latex--caption/label-string'."
  513. (let ((label (org-element-get-property :name element)))
  514. (if (or (not output) (not label) (string= output "") (string= label ""))
  515. output
  516. (concat (format "\\label{%s}\n" label) output))))
  517. ;;; Template
  518. (defun org-latex-template (contents info)
  519. "Return complete document string after LaTeX conversion.
  520. CONTENTS is the transcoded contents string. INFO is a plist
  521. holding export options."
  522. (let ((title (org-export-secondary-string
  523. (plist-get info :title) 'latex info)))
  524. (concat
  525. ;; 1. Time-stamp.
  526. (and (plist-get info :time-stamp-file)
  527. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  528. ;; 2. Document class and packages.
  529. (let ((class (plist-get info :latex-class))
  530. (class-options (plist-get info :latex-class-options)))
  531. (org-element-normalize-string
  532. (let* ((header (nth 1 (assoc class org-latex-classes)))
  533. (document-class-string
  534. (and (stringp header)
  535. (if class-options
  536. (replace-regexp-in-string
  537. "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)"
  538. class-options header t nil 1)
  539. header))))
  540. (org-latex--guess-inputenc
  541. (org-splice-latex-header
  542. document-class-string
  543. org-export-latex-default-packages-alist ; defined in org.el
  544. org-export-latex-packages-alist nil ; defined in org.el
  545. (plist-get info :latex-header-extra))))))
  546. ;; 3. Define alert if not yet defined.
  547. "\\providecommand{\\alert}[1]{\\textbf{#1}}\n"
  548. ;; 4. Author.
  549. (let ((author (and (plist-get info :with-author)
  550. (let ((auth (plist-get info :author)))
  551. (and auth (org-export-secondary-string
  552. auth 'latex info)))))
  553. (email (and (plist-get info :with-email)
  554. (org-export-secondary-string
  555. (plist-get info :email) 'latex info))))
  556. (cond ((and author email (not (string= "" email)))
  557. (format "\\author{%s\\thanks{%s}}\n" author email))
  558. (author (format "\\author{%s}\n" author))
  559. (t "\\author{}\n")))
  560. ;; 5. Date.
  561. (let ((date (plist-get info :date)))
  562. (and date (format "\\date{%s}\n" date)))
  563. ;; 6. Title
  564. (format "\\title{%s}\n" title)
  565. ;; 7. Hyperref options.
  566. (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
  567. (or (plist-get info :keywords) "")
  568. (or (plist-get info :description) "")
  569. (let ((creator-info (plist-get info :with-creator)))
  570. (cond
  571. ((not creator-info) "")
  572. ((eq creator-info 'comment) "")
  573. (t (plist-get info :creator)))))
  574. ;; 7. Document start.
  575. "\\begin{document}\n\n"
  576. ;; 8. Title command.
  577. (org-element-normalize-string
  578. (cond ((string= "" title) nil)
  579. ((not (stringp org-latex-title-command)) nil)
  580. ((string-match "\\(?:[^%]\\|^\\)%s"
  581. org-latex-title-command)
  582. (format org-latex-title-command title))
  583. (t org-latex-title-command)))
  584. ;; 9. Table of contents.
  585. (let ((depth (plist-get info :with-toc)))
  586. (when depth
  587. (concat (when (wholenump depth)
  588. (format "\\setcounter{tocdepth}{%d}\n" depth))
  589. "\\tableofcontents\n\\vspace*{1cm}\n\n")))
  590. ;; 10. Document's body.
  591. contents
  592. ;; 11. Creator.
  593. (let ((creator-info (plist-get info :with-creator)))
  594. (cond
  595. ((not creator-info))
  596. ((eq creator-info 'comment)
  597. (format "%% %s\n" (plist-get info :creator)))
  598. (t (concat (plist-get info :creator) "\n"))))
  599. ;; 12. Document end.
  600. "\\end{document}")))
  601. ;;; Transcode Functions
  602. ;;;; Block
  603. (defun org-latex-center-block (center-block contents info)
  604. "Transcode a CENTER-BLOCK element from Org to LaTeX.
  605. CONTENTS holds the contents of the block. INFO is a plist
  606. holding contextual information."
  607. (org-latex--wrap-label
  608. center-block
  609. (format "\\begin{center}\n%s\\end{center}" contents)))
  610. ;;;; Comment
  611. ;; Comments are ignored.
  612. ;;;; Comment Block
  613. ;; Comment Blocks are ignored.
  614. ;;;; Drawer
  615. (defun org-latex-drawer (drawer contents info)
  616. "Transcode a DRAWER element from Org to LaTeX.
  617. CONTENTS holds the contents of the block. INFO is a plist
  618. holding contextual information."
  619. (let* ((name (org-element-get-property :drawer-name drawer))
  620. (output (if (functionp org-latex-format-drawer-function)
  621. (funcall org-latex-format-drawer-function
  622. name contents)
  623. ;; If there's no user defined function: simply
  624. ;; display contents of the drawer.
  625. contents)))
  626. (org-latex--wrap-label drawer output)))
  627. ;;;; Dynamic Block
  628. (defun org-latex-dynamic-block (dynamic-block contents info)
  629. "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
  630. CONTENTS holds the contents of the block. INFO is a plist
  631. holding contextual information. See
  632. `org-export-data'."
  633. (org-latex--wrap-label dynamic-block contents))
  634. ;;;; Emphasis
  635. (defun org-latex-emphasis (emphasis contents info)
  636. "Transcode EMPHASIS from Org to LaTeX.
  637. CONTENTS is the contents of the emphasized text. INFO is a plist
  638. holding contextual information.."
  639. (format (cdr (assoc (org-element-get-property :marker emphasis)
  640. org-latex-emphasis-alist))
  641. contents))
  642. ;;;; Entity
  643. (defun org-latex-entity (entity contents info)
  644. "Transcode an ENTITY object from Org to LaTeX.
  645. CONTENTS are the definition itself. INFO is a plist holding
  646. contextual information."
  647. (let ((ent (org-element-get-property :latex entity)))
  648. (if (org-element-get-property :latex-math-p entity)
  649. (format "$%s$" ent)
  650. ent)))
  651. ;;;; Example Block
  652. (defun org-latex-example-block (example-block contents info)
  653. "Transcode a EXAMPLE-BLOCK element from Org to LaTeX.
  654. CONTENTS is nil. INFO is a plist holding contextual information."
  655. (let* ((options (or (org-element-get-property :options example-block) ""))
  656. (value (org-export-handle-code
  657. (org-element-get-property :value example-block) options info)))
  658. (org-latex--wrap-label example-block value)))
  659. ;;;; Export Snippet
  660. (defun org-latex-export-snippet (export-snippet contents info)
  661. "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
  662. CONTENTS is nil. INFO is a plist holding contextual information."
  663. (org-element-get-property :value export-snippet))
  664. ;;;; Export Block
  665. (defun org-latex-export-block (export-block contents info)
  666. "Transcode a EXPORT-BLOCK element from Org to LaTeX.
  667. CONTENTS is nil. INFO is a plist holding contextual information."
  668. (when (string= (org-element-get-property :type export-block) "latex")
  669. (org-remove-indentation (org-element-get-property :value export-block))))
  670. ;;;; Fixed Width
  671. (defun org-latex-fixed-width (fixed-width contents info)
  672. "Transcode a FIXED-WIDTH element from Org to LaTeX.
  673. CONTENTS is nil. INFO is a plist holding contextual information."
  674. (let* ((value (org-element-normalize-string
  675. (replace-regexp-in-string
  676. "^[ \t]*: ?" ""
  677. (org-element-get-property :value fixed-width)))))
  678. (org-latex--wrap-label
  679. fixed-width
  680. (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
  681. ;;;; Footnote Definition
  682. ;; Footnote Definitions are ignored.
  683. ;;;; Footnote Reference
  684. (defun org-latex-footnote-reference (footnote-reference contents info)
  685. "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
  686. CONTENTS is nil. INFO is a plist holding contextual information."
  687. (concat
  688. ;; Insert separator between two footnotes in a row.
  689. (when (eq (plist-get info :previous-object) 'footnote-reference)
  690. org-latex-footnote-separator)
  691. ;; Use \footnotemark if the footnote has already been defined.
  692. ;; Otherwise, define it with \footnote command.
  693. (let* ((all-seen (plist-get org-export-persistent-properties
  694. :seen-footnote-labels))
  695. (label (org-element-get-property :label footnote-reference))
  696. ;; Anonymous footnotes are always new footnotes.
  697. (seenp (and label (member label all-seen)))
  698. (inline-def-p (org-element-get-property
  699. :inline-definition footnote-reference)))
  700. (cond
  701. (seenp (format "\\footnotemark[%s]" (length seenp)))
  702. ;; Inline definitions are secondary strings.
  703. (inline-def-p
  704. (format "\\footnote{%s}"
  705. (org-trim
  706. (org-export-secondary-string inline-def-p 'latex info))))
  707. ;; Non-inline footnotes necessarily contain a label. Retrieve
  708. ;; match definition in `:footnotes-labels-alist'.
  709. (t
  710. (format "\\footnote{%s}"
  711. (org-trim
  712. (org-export-data
  713. (cdr (assoc label (plist-get info :footnotes-labels-alist)))
  714. 'latex info))))))))
  715. ;;;; Headline
  716. (defun org-latex-headline (headline contents info)
  717. "Transcode an HEADLINE element from Org to LaTeX.
  718. CONTENTS holds the contents of the headline. INFO is a plist
  719. holding contextual information."
  720. (let* ((class (plist-get info :latex-class))
  721. (numberedp (plist-get info :section-numbers))
  722. ;; Get level relative to current parsed data.
  723. (level (+ (org-element-get-property :level headline)
  724. (plist-get info :headline-offset)))
  725. (class-sectionning (assoc class org-latex-classes))
  726. ;; Section formatting will set two placeholders: one for the
  727. ;; title and the other for the contents.
  728. (section-fmt
  729. (let ((sec (if (and (symbolp (nth 2 class-sectionning))
  730. (fboundp (nth 2 class-sectionning)))
  731. (funcall (nth 2 class-sectionning) level numberedp)
  732. (nth (1+ level) class-sectionning))))
  733. (cond
  734. ;; No section available for that LEVEL.
  735. ((not sec) nil)
  736. ;; Section format directly returned by a function.
  737. ((stringp sec) sec)
  738. ;; (numbered-section . unnumbered-section)
  739. ((not (consp (cdr sec)))
  740. (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
  741. ;; (numbered-open numbered-close)
  742. ((= (length sec) 2)
  743. (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
  744. ;; (num-in num-out no-num-in no-num-out)
  745. ((= (length sec) 4)
  746. (if numberedp
  747. (concat (car sec) "\n%s" (nth 1 sec))
  748. (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
  749. (text (org-export-secondary-string
  750. (org-element-get-property :title headline) 'latex info))
  751. (todo (and (plist-get info :with-todo-keywords)
  752. (let ((todo (org-element-get-property
  753. :todo-keyword headline)))
  754. (and todo
  755. (org-export-secondary-string todo 'latex info)))))
  756. (todo-type (and todo (org-element-get-property :todo-type headline)))
  757. (tags (and (plist-get info :with-tags)
  758. (org-element-get-property :tags headline)))
  759. (priority (and (plist-get info :with-priority)
  760. (org-element-get-property :priority headline)))
  761. ;; Create the headline text.
  762. (full-text (if (functionp org-latex-format-headline-function)
  763. ;; User-defined formatting function.
  764. (funcall org-latex-format-headline-function
  765. todo todo-type priority text tags)
  766. ;; Default formatting.
  767. (concat
  768. (when todo
  769. (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  770. (when priority (format "\\framebox{\\#%c} " priority))
  771. text
  772. (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
  773. ;; Associate some \label to the headline for internal links.
  774. (headline-labels (mapconcat
  775. (lambda (p)
  776. (let ((val (org-element-get-property p headline)))
  777. (when val (format "\\label{%s}\n"
  778. (if (eq p :begin)
  779. (format "headline-%s" val)
  780. val)))))
  781. '(:custom-id :id :begin) ""))
  782. (pre-blanks (make-string (org-element-get-property :pre-blank headline)
  783. 10)))
  784. (cond
  785. ;; Case 1: This is a footnote section: ignore it.
  786. ((org-element-get-property :footnote-section-p headline) nil)
  787. ;; Case 2. This is a deep sub-tree: export it as a list item.
  788. ;; Also export as items headlines for which no section
  789. ;; format has been found.
  790. ((or (not section-fmt)
  791. (and (wholenump (plist-get info :headline-levels))
  792. (> level (plist-get info :headline-levels))))
  793. ;; Build the real contents of the sub-tree.
  794. (let ((low-level-body
  795. (concat
  796. ;; If the headline is the first sibling, start a list.
  797. (when (org-export-first-sibling-p headline info)
  798. (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
  799. ;; Itemize headline
  800. "\\item " full-text "\n" headline-labels pre-blanks contents)))
  801. ;; If headline in the last sibling, close the list, before any
  802. ;; blank line. Otherwise, simply return LOW-LEVEL-BODY.
  803. (if (org-export-last-sibling-p headline info)
  804. (replace-regexp-in-string
  805. "[ \t\n]*\\'"
  806. (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
  807. low-level-body)
  808. low-level-body)))
  809. ;; Case 3. Standard headline. Export it as a section.
  810. (t (format section-fmt full-text
  811. (concat headline-labels pre-blanks contents))))))
  812. ;;;; Horizontal Rule
  813. (defun org-latex-horizontal-rule (horizontal-rule contents info)
  814. "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
  815. CONTENTS is nil. INFO is a plist holding contextual information."
  816. (let ((attr (mapconcat #'identity
  817. (org-element-get-property :attr_latex horizontal-rule)
  818. " ")))
  819. (org-latex--wrap-label horizontal-rule (concat "\\hrule " attr))))
  820. ;;;; Inline Babel Call
  821. ;; Inline Babel Calls are ignored.
  822. ;;;; Inline Src Block
  823. (defun org-latex-inline-src-block (inline-src-block contents info)
  824. "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
  825. CONTENTS holds the contents of the item. INFO is a plist holding
  826. contextual information."
  827. (let* ((code (org-element-get-property :value inline-src-block))
  828. (separator (org-latex--find-verb-separator code)))
  829. (cond
  830. ;; Do not use a special package: transcode it verbatim.
  831. ((not org-latex-listings)
  832. (concat "\\verb" separator code separator))
  833. ;; Use minted package.
  834. ((eq org-latex-listings 'minted)
  835. (let* ((org-lang (org-element-get-property :language inline-src-block))
  836. (mint-lang (or (cadr (assq (intern org-lang)
  837. org-latex-minted-langs))
  838. org-lang))
  839. (options (org-latex--make-option-string
  840. org-latex-minted-options)))
  841. (concat (format "\\mint%s{%s}"
  842. (if (string= options "") "" (format "[%s]" options))
  843. mint-lang)
  844. separator code separator)))
  845. ;; Use listings package.
  846. (t
  847. ;; Maybe translate language's name.
  848. (let* ((org-lang (org-element-get-property :language inline-src-block))
  849. (lst-lang (or (cadr (assq (intern org-lang)
  850. org-latex-listings-langs))
  851. org-lang))
  852. (options (org-latex--make-option-string
  853. (append org-latex-listings-options
  854. `(("language" ,lst-lang))))))
  855. (concat (format "\\lstinline[%s]" options)
  856. separator code separator))))))
  857. ;;;; Inlinetask
  858. (defun org-latex-inlinetask (inlinetask contents info)
  859. "Transcode an INLINETASK element from Org to LaTeX.
  860. CONTENTS holds the contents of the block. INFO is a plist
  861. holding contextual information."
  862. (let ((title (org-export-secondary-string
  863. (org-element-get-property :title inlinetask) 'latex info))
  864. (todo (and (plist-get info :with-todo-keywords)
  865. (let ((todo (org-element-get-property
  866. :todo-keyword inlinetask)))
  867. (and todo
  868. (org-export-secondary-string todo 'latex info)))))
  869. (todo-type (org-element-get-property :todo-type inlinetask))
  870. (tags (and (plist-get info :with-tags)
  871. (org-element-get-property :tags inlinetask)))
  872. (priority (and (plist-get info :with-priority)
  873. (org-element-get-property :priority inlinetask))))
  874. ;; If `org-latex-format-inlinetask-function' is provided, call it
  875. ;; with appropriate arguments.
  876. (if (functionp org-latex-format-inlinetask-function)
  877. (funcall org-latex-format-inlinetask-function
  878. todo todo-type priority title tags contents)
  879. ;; Otherwise, use a default template.
  880. (org-latex--wrap-label
  881. inlinetask
  882. (let ((full-title
  883. (concat
  884. (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  885. (when priority (format "\\framebox{\\#%c} " priority))
  886. title
  887. (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
  888. (format (concat "\\begin{center}\n"
  889. "\\fbox{\n"
  890. "\\begin{minipage}[c]{.6\\textwidth}\n"
  891. "%s\n\n"
  892. "\\rule[.8em]{\\textwidth}{2pt}\n\n"
  893. "%s"
  894. "\\end{minipage}\n"
  895. "}\n"
  896. "\\end{center}")
  897. full-title contents))))))
  898. ;;;; Item
  899. (defun org-latex-item (item contents info)
  900. "Transcode an ITEM element from Org to LaTeX.
  901. CONTENTS holds the contents of the item. INFO is a plist holding
  902. contextual information."
  903. (let* ((level (plist-get (plist-get info :parent-properties) :level))
  904. (counter (let ((count (org-element-get-property :counter item)))
  905. (and count
  906. (< level 4)
  907. (format "\\setcounter{enum%s}{%s}\n"
  908. (nth level '("i" "ii" "iii" "iv"))
  909. (1- count)))))
  910. (checkbox (let ((checkbox (org-element-get-property :checkbox item)))
  911. (cond ((eq checkbox 'on) "$\\boxtimes$ ")
  912. ((eq checkbox 'off) "$\\Box$ ")
  913. ((eq checkbox 'trans) "$\\boxminus$ "))))
  914. (tag (let ((tag (org-element-get-property :tag item)))
  915. (and tag
  916. (format "[%s]" (org-export-secondary-string
  917. tag 'latex info))))))
  918. (concat counter "\\item" tag " " checkbox contents)))
  919. ;;;; Keyword
  920. (defun org-latex-keyword (keyword contents info)
  921. "Transcode a KEYWORD element from Org to LaTeX.
  922. CONTENTS is nil. INFO is a plist holding contextual information."
  923. (let ((key (downcase (org-element-get-property :key keyword)))
  924. (value (org-element-get-property :value keyword)))
  925. (cond
  926. ((string= key "latex") value)
  927. ((string= key "index") (format "\\index{%s}" value))
  928. ((string= key "target")
  929. (format "\\label{%s}" (org-export-solidify-link-text value)))
  930. ((string= key "toc")
  931. (let ((value (downcase value)))
  932. (cond
  933. ((string-match "\\<headlines\\>" value)
  934. (let ((depth (or (and (string-match "[0-9]+" value)
  935. (string-to-number (match-string 0 value)))
  936. (plist-get info :with-toc))))
  937. (concat
  938. (when (wholenump depth)
  939. (format "\\setcounter{tocdepth}{%s}\n" depth))
  940. "\\tableofcontents")))
  941. ((string= "tables" value) "\\listoftables")
  942. ((string= "figures" value) "\\listoffigures")
  943. ((string= "listings" value) "\\listoflistings"))))
  944. ((string= key "include")
  945. (org-export-included-file keyword 'latex info)))))
  946. ;;;; Latex Environment
  947. (defun org-latex-latex-environment (latex-environment contents info)
  948. "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
  949. CONTENTS is nil. INFO is a plist holding contextual information."
  950. (org-latex--wrap-label
  951. latex-environment
  952. (org-remove-indentation (org-element-get-property :value latex-environment))))
  953. ;;;; Latex Fragment
  954. (defun org-latex-latex-fragment (latex-fragment contents info)
  955. "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
  956. CONTENTS is nil. INFO is a plist holding contextual information."
  957. (org-element-get-property :value latex-fragment))
  958. ;;;; Line Break
  959. (defun org-latex-line-break (line-break contents info)
  960. "Transcode a LINE-BREAK object from Org to LaTeX.
  961. CONTENTS is nil. INFO is a plist holding contextual information."
  962. "\\\\")
  963. ;;;; Link
  964. (defun org-latex-link--inline-image (path info)
  965. "Return LaTeX code for an image at PATH.
  966. INFO is a plist containing export options."
  967. (let* ((parent-props (plist-get info :parent-properties))
  968. (caption (org-latex--caption/label-string
  969. (plist-get parent-props :caption)
  970. (plist-get parent-props :name)
  971. info))
  972. ;; Retrieve latex attributes from the element around.
  973. (attr (let ((raw-attr
  974. (mapconcat #'identity
  975. (plist-get parent-props :attr_latex) " ")))
  976. (unless (string= raw-attr "") raw-attr)))
  977. (disposition
  978. (cond
  979. ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
  980. ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
  981. ((or (and attr (string-match "\\<float\\>" attr))
  982. (not (string= caption "")))
  983. 'float)))
  984. (placement
  985. (cond
  986. ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
  987. (org-match-string-no-properties 1 attr))
  988. ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
  989. ((eq disposition 'float)
  990. (concat "[" org-latex-default-figure-position "]"))
  991. (t ""))))
  992. ;; Now clear ATTR from any special keyword and set a default
  993. ;; value if nothing is left.
  994. (if (not attr)
  995. (setq attr "")
  996. (while (string-match "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)"
  997. attr)
  998. (replace-match "" nil nil attr))
  999. (setq attr (org-trim attr)))
  1000. (setq attr (cond ((not (string= attr "")) attr)
  1001. ((eq disposition 'float) "width=0.7\\textwidth")
  1002. ((eq disposition 'wrap) "width=0.48\\textwidth")
  1003. (t (or org-latex-image-default-option ""))))
  1004. ;; Return proper string, depending on DISPOSITION.
  1005. (case disposition
  1006. ('wrap (format "\\begin{wrapfigure}%s
  1007. \\centering
  1008. \\includegraphics[%s]{%s}
  1009. %s\\end{wrapfigure}" placement attr path caption))
  1010. ('mulicolumn (format "\\begin{figure*}%s
  1011. \\centering
  1012. \\includegraphics[%s]{%s}
  1013. %s\\end{figure*}" placement attr path caption))
  1014. ('float (format "\\begin{figure}%s
  1015. \\centering
  1016. \\includegraphics[%s]{%s}
  1017. %s\\end{figure}" placement attr path caption))
  1018. (t (format "\\includegraphics[%s]{%s}" attr path)))))
  1019. (defun org-latex-link (link desc info)
  1020. "Transcode a LINK object from Org to LaTeX.
  1021. DESC is the description part of the link, or the empty string.
  1022. INFO is a plist holding contextual information. See
  1023. `org-export-data'."
  1024. (let* ((type (org-element-get-property :type link))
  1025. (raw-path (org-element-get-property :path link))
  1026. ;; Ensure DESC really exists, or set it to nil.
  1027. (desc (and (not (string= desc "")) desc))
  1028. (imagep (org-export-inline-image-p
  1029. link desc org-latex-inline-image-extensions))
  1030. (path (cond
  1031. ((member type '("http" "https" "ftp" "mailto"))
  1032. (concat type ":" raw-path))
  1033. ((and (not imagep) (string= type "file"))
  1034. (when (string-match "\\(.+\\)::.+" raw-path)
  1035. (setq raw-path (match-string 1 raw-path)))
  1036. (if (file-name-absolute-p raw-path)
  1037. (concat "file://" (expand-file-name raw-path))
  1038. ;; TODO: Not implemented yet. Concat also:
  1039. ;; (org-export-directory :LaTeX info)
  1040. (concat "file://" raw-path)))
  1041. (t raw-path)))
  1042. protocol)
  1043. (cond
  1044. ;; Image file.
  1045. (imagep (org-latex-link--inline-image path info))
  1046. ;; Id: for now, assume it's an internal link. TODO: do something
  1047. ;; to check if it isn't in the current file.
  1048. ((string= type "id")
  1049. (format "\\hyperref[%s]{%s}" path (or desc path)))
  1050. ;; Custom-id, target or radioed target: replace link with the
  1051. ;; normalized custom-id/target name.
  1052. ((member type '("custom-id" "target" "radio"))
  1053. (format "\\hyperref[%s]{%s}"
  1054. (org-export-solidify-link-text path)
  1055. (or desc (org-export-secondary-string path 'latex info))))
  1056. ;; Fuzzy: With the help of `org-export-resolve-fuzzy-link', find
  1057. ;; the destination of the link.
  1058. ((string= type "fuzzy")
  1059. (let ((destination (org-export-resolve-fuzzy-link link info)))
  1060. (cond
  1061. ;; Target match.
  1062. ((stringp destination)
  1063. (format "\\hyperref[%s]{%s}"
  1064. (org-export-solidify-link-text destination)
  1065. (or desc
  1066. (org-export-secondary-string
  1067. (org-element-get-property :raw-link link) 'latex info))))
  1068. ;; Headline match.
  1069. ((integerp destination)
  1070. (format "\\hyperref[headline-%d]{%s}"
  1071. destination
  1072. (or desc
  1073. (org-export-secondary-string
  1074. (org-element-get-property :raw-link link) 'latex info))))
  1075. ;; No match.
  1076. (t (format "\\texttt{%s}"
  1077. (or desc
  1078. (org-export-secondary-string
  1079. (org-element-get-property :raw-link link)
  1080. 'latex info)))))))
  1081. ;; Coderef: replace link with the reference name or the
  1082. ;; equivalent line number.
  1083. ((string= type "coderef")
  1084. (format (org-export-get-coderef-format path (or desc ""))
  1085. (cdr (assoc path (plist-get info :code-refs)))))
  1086. ;; Link type is handled by a special function.
  1087. ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
  1088. (funcall protocol (org-link-unescape path) desc 'latex))
  1089. ;; External link with a description part.
  1090. ((and path desc) (format "\\href{%s}{%s}" path desc))
  1091. ;; External link without a description part.
  1092. (path (format "\\url{%s}" path))
  1093. ;; No path, only description. Try to do something useful.
  1094. (t (format "\\texttt{%s}" desc)))))
  1095. ;;;; Babel Call
  1096. ;; Babel Calls are ignored.
  1097. ;;;; Macro
  1098. (defun org-latex-macro (macro contents info)
  1099. "Transcode a MACRO element from Org to LaTeX.
  1100. CONTENTS is nil. INFO is a plist holding contextual information."
  1101. ;; Use available tools.
  1102. (org-export-expand-macro macro info))
  1103. ;;;; Paragraph
  1104. (defun org-latex-paragraph (paragraph contents info)
  1105. "Transcode a PARAGRAPH element from Org to LaTeX.
  1106. CONTENTS is the contents of the paragraph, as a string. INFO is
  1107. the plist used as a communication channel."
  1108. contents)
  1109. ;;;; Plain List
  1110. (defun org-latex-plain-list (plain-list contents info)
  1111. "Transcode a PLAIN-LIST element from Org to LaTeX.
  1112. CONTENTS is the contents of the list. INFO is a plist holding
  1113. contextual information."
  1114. (let* ((type (org-element-get-property :type plain-list))
  1115. (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
  1116. "inparadesc" "asparadesc"))
  1117. (paralist-regexp (concat
  1118. "\\("
  1119. (mapconcat 'identity paralist-types "\\|")
  1120. "\\)"))
  1121. (attr (mapconcat #'identity
  1122. (org-element-get-property :attr_latex plain-list)
  1123. " "))
  1124. (latex-type (cond
  1125. ((and attr
  1126. (string-match
  1127. (format "\\<%s\\>" paralist-regexp) attr))
  1128. (match-string 1 attr))
  1129. ((eq type 'ordered) "enumerate")
  1130. ((eq type 'unordered) "itemize")
  1131. ((eq type 'descriptive) "description"))))
  1132. (org-latex--wrap-label
  1133. plain-list
  1134. (format "\\begin{%s}%s\n%s\\end{%s}"
  1135. latex-type
  1136. ;; Once special environment, if any, has been removed, the
  1137. ;; rest of the attributes will be optional arguments.
  1138. ;; They will be put inside square brackets if necessary.
  1139. (let ((opt (replace-regexp-in-string
  1140. (format " *%s *" paralist-regexp) "" attr)))
  1141. (cond ((string= opt "") "")
  1142. ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
  1143. (t (format "[%s]" opt))))
  1144. contents
  1145. latex-type))))
  1146. ;;;; Plain Text
  1147. (defun org-latex-plain-text (text info)
  1148. "Transcode a TEXT string from Org to LaTeX.
  1149. TEXT is the string to transcode. INFO is a plist holding
  1150. contextual information."
  1151. ;; Protect %, #, &, $, ~, ^, _, { and }.
  1152. (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
  1153. (setq text
  1154. (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
  1155. ;; Protect \
  1156. (setq text (replace-regexp-in-string
  1157. "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
  1158. "$\\backslash$" text nil t 1))
  1159. ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
  1160. (let ((case-fold-search nil)
  1161. (start 0))
  1162. (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
  1163. (setq text (replace-match
  1164. (format "\\%s{}" (match-string 1 text)) nil t text)
  1165. start (match-end 0))))
  1166. ;; Handle quotation marks
  1167. (setq text (org-latex--quotation-marks text info))
  1168. ;; Convert special strings.
  1169. (when (plist-get info :with-special-strings)
  1170. (while (string-match (regexp-quote "...") text)
  1171. (setq text (replace-match "\\ldots{}" nil t text))))
  1172. ;; Handle break preservation if required.
  1173. (when (plist-get info :preserve-breaks)
  1174. (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
  1175. text)))
  1176. ;; Return value.
  1177. text)
  1178. ;;;; Property Drawer
  1179. (defun org-latex-property-drawer (property-drawer contents info)
  1180. "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
  1181. CONTENTS is nil. INFO is a plist holding contextual
  1182. information."
  1183. ;; The property drawer isn't exported but we want separating blank
  1184. ;; lines nonetheless.
  1185. "")
  1186. ;;;; Quote Block
  1187. (defun org-latex-quote-block (quote-block contents info)
  1188. "Transcode a QUOTE-BLOCK element from Org to LaTeX.
  1189. CONTENTS holds the contents of the block. INFO is a plist
  1190. holding contextual information."
  1191. (org-latex--wrap-label
  1192. quote-block
  1193. (format "\\begin{quote}\n%s\\end{quote}" contents)))
  1194. ;;;; Quote Section
  1195. (defun org-latex-quote-section (quote-section contents info)
  1196. "Transcode a QUOTE-SECTION element from Org to LaTeX.
  1197. CONTENTS is nil. INFO is a plist holding contextual information."
  1198. (let ((value (org-remove-indentation
  1199. (org-element-get-property :value quote-section))))
  1200. (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
  1201. ;;;; Radio Target
  1202. (defun org-latex-radio-target (radio-target text info)
  1203. "Transcode a RADIO-TARGET object from Org to LaTeX.
  1204. TEXT is the text of the target. INFO is a plist holding
  1205. contextual information."
  1206. (format "\\label{%s}%s"
  1207. (org-export-solidify-link-text
  1208. (org-element-get-property :raw-value radio-target))
  1209. text))
  1210. ;;;; Special Block
  1211. (defun org-latex-special-block (special-block contents info)
  1212. "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
  1213. CONTENTS holds the contents of the block. INFO is a plist
  1214. holding contextual information."
  1215. (let ((type (downcase (org-element-get-property :type special-block))))
  1216. (org-latex--wrap-label
  1217. special-block
  1218. (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
  1219. ;;;; Src Block
  1220. (defun org-latex-src-block (src-block contents info)
  1221. "Transcode a SRC-BLOCK element from Org to LaTeX.
  1222. CONTENTS holds the contents of the item. INFO is a plist holding
  1223. contextual information."
  1224. (let* ((lang (org-element-get-property :language src-block))
  1225. (code (org-export-handle-code
  1226. (org-element-get-property :value src-block)
  1227. (org-element-get-property :switches src-block)
  1228. info lang))
  1229. (caption (org-element-get-property :caption src-block))
  1230. (label (org-element-get-property :name src-block))
  1231. (custom-env (and lang
  1232. (cadr (assq (intern lang)
  1233. org-latex-custom-lang-environments)))))
  1234. (cond
  1235. ;; No source fontification.
  1236. ((not org-latex-listings)
  1237. (let ((caption-str (org-latex--caption/label-string
  1238. caption label info))
  1239. (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
  1240. (format (or float-env "%s")
  1241. (concat
  1242. caption-str
  1243. (format "\\begin{verbatim}\n%s\\end{verbatim}" code)))))
  1244. ;; Custom environment.
  1245. (custom-env
  1246. (format "\\begin{%s}\n%s\\end{%s}\n" custom-env code custom-env))
  1247. ;; Use minted package.
  1248. ((eq org-latex-listings 'minted)
  1249. (let* ((mint-lang (or (cadr (assq (intern lang) org-latex-minted-langs))
  1250. lang))
  1251. (float-env (when (or label caption)
  1252. (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
  1253. (org-latex--caption/label-string
  1254. caption label info))))
  1255. (body (format "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
  1256. (org-latex--make-option-string
  1257. org-latex-minted-options)
  1258. mint-lang code)))
  1259. (if float-env (format float-env body) body)))
  1260. ;; Use listings package.
  1261. (t
  1262. (let ((lst-lang (or (cadr (assq (intern lang) org-latex-listings-langs))
  1263. lang))
  1264. (caption-str (and caption
  1265. (org-export-secondary-string
  1266. (org-element-get-property :caption src-block)
  1267. 'latex info))))
  1268. (concat (format "\\lstset{%s}\n"
  1269. (org-latex--make-option-string
  1270. (append org-latex-listings-options
  1271. `(("language" ,lst-lang))
  1272. (when label `(("label" ,label)))
  1273. (when caption-str
  1274. `(("caption" ,caption-str))))))
  1275. (format "\\begin{lstlisting}\n%s\\end{lstlisting}" code)))))))
  1276. ;;;; Statistics Cookie
  1277. (defun org-latex-statistics-cookie (statistics-cookie contents info)
  1278. "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
  1279. CONTENTS is nil. INFO is a plist holding contextual information."
  1280. (org-element-get-property :value statistics-cookie))
  1281. ;;;; Subscript
  1282. (defun org-latex-subscript (subscript contents info)
  1283. "Transcode a SUBSCRIPT object from Org to LaTeX.
  1284. CONTENTS is the contents of the object. INFO is a plist holding
  1285. contextual information."
  1286. (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents))
  1287. ;;;; Superscript
  1288. (defun org-latex-superscript (superscript contents info)
  1289. "Transcode a SUPERSCRIPT object from Org to LaTeX.
  1290. CONTENTS is the contents of the object. INFO is a plist holding
  1291. contextual information."
  1292. (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents))
  1293. ;;;; Table
  1294. (defun org-latex-table--format-string (table info)
  1295. "Return an appropriate format string for TABLE.
  1296. INFO is the plist containing format info about the table, as
  1297. returned by `org-export-table-format-info'.
  1298. The format string one placeholder for the body of the table."
  1299. (let* ((label (org-element-get-property :name table))
  1300. (caption (org-latex--caption/label-string
  1301. (org-element-get-property :caption table) label info))
  1302. (attr (mapconcat #'identity
  1303. (org-element-get-property :attr_latex table)
  1304. " "))
  1305. ;; Determine alignment string.
  1306. (alignment (org-latex-table--align-string attr info))
  1307. ;; Determine environment for the table: longtable, tabular...
  1308. (table-env (cond
  1309. ((not attr) org-latex-default-table-environment)
  1310. ((string-match "\\<longtable\\>" attr) "longtable")
  1311. ((string-match "\\(tabular.\\)" attr)
  1312. (org-match-string-no-properties 1 attr))
  1313. (t org-latex-default-table-environment)))
  1314. ;; If table is a float, determine environment: table or table*.
  1315. (float-env (cond
  1316. ((string= "longtable" table-env) nil)
  1317. ((and attr
  1318. (or (string-match (regexp-quote "table*") attr)
  1319. (string-match "\\<multicolumn\\>" attr)))
  1320. "table*")
  1321. ((or (not (string= caption "")) label) "table")))
  1322. ;; Extract others display options.
  1323. (width (and attr
  1324. (string-match "\\<width=\\(\\S-+\\)" attr)
  1325. (org-match-string-no-properties 1 attr)))
  1326. (placement (if (and attr
  1327. (string-match "\\<placement=\\(\\S-+\\)" attr))
  1328. (org-match-string-no-properties 1 attr)
  1329. (concat "["
  1330. org-latex-default-figure-position
  1331. "]"))))
  1332. ;; Prepare the final format string for the table.
  1333. (cond
  1334. ;; Longtable.
  1335. ((string= "longtable" table-env)
  1336. (format "\\begin{longtable}{%s}\n%s\n%%s\n%s\\end{longtable}"
  1337. alignment
  1338. (if (or (not org-latex-table-caption-above)
  1339. (string= "" caption))
  1340. ""
  1341. (concat (org-trim caption) "\\\\"))
  1342. (if (or org-latex-table-caption-above
  1343. (string= "" caption))
  1344. ""
  1345. (concat (org-trim caption) "\\\\\n"))))
  1346. ;; Others.
  1347. (t (concat (when float-env
  1348. (concat
  1349. (format "\\begin{%s}%s\n" float-env placement)
  1350. (if org-latex-table-caption-above caption "")))
  1351. (when org-latex-tables-centered "\\begin{center}\n")
  1352. (format "\\begin{%s}%s{%s}\n%%s\n\\end{%s}"
  1353. table-env
  1354. (if width (format "{%s}" width) "")
  1355. alignment
  1356. table-env)
  1357. (when org-latex-tables-centered "\n\\end{center}")
  1358. (when float-env
  1359. (concat (if org-latex-table-caption-above "" caption)
  1360. (format "\n\\end{%s}" float-env))))))))
  1361. (defun org-latex-table--align-string (attr info)
  1362. "Return an appropriate LaTeX alignment string.
  1363. INFO is the plist containing format info about the table, as
  1364. returned by `org-export-table-format-info'."
  1365. (or (and attr
  1366. (string-match "\\<align=\\(\\S-+\\)" attr)
  1367. (match-string 1 attr))
  1368. (let* ((align (copy-sequence (plist-get info :alignment)))
  1369. (colgroups (copy-sequence (plist-get info :column-groups)))
  1370. (cols (length align))
  1371. (separators (make-vector (1+ cols) "")))
  1372. ;; Ignore the first column if it's special.
  1373. (when (plist-get info :special-column-p)
  1374. (aset align 0 "") (aset colgroups 0 nil))
  1375. (let ((col 0))
  1376. (mapc (lambda (el)
  1377. (let ((gr (aref colgroups col)))
  1378. (when (memq gr '(start start-end))
  1379. (aset separators col "|"))
  1380. (when (memq gr '(end start-end))
  1381. (aset separators (1+ col) "|")))
  1382. (incf col))
  1383. align))
  1384. ;; Build the LaTeX specific alignment string.
  1385. (loop for al across align
  1386. for sep across separators
  1387. concat (concat sep al) into output
  1388. finally return (concat output (aref separators cols))))))
  1389. (defun org-latex-table (table contents info)
  1390. "Transcode a TABLE element from Org to LaTeX.
  1391. CONTENTS is nil. INFO is a plist holding contextual information."
  1392. (let ((attr (mapconcat #'identity
  1393. (org-element-get-property :attr_latex table)
  1394. " "))
  1395. (raw-table (org-element-get-property :raw-table table)))
  1396. (cond
  1397. ;; Case 1: verbatim table.
  1398. ((or org-latex-tables-verbatim
  1399. (and attr (string-match "\\<verbatim\\>" attr)))
  1400. (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
  1401. (org-export-clean-table
  1402. raw-table
  1403. (plist-get (org-export-table-format-info raw-table)
  1404. :special-column-p))))
  1405. ;; Case 2: table.el table. Convert it using appropriate tools.
  1406. ((eq (org-element-get-property :type table) 'table.el)
  1407. (require 'table)
  1408. ;; Ensure "*org-export-table*" buffer is empty.
  1409. (and (get-buffer "*org-export-table*")
  1410. (kill-buffer (get-buffer "*org-export-table*")))
  1411. (let ((output (with-temp-buffer
  1412. (insert raw-table)
  1413. (goto-char 1)
  1414. (re-search-forward "^[ \t]*|[^|]" nil t)
  1415. (table-generate-source 'latex "*org-export-table*")
  1416. (with-current-buffer "*org-export-table*"
  1417. (org-trim (buffer-string))))))
  1418. (kill-buffer (get-buffer "*org-export-table*"))
  1419. ;; Remove left out comments.
  1420. (while (string-match "^%.*\n" output)
  1421. (setq output (replace-match "" t t output)))
  1422. ;; When the "rmlines" attribute is provided, remove all hlines
  1423. ;; but the the one separating heading from the table body.
  1424. (when (and attr (string-match "\\<rmlines\\>" attr))
  1425. (let ((n 0) (pos 0))
  1426. (while (and (< (length output) pos)
  1427. (setq pos (string-match "^\\\\hline\n?" output pos)))
  1428. (incf n)
  1429. (unless (= n 2) (setq output (replace-match "" nil nil output))))))
  1430. (if org-latex-tables-centered
  1431. (format "\\begin{center}\n%s\n\\end{center}" output)
  1432. output)))
  1433. ;; Case 3: Standard table.
  1434. (t (let* (
  1435. (info (org-export-table-format-info raw-table))
  1436. (clean-table (org-export-clean-table
  1437. raw-table (plist-get info :special-column-p)))
  1438. (columns-number (length (plist-get info :alignment))))
  1439. ;; Convert ROWS to send them to `orgtbl-to-latex'. In
  1440. ;; particular, send each cell to
  1441. ;; `org-element-parse-secondary-string' to expand any Org
  1442. ;; object within. Eventually, flesh the format string out with
  1443. ;; the table.
  1444. (format (org-latex-table--format-string table info)
  1445. (orgtbl-to-latex
  1446. (mapcar
  1447. (lambda (row)
  1448. (if (string-match org-table-hline-regexp row)
  1449. 'hline
  1450. (mapcar
  1451. (lambda (cell)
  1452. (org-export-secondary-string
  1453. (org-element-parse-secondary-string
  1454. cell
  1455. (cdr (assq 'table org-element-string-restrictions)))
  1456. 'latex info))
  1457. (org-split-string row "[ \t]*|[ \t]*"))))
  1458. (org-split-string clean-table "\n"))
  1459. `(:tstart nil :tend nil
  1460. ;; Longtable environment requires specific
  1461. ;; header line end.
  1462. :hlend ,(and attr
  1463. (string-match "\\<longtable\\>" attr)
  1464. (format "\\\\
  1465. \\hline
  1466. \\endhead
  1467. \\hline\\multicolumn{%d}{r}{Continued on next page}\\\\
  1468. \\endfoot
  1469. \\endlastfoot"
  1470. columns-number))))))))))
  1471. ;;;; Target
  1472. (defun org-latex-target (target text info)
  1473. "Transcode a TARGET object from Org to LaTeX.
  1474. TEXT is the text of the target. INFO is a plist holding
  1475. contextual information."
  1476. (format "\\label{%s}%s"
  1477. (org-export-solidify-link-text
  1478. (org-element-get-property :raw-value target))
  1479. text))
  1480. ;;;; Time-stamp
  1481. (defun org-latex-time-stamp (time-stamp contents info)
  1482. "Transcode a TIME-STAMP object from Org to LaTeX.
  1483. CONTENTS is nil. INFO is a plist holding contextual information."
  1484. (let ((value (org-element-get-property :value time-stamp))
  1485. (type (org-element-get-property :type time-stamp))
  1486. (appt-type (org-element-get-property :appt-type time-stamp)))
  1487. (concat (cond ((eq appt-type 'scheduled)
  1488. (format "\\textbf{\\textsc{%s}} " org-scheduled-string))
  1489. ((eq appt-type 'deadline)
  1490. (format "\\textbf{\\textsc{%s}} " org-deadline-string))
  1491. ((eq appt-type 'closed)
  1492. (format "\\textbf{\\textsc{%s}} " org-closed-string)))
  1493. (cond ((memq type '(active active-range))
  1494. (format org-latex-active-timestamp-format value))
  1495. ((memq type '(inactive inactive-range))
  1496. (format org-latex-inactive-timestamp-format value))
  1497. (t
  1498. (format org-latex-diary-timestamp-format value))))))
  1499. ;;;; Verbatim
  1500. (defun org-latex-verbatim (element contents info)
  1501. "Return verbatim text in LaTeX."
  1502. (let ((fmt (cdr (assoc (org-element-get-property :marker element)
  1503. org-latex-emphasis-alist)))
  1504. (value (org-element-get-property :value element)))
  1505. (cond
  1506. ;; Handle the `verb' special case.
  1507. ((eq 'verb fmt)
  1508. (let ((separator (org-latex--find-verb-separator value)))
  1509. (concat "\\verb" separator value separator)))
  1510. ;; Handle the `protectedtexttt' special case.
  1511. ((eq 'protectedtexttt fmt)
  1512. (let ((start 0)
  1513. (trans '(("\\" . "\\textbackslash{}")
  1514. ("~" . "\\textasciitilde{}")
  1515. ("^" . "\\textasciicircum{}")))
  1516. (rtn "")
  1517. char)
  1518. (while (string-match "[\\{}$%&_#~^]" value)
  1519. (setq char (match-string 0 value))
  1520. (if (> (match-beginning 0) 0)
  1521. (setq rtn (concat rtn (substring value 0 (match-beginning 0)))))
  1522. (setq value (substring value (1+ (match-beginning 0))))
  1523. (setq char (or (cdr (assoc char trans)) (concat "\\" char))
  1524. rtn (concat rtn char)))
  1525. (setq value (concat rtn value)
  1526. fmt "\\texttt{%s}")
  1527. (while (string-match "--" value)
  1528. (setq value (replace-match "-{}-" t t value)))
  1529. (format fmt value)))
  1530. ;; Else use format string.
  1531. (t (format fmt value)))))
  1532. ;;;; Verse Block
  1533. (defun org-latex-verse-block (verse-block contents info)
  1534. "Transcode a VERSE-BLOCK element from Org to LaTeX.
  1535. CONTENTS is nil. INFO is a plist holding contextual information."
  1536. (org-latex--wrap-label
  1537. verse-block
  1538. ;; In a verse environment, add a line break to each newline
  1539. ;; character and change each white space at beginning of a line
  1540. ;; into a space of 1 em. Also change each blank line with
  1541. ;; a vertical space of 1 em.
  1542. (progn
  1543. (setq contents (replace-regexp-in-string
  1544. "^ *\\\\\\\\$" "\\\\vspace*{1em}"
  1545. (replace-regexp-in-string
  1546. "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
  1547. (org-remove-indentation
  1548. (org-export-secondary-string
  1549. (org-element-get-property :value verse-block)
  1550. 'latex info)))))
  1551. (while (string-match "^[ \t]+" contents)
  1552. (let ((new-str (format "\\hspace*{%dem}"
  1553. (length (match-string 0 contents)))))
  1554. (setq contents (replace-match new-str nil t contents))))
  1555. (format "\\begin{verse}\n%s\\end{verse}" contents))))
  1556. (provide 'org-latex)
  1557. ;;; org-latex.el ends here