org-e-latex.el 103 KB

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