org-e-latex.el 100 KB

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