org-e-latex.el 96 KB

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