org-e-latex.el 100 KB

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