org-e-latex.el 95 KB

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