org-e-latex.el 80 KB

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