org-e-latex.el 95 KB

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