org-e-latex.el 75 KB

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