org-e-latex.el 70 KB

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