org-e-latex.el 103 KB

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