org-e-latex.el 77 KB

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