org-e-latex.el 80 KB

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