org-e-latex.el 81 KB

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