org-e-latex.el 101 KB

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