org-e-latex.el 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007
  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. ;; This library implements a LaTeX back-end for Org generic exporter.
  17. ;; To test it, run
  18. ;;
  19. ;; M-: (org-export-to-buffer 'e-latex "*Test e-LaTeX*") RET
  20. ;;
  21. ;; in an org-mode buffer then switch to the buffer to see the LaTeX
  22. ;; export. See contrib/lisp/org-export.el for more details on how
  23. ;; this exporter works.
  24. ;; It introduces three new buffer keywords: "LATEX_CLASS",
  25. ;; "LATEX_CLASS_OPTIONS" and "LATEX_HEADER".
  26. ;;; Code:
  27. (eval-when-compile (require 'cl))
  28. (require 'org-element)
  29. (require 'org-export)
  30. ;;; Internal Variables
  31. (defconst org-e-latex-option-alist
  32. '((:date "DATE" nil org-e-latex-date-format t)
  33. (:latex-class "LATEX_CLASS" nil org-e-latex-default-class t)
  34. (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
  35. (:latex-header-extra "LATEX_HEADER" nil nil newline))
  36. "Alist between LaTeX export properties and ways to set them.
  37. See `org-export-option-alist' for more information on the
  38. structure of the value.")
  39. ;;; User Configurable Variables
  40. (defgroup org-export-latex nil
  41. "Options for exporting Org mode files to LaTeX."
  42. :tag "Org Export LaTeX"
  43. :group 'org-export)
  44. ;;;; Preamble
  45. (defcustom org-e-latex-default-class "article"
  46. "The default LaTeX class."
  47. :group 'org-export-e-latex
  48. :type '(string :tag "LaTeX class"))
  49. (defcustom org-e-latex-classes
  50. '(("article"
  51. "\\documentclass[11pt]{article}"
  52. ("\\section{%s}" . "\\section*{%s}")
  53. ("\\subsection{%s}" . "\\subsection*{%s}")
  54. ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
  55. ("\\paragraph{%s}" . "\\paragraph*{%s}")
  56. ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
  57. ("report"
  58. "\\documentclass[11pt]{report}"
  59. ("\\part{%s}" . "\\part*{%s}")
  60. ("\\chapter{%s}" . "\\chapter*{%s}")
  61. ("\\section{%s}" . "\\section*{%s}")
  62. ("\\subsection{%s}" . "\\subsection*{%s}")
  63. ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
  64. ("book"
  65. "\\documentclass[11pt]{book}"
  66. ("\\part{%s}" . "\\part*{%s}")
  67. ("\\chapter{%s}" . "\\chapter*{%s}")
  68. ("\\section{%s}" . "\\section*{%s}")
  69. ("\\subsection{%s}" . "\\subsection*{%s}")
  70. ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
  71. "Alist of LaTeX classes and associated header and structure.
  72. If #+LaTeX_CLASS is set in the buffer, use its value and the
  73. associated information. Here is the structure of each cell:
  74. \(class-name
  75. header-string
  76. \(numbered-section . unnumbered-section\)
  77. ...\)
  78. The header string
  79. -----------------
  80. The HEADER-STRING is the header that will be inserted into the LaTeX file.
  81. It should contain the \\documentclass macro, and anything else that is needed
  82. for this setup. To this header, the following commands will be added:
  83. - Calls to \\usepackage for all packages mentioned in the variables
  84. `org-e-latex-default-packages-alist' and
  85. `org-e-latex-packages-alist'. Thus, your header definitions should
  86. avoid to also request these packages.
  87. - Lines specified via \"#+LaTeX_HEADER:\"
  88. If you need more control about the sequence in which the header is built
  89. up, or if you want to exclude one of these building blocks for a particular
  90. class, you can use the following macro-like placeholders.
  91. [DEFAULT-PACKAGES] \\usepackage statements for default packages
  92. [NO-DEFAULT-PACKAGES] do not include any of the default packages
  93. [PACKAGES] \\usepackage statements for packages
  94. [NO-PACKAGES] do not include the packages
  95. [EXTRA] the stuff from #+LaTeX_HEADER
  96. [NO-EXTRA] do not include #+LaTeX_HEADER stuff
  97. [BEAMER-HEADER-EXTRA] the beamer extra headers
  98. So a header like
  99. \\documentclass{article}
  100. [NO-DEFAULT-PACKAGES]
  101. [EXTRA]
  102. \\providecommand{\\alert}[1]{\\textbf{#1}}
  103. [PACKAGES]
  104. will omit the default packages, and will include the #+LaTeX_HEADER lines,
  105. then have a call to \\providecommand, and then place \\usepackage commands
  106. based on the content of `org-e-latex-packages-alist'.
  107. If your header or `org-e-latex-default-packages-alist' inserts
  108. \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be replaced with
  109. a coding system derived from `buffer-file-coding-system'. See also the
  110. variable `org-e-latex-inputenc-alist' for a way to influence this
  111. mechanism.
  112. The sectioning structure
  113. ------------------------
  114. The sectioning structure of the class is given by the elements following
  115. the header string. For each sectioning level, a number of strings is
  116. specified. A %s formatter is mandatory in each section string and will
  117. be replaced by the title of the section.
  118. Instead of a cons cell \(numbered . unnumbered\), you can also
  119. provide a list of 2 or 4 elements,
  120. \(numbered-open numbered-close\)
  121. or
  122. \(numbered-open numbered-close unnumbered-open unnumbered-close\)
  123. providing opening and closing strings for a LaTeX environment that should
  124. represent the document section. The opening clause should have a %s
  125. to represent the section title.
  126. Instead of a list of sectioning commands, you can also specify a
  127. function name. That function will be called with two parameters,
  128. the (reduced) level of the headline, and a predicate non-nil when
  129. the headline should be numbered. It must return a format string in
  130. which the section title will be added."
  131. :group 'org-export-e-latex
  132. :type '(repeat
  133. (list (string :tag "LaTeX class")
  134. (string :tag "LaTeX header")
  135. (repeat :tag "Levels" :inline t
  136. (choice
  137. (cons :tag "Heading"
  138. (string :tag " numbered")
  139. (string :tag "unnumbered"))
  140. (list :tag "Environment"
  141. (string :tag "Opening (numbered)")
  142. (string :tag "Closing (numbered)")
  143. (string :tag "Opening (unnumbered)")
  144. (string :tag "Closing (unnumbered)"))
  145. (function :tag "Hook computing sectioning"))))))
  146. (defcustom org-e-latex-inputenc-alist nil
  147. "Alist of inputenc coding system names, and what should really be used.
  148. For example, adding an entry
  149. (\"utf8\" . \"utf8x\")
  150. will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
  151. are written as utf8 files."
  152. :group 'org-export-e-latex
  153. :type '(repeat
  154. (cons
  155. (string :tag "Derived from buffer")
  156. (string :tag "Use this instead"))))
  157. (defcustom org-e-latex-date-format
  158. "\\today"
  159. "Format string for \\date{...}."
  160. :group 'org-export-e-latex
  161. :type 'boolean)
  162. (defcustom org-e-latex-title-command "\\maketitle"
  163. "The command used to insert the title just after \\begin{document}.
  164. If this string contains the formatting specification \"%s\" then
  165. it will be used as a formatting string, passing the title as an
  166. argument."
  167. :group 'org-export-e-latex
  168. :type 'string)
  169. ;;;; Headline
  170. (defcustom org-e-latex-format-headline-function nil
  171. "Function to format headline text.
  172. This function will be called with 5 arguments:
  173. TODO the todo keyword \(string or nil\).
  174. TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
  175. PRIORITY the priority of the headline \(integer or nil\)
  176. TEXT the main headline text \(string\).
  177. TAGS the tags string, separated with colons \(string or nil\).
  178. The function result will be used in the section format string.
  179. As an example, one could set the variable to the following, in
  180. order to reproduce the default set-up:
  181. \(defun org-e-latex-format-headline-default \(todo todo-type priority text tags\)
  182. \"Default format function for an headline.\"
  183. \(concat \(when todo \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
  184. \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
  185. text
  186. \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
  187. :group 'org-export-e-latex
  188. :type 'function)
  189. ;;;; Emphasis
  190. (defcustom org-e-latex-emphasis-alist
  191. '(("*" . "\\textbf{%s}")
  192. ("/" . "\\emph{%s}")
  193. ("_" . "\\underline{%s}")
  194. ("+" . "\\st{%s}")
  195. ("=" . protectedtexttt)
  196. ("~" . verb))
  197. "Alist of LaTeX expressions to convert emphasis fontifiers.
  198. The key is the character used as a marker for fontification. The
  199. value is a formatting string to wrap fontified text with.
  200. Value can also be set to the following symbols: `verb' and
  201. `protectedtexttt'. For the former, Org will use \"\\verb\" to
  202. create a format string and select a delimiter character that
  203. isn't in the string. For the latter, Org will use \"\\texttt\"
  204. to typeset and try to protect special characters."
  205. :group 'org-export-e-latex
  206. :type 'alist)
  207. ;;;; Footnotes
  208. (defcustom org-e-latex-footnote-separator "\\textsuperscript{,}\\,"
  209. "Text used to separate footnotes."
  210. :group 'org-export-e-latex
  211. :type 'string)
  212. ;;;; Time-stamps
  213. (defcustom org-e-latex-active-timestamp-format "\\textit{%s}"
  214. "A printf format string to be applied to active time-stamps."
  215. :group 'org-export-e-latex
  216. :type 'string)
  217. (defcustom org-e-latex-inactive-timestamp-format "\\textit{%s}"
  218. "A printf format string to be applied to inactive time-stamps."
  219. :group 'org-export-e-latex
  220. :type 'string)
  221. (defcustom org-e-latex-diary-timestamp-format "\\textit{%s}"
  222. "A printf format string to be applied to diary time-stamps."
  223. :group 'org-export-e-latex
  224. :type 'string)
  225. ;;;; Links
  226. (defcustom org-e-latex-image-default-option "width=.9\\linewidth"
  227. "Default option for images."
  228. :group 'org-export-e-latex
  229. :type 'string)
  230. (defcustom org-e-latex-default-figure-position "htb"
  231. "Default position for latex figures."
  232. :group 'org-export-e-latex
  233. :type 'string)
  234. (defcustom org-e-latex-inline-image-extensions
  235. '("pdf" "jpeg" "jpg" "png" "ps" "eps")
  236. "Extensions of image files that can be inlined into LaTeX.
  237. Note that the image extension *actually* allowed depend on the
  238. way the LaTeX file is processed. When used with pdflatex, pdf,
  239. jpg and png images are OK. When processing through dvi to
  240. Postscript, only ps and eps are allowed. The default we use here
  241. encompasses both."
  242. :group 'org-export-e-latex
  243. :type '(repeat (string :tag "Extension")))
  244. ;;;; Tables
  245. (defcustom org-e-latex-default-table-environment "tabular"
  246. "Default environment used to build tables."
  247. :group 'org-export-e-latex
  248. :type 'string)
  249. (defcustom org-e-latex-tables-centered t
  250. "When non-nil, tables are exported in a center environment."
  251. :group 'org-export-e-latex
  252. :type 'boolean)
  253. (defcustom org-e-latex-tables-verbatim nil
  254. "When non-nil, tables are exported verbatim."
  255. :group 'org-export-e-latex
  256. :type 'boolean)
  257. (defcustom org-e-latex-table-caption-above t
  258. "When non-nil, place caption string at the beginning of the table.
  259. Otherwise, place it near the end."
  260. :group 'org-export-e-latex
  261. :type 'boolean)
  262. ;;;; Drawers
  263. (defcustom org-e-latex-format-drawer-function nil
  264. "Function called to format a drawer in LaTeX code.
  265. The function must accept two parameters:
  266. NAME the drawer name, like \"LOGBOOK\"
  267. CONTENTS the contents of the drawer.
  268. The function should return the string to be exported.
  269. For example, the variable could be set to the following function
  270. in order to mimic default behaviour:
  271. \(defun org-e-latex-format-drawer-default \(name contents\)
  272. \"Format a drawer element for LaTeX export.\"
  273. contents\)"
  274. :group 'org-export-e-latex
  275. :type 'function)
  276. ;;;; Inlinetasks
  277. (defcustom org-e-latex-format-inlinetask-function nil
  278. "Function called to format an inlinetask in LaTeX code.
  279. The function must accept six parameters:
  280. TODO the todo keyword, as a string
  281. TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
  282. PRIORITY the inlinetask priority, as a string
  283. NAME the inlinetask name, as a string.
  284. TAGS the inlinetask tags, as a string.
  285. CONTENTS the contents of the inlinetask, as a string.
  286. The function should return the string to be exported.
  287. For example, the variable could be set to the following function
  288. in order to mimic default behaviour:
  289. \(defun org-e-latex-format-inlinetask-default \(todo type priority name tags contents\)
  290. \"Format an inline task element for LaTeX export.\"
  291. \(let \(\(full-title
  292. \(concat
  293. \(when todo \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
  294. \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
  295. title
  296. \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
  297. \(format \(concat \"\\\\begin{center}\\n\"
  298. \"\\\\fbox{\\n\"
  299. \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
  300. \"%s\\n\\n\"
  301. \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
  302. \"%s\"
  303. \"\\\\end{minipage}}\"
  304. \"\\\\end{center}\"\)
  305. full-title contents\)\)"
  306. :group 'org-export-e-latex
  307. :type 'function)
  308. ;; Src blocks
  309. (defcustom org-e-latex-listings nil
  310. "Non-nil means export source code using the listings package.
  311. This package will fontify source code, possibly even with color.
  312. If you want to use this, you also need to make LaTeX use the
  313. listings package, and if you want to have color, the color
  314. package. Just add these to `org-e-latex-packages-alist',
  315. for example using customize, or with something like
  316. (require 'org-e-latex)
  317. (add-to-list 'org-export-latex-packages-alist '(\"\" \"listings\"))
  318. (add-to-list 'org-export-latex-packages-alist '(\"\" \"color\"))
  319. Alternatively,
  320. (setq org-e-latex-listings 'minted)
  321. causes source code to be exported using the minted package as
  322. opposed to listings. If you want to use minted, you need to add
  323. the minted package to `org-e-latex-packages-alist', for
  324. example using customize, or with
  325. (require 'org-e-latex)
  326. (add-to-list 'org-e-latex-packages-alist '(\"\" \"minted\"))
  327. In addition, it is necessary to install
  328. pygments (http://pygments.org), and to configure the variable
  329. `org-e-latex-to-pdf-process' so that the -shell-escape option is
  330. passed to pdflatex."
  331. :group 'org-export-e-latex
  332. :type '(choice
  333. (const :tag "Use listings" t)
  334. (const :tag "Use minted" 'minted)
  335. (const :tag "Export verbatim" nil)))
  336. (defcustom org-e-latex-listings-langs
  337. '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
  338. (c "C") (cc "C++")
  339. (fortran "fortran")
  340. (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
  341. (html "HTML") (xml "XML")
  342. (tex "TeX") (latex "TeX")
  343. (shell-script "bash")
  344. (gnuplot "Gnuplot")
  345. (ocaml "Caml") (caml "Caml")
  346. (sql "SQL") (sqlite "sql"))
  347. "Alist mapping languages to their listing language counterpart.
  348. The key is a symbol, the major mode symbol without the \"-mode\".
  349. The value is the string that should be inserted as the language parameter
  350. for the listings package. If the mode name and the listings name are
  351. the same, the language does not need an entry in this list - but it does not
  352. hurt if it is present."
  353. :group 'org-export-e-latex
  354. :type '(repeat
  355. (list
  356. (symbol :tag "Major mode ")
  357. (string :tag "Listings language"))))
  358. (defcustom org-e-latex-listings-options nil
  359. "Association list of options for the latex listings package.
  360. These options are supplied as a comma-separated list to the
  361. \\lstset command. Each element of the association list should be
  362. a list containing two strings: the name of the option, and the
  363. value. For example,
  364. (setq org-export-latex-listings-options
  365. '((\"basicstyle\" \"\\small\")
  366. (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
  367. will typeset the code in a small size font with underlined, bold
  368. black keywords.
  369. Note that the same options will be applied to blocks of all
  370. languages."
  371. :group 'org-export-e-latex
  372. :type '(repeat
  373. (list
  374. (string :tag "Listings option name ")
  375. (string :tag "Listings option value"))))
  376. (defcustom org-e-latex-minted-langs
  377. '((emacs-lisp "common-lisp")
  378. (cc "c++")
  379. (cperl "perl")
  380. (shell-script "bash")
  381. (caml "ocaml"))
  382. "Alist mapping languages to their minted language counterpart.
  383. The key is a symbol, the major mode symbol without the \"-mode\".
  384. The value is the string that should be inserted as the language parameter
  385. for the minted package. If the mode name and the listings name are
  386. the same, the language does not need an entry in this list - but it does not
  387. hurt if it is present.
  388. Note that minted uses all lower case for language identifiers,
  389. and that the full list of language identifiers can be obtained
  390. with:
  391. pygmentize -L lexers"
  392. :group 'org-export-e-latex
  393. :type '(repeat
  394. (list
  395. (symbol :tag "Major mode ")
  396. (string :tag "Minted language"))))
  397. (defcustom org-e-latex-minted-options nil
  398. "Association list of options for the latex minted package.
  399. These options are supplied within square brackets in
  400. \\begin{minted} environments. Each element of the alist should be
  401. a list containing two strings: the name of the option, and the
  402. value. For example,
  403. (setq org-export-latex-minted-options
  404. '((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
  405. will result in src blocks being exported with
  406. \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
  407. as the start of the minted environment. Note that the same
  408. options will be applied to blocks of all languages."
  409. :group 'org-export-e-latex
  410. :type '(repeat
  411. (list
  412. (string :tag "Minted option name ")
  413. (string :tag "Minted option value"))))
  414. (defvar org-e-latex-custom-lang-environments nil
  415. "Association list mapping languages to language-specific latex
  416. environments used during export of src blocks by the listings and
  417. minted latex packages. For example,
  418. (setq org-export-latex-custom-lang-environments
  419. '((python \"pythoncode\")))
  420. would have the effect that if org encounters begin_src python
  421. during latex export it will output
  422. \\begin{pythoncode}
  423. <src block body>
  424. \\end{pythoncode}")
  425. ;;;; Plain text
  426. (defcustom org-e-latex-quotes
  427. '(("fr" ("\\(\\s-\\|[[(]\\)\"" . "«~") ("\\(\\S-\\)\"" . "~»") ("\\(\\s-\\|(\\)'" . "'"))
  428. ("en" ("\\(\\s-\\|[[(]\\)\"" . "``") ("\\(\\S-\\)\"" . "''") ("\\(\\s-\\|(\\)'" . "`")))
  429. "Alist for quotes to use when converting english double-quotes.
  430. The CAR of each item in this alist is the language code.
  431. The CDR of each item in this alist is a list of three CONS:
  432. - the first CONS defines the opening quote;
  433. - the second CONS defines the closing quote;
  434. - the last CONS defines single quotes.
  435. For each item in a CONS, the first string is a regexp
  436. for allowed characters before/after the quote, the second
  437. string defines the replacement string for this quote."
  438. :group 'org-export-e-latex
  439. :type '(list
  440. (cons :tag "Opening quote"
  441. (string :tag "Regexp for char before")
  442. (string :tag "Replacement quote "))
  443. (cons :tag "Closing quote"
  444. (string :tag "Regexp for char after ")
  445. (string :tag "Replacement quote "))
  446. (cons :tag "Single quote"
  447. (string :tag "Regexp for char before")
  448. (string :tag "Replacement quote "))))
  449. ;;;; Compilation
  450. (defcustom org-e-latex-pdf-process
  451. '("pdflatex -interaction nonstopmode -output-directory %o %f"
  452. "pdflatex -interaction nonstopmode -output-directory %o %f"
  453. "pdflatex -interaction nonstopmode -output-directory %o %f")
  454. "Commands to process a LaTeX file to a PDF file.
  455. This is a list of strings, each of them will be given to the shell
  456. as a command. %f in the command will be replaced by the full file name, %b
  457. by the file base name (i.e. without extension) and %o by the base directory
  458. of the file.
  459. The reason why this is a list is that it usually takes several runs of
  460. `pdflatex', maybe mixed with a call to `bibtex'. Org does not have a clever
  461. mechanism to detect which of these commands have to be run to get to a stable
  462. result, and it also does not do any error checking.
  463. By default, Org uses 3 runs of `pdflatex' to do the processing. If you
  464. have texi2dvi on your system and if that does not cause the infamous
  465. egrep/locale bug:
  466. http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
  467. then `texi2dvi' is the superior choice. Org does offer it as one
  468. of the customize options.
  469. Alternatively, this may be a Lisp function that does the processing, so you
  470. could use this to apply the machinery of AUCTeX or the Emacs LaTeX mode.
  471. This function should accept the file name as its single argument."
  472. :group 'org-export-pdf
  473. :type '(choice
  474. (repeat :tag "Shell command sequence"
  475. (string :tag "Shell command"))
  476. (const :tag "2 runs of pdflatex"
  477. ("pdflatex -interaction nonstopmode -output-directory %o %f"
  478. "pdflatex -interaction nonstopmode -output-directory %o %f"))
  479. (const :tag "3 runs of pdflatex"
  480. ("pdflatex -interaction nonstopmode -output-directory %o %f"
  481. "pdflatex -interaction nonstopmode -output-directory %o %f"
  482. "pdflatex -interaction nonstopmode -output-directory %o %f"))
  483. (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
  484. ("pdflatex -interaction nonstopmode -output-directory %o %f"
  485. "bibtex %b"
  486. "pdflatex -interaction nonstopmode -output-directory %o %f"
  487. "pdflatex -interaction nonstopmode -output-directory %o %f"))
  488. (const :tag "texi2dvi"
  489. ("texi2dvi -p -b -c -V %f"))
  490. (const :tag "rubber"
  491. ("rubber -d --into %o %f"))
  492. (function)))
  493. (defcustom org-e-latex-logfiles-extensions
  494. '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
  495. "The list of file extensions to consider as LaTeX logfiles."
  496. :group 'org-export-e-latex
  497. :type '(repeat (string :tag "Extension")))
  498. (defcustom org-e-latex-remove-logfiles t
  499. "Non-nil means remove the logfiles produced by PDF production.
  500. These are the .aux, .log, .out, and .toc files."
  501. :group 'org-export-e-latex
  502. :type 'boolean)
  503. ;;; Internal Functions
  504. (defun org-e-latex--caption/label-string (caption label info)
  505. "Return caption and label LaTeX string for floats.
  506. CAPTION is a secondary string \(a list of strings and Org
  507. objects\) and LABEL a string representing the label. INFO is
  508. a plist holding contextual information.
  509. If there's no caption nor label, return the empty string.
  510. For non-floats, see `org-e-latex--wrap-label'."
  511. (let ((caption-str (and caption
  512. (org-export-secondary-string
  513. caption 'e-latex info)))
  514. (label-str (if label (format "\\label{%s}" label) "")))
  515. (cond
  516. ((and (not caption-str) (not label)) "")
  517. ((not caption-str) (format "\\label{%s}\n" label))
  518. ;; Option caption format with short name.
  519. ((string-match "\\[\\([^][]*\\)\\]{\\([^{}]*\\)}" caption-str)
  520. (format "\\caption[%s]{%s%s}\n"
  521. (org-match-string-no-properties 1 caption-str)
  522. label-str
  523. (org-match-string-no-properties 2 caption-str)))
  524. ;; Standard caption format.
  525. (t (format "\\caption{%s%s}\n" label-str caption-str)))))
  526. (defun org-e-latex--guess-inputenc (header)
  527. "Set the coding system in inputenc to what the buffer is.
  528. HEADER is the LaTeX header string.
  529. Return the new header."
  530. (let* ((cs (or (ignore-errors
  531. (latexenc-coding-system-to-inputenc
  532. buffer-file-coding-system))
  533. "utf8")))
  534. (if (not cs)
  535. header
  536. ;; First translate if that is requested.
  537. (setq cs (or (cdr (assoc cs org-e-latex-inputenc-alist)) cs))
  538. ;; Then find the \usepackage statement and replace the option.
  539. (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
  540. cs header t nil 1))))
  541. (defun org-e-latex--find-verb-separator (s)
  542. "Return a character not used in string S.
  543. This is used to choose a separator for constructs like \\verb."
  544. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  545. (loop for c across ll
  546. when (not (string-match (regexp-quote (char-to-string c)) s))
  547. return (char-to-string c))))
  548. (defun org-e-latex--make-option-string (options)
  549. "Return a comma separated string of keywords and values.
  550. OPTIONS is an alist where the key is the options keyword as
  551. a string, and the value a list containing the keyword value, or
  552. nil."
  553. (mapconcat (lambda (pair)
  554. (concat (first pair)
  555. (when (> (length (second pair)) 0)
  556. (concat "=" (second pair)))))
  557. options
  558. ","))
  559. (defun org-e-latex--quotation-marks (text info)
  560. "Export quotation marks depending on language conventions."
  561. (mapc (lambda(l)
  562. (let ((start 0))
  563. (while (setq start (string-match (car l) text start))
  564. (let ((new-quote (concat (match-string 1 text) (cdr l))))
  565. (setq text (replace-match new-quote t t text))))))
  566. (cdr (or (assoc (plist-get info :language) org-e-latex-quotes)
  567. ;; Falls back on English.
  568. (assoc "en" org-e-latex-quotes))))
  569. text)
  570. (defun org-e-latex--wrap-label (element output)
  571. "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
  572. This function shouldn't be used for floats. See
  573. `org-e-latex--caption/label-string'."
  574. (let ((label (org-element-get-property :name element)))
  575. (if (or (not output) (not label) (string= output "") (string= label ""))
  576. output
  577. (concat (format "\\label{%s}\n" label) output))))
  578. ;;; Template
  579. (defun org-e-latex-template (contents info)
  580. "Return complete document string after LaTeX conversion.
  581. CONTENTS is the transcoded contents string. INFO is a plist
  582. holding export options."
  583. (let ((title (org-export-secondary-string
  584. (plist-get info :title) 'e-latex info)))
  585. (concat
  586. ;; 1. Time-stamp.
  587. (and (plist-get info :time-stamp-file)
  588. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  589. ;; 2. Document class and packages.
  590. (let ((class (plist-get info :latex-class))
  591. (class-options (plist-get info :latex-class-options)))
  592. (org-element-normalize-string
  593. (let* ((header (nth 1 (assoc class org-e-latex-classes)))
  594. (document-class-string
  595. (and (stringp header)
  596. (if class-options
  597. (replace-regexp-in-string
  598. "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)"
  599. class-options header t nil 1)
  600. header))))
  601. (org-e-latex--guess-inputenc
  602. (org-splice-latex-header
  603. document-class-string
  604. org-export-latex-default-packages-alist ; defined in org.el
  605. org-export-latex-packages-alist nil ; defined in org.el
  606. (plist-get info :latex-header-extra))))))
  607. ;; 3. Define alert if not yet defined.
  608. "\\providecommand{\\alert}[1]{\\textbf{#1}}\n"
  609. ;; 4. Author.
  610. (let ((author (and (plist-get info :with-author)
  611. (let ((auth (plist-get info :author)))
  612. (and auth (org-export-secondary-string
  613. auth 'e-latex info)))))
  614. (email (and (plist-get info :with-email)
  615. (org-export-secondary-string
  616. (plist-get info :email) 'e-latex info))))
  617. (cond ((and author email (not (string= "" email)))
  618. (format "\\author{%s\\thanks{%s}}\n" author email))
  619. (author (format "\\author{%s}\n" author))
  620. (t "\\author{}\n")))
  621. ;; 5. Date.
  622. (let ((date (plist-get info :date)))
  623. (and date (format "\\date{%s}\n" date)))
  624. ;; 6. Title
  625. (format "\\title{%s}\n" title)
  626. ;; 7. Hyperref options.
  627. (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
  628. (or (plist-get info :keywords) "")
  629. (or (plist-get info :description) "")
  630. (let ((creator-info (plist-get info :with-creator)))
  631. (cond
  632. ((not creator-info) "")
  633. ((eq creator-info 'comment) "")
  634. (t (plist-get info :creator)))))
  635. ;; 7. Document start.
  636. "\\begin{document}\n\n"
  637. ;; 8. Title command.
  638. (org-element-normalize-string
  639. (cond ((string= "" title) nil)
  640. ((not (stringp org-e-latex-title-command)) nil)
  641. ((string-match "\\(?:[^%]\\|^\\)%s"
  642. org-e-latex-title-command)
  643. (format org-e-latex-title-command title))
  644. (t org-e-latex-title-command)))
  645. ;; 9. Table of contents.
  646. (let ((depth (plist-get info :with-toc)))
  647. (when depth
  648. (concat (when (wholenump depth)
  649. (format "\\setcounter{tocdepth}{%d}\n" depth))
  650. "\\tableofcontents\n\\vspace*{1cm}\n\n")))
  651. ;; 10. Document's body.
  652. contents
  653. ;; 11. Creator.
  654. (let ((creator-info (plist-get info :with-creator)))
  655. (cond
  656. ((not creator-info))
  657. ((eq creator-info 'comment)
  658. (format "%% %s\n" (plist-get info :creator)))
  659. (t (concat (plist-get info :creator) "\n"))))
  660. ;; 12. Document end.
  661. "\\end{document}")))
  662. ;;; Transcode Functions
  663. ;;;; Block
  664. (defun org-e-latex-center-block (center-block contents info)
  665. "Transcode a CENTER-BLOCK element from Org to LaTeX.
  666. CONTENTS holds the contents of the block. INFO is a plist
  667. holding contextual information."
  668. (org-e-latex--wrap-label
  669. center-block
  670. (format "\\begin{center}\n%s\\end{center}" contents)))
  671. ;;;; Comment
  672. ;; Comments are ignored.
  673. ;;;; Comment Block
  674. ;; Comment Blocks are ignored.
  675. ;;;; Drawer
  676. (defun org-e-latex-drawer (drawer contents info)
  677. "Transcode a DRAWER element from Org to LaTeX.
  678. CONTENTS holds the contents of the block. INFO is a plist
  679. holding contextual information."
  680. (let* ((name (org-element-get-property :drawer-name drawer))
  681. (output (if (functionp org-e-latex-format-drawer-function)
  682. (funcall org-e-latex-format-drawer-function
  683. name contents)
  684. ;; If there's no user defined function: simply
  685. ;; display contents of the drawer.
  686. contents)))
  687. (org-e-latex--wrap-label drawer output)))
  688. ;;;; Dynamic Block
  689. (defun org-e-latex-dynamic-block (dynamic-block contents info)
  690. "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
  691. CONTENTS holds the contents of the block. INFO is a plist
  692. holding contextual information. See
  693. `org-export-data'."
  694. (org-e-latex--wrap-label dynamic-block contents))
  695. ;;;; Emphasis
  696. (defun org-e-latex-emphasis (emphasis contents info)
  697. "Transcode EMPHASIS from Org to LaTeX.
  698. CONTENTS is the contents of the emphasized text. INFO is a plist
  699. holding contextual information.."
  700. (format (cdr (assoc (org-element-get-property :marker emphasis)
  701. org-e-latex-emphasis-alist))
  702. contents))
  703. ;;;; Entity
  704. (defun org-e-latex-entity (entity contents info)
  705. "Transcode an ENTITY object from Org to LaTeX.
  706. CONTENTS are the definition itself. INFO is a plist holding
  707. contextual information."
  708. (let ((ent (org-element-get-property :latex entity)))
  709. (if (org-element-get-property :latex-math-p entity)
  710. (format "$%s$" ent)
  711. ent)))
  712. ;;;; Example Block
  713. (defun org-e-latex-example-block (example-block contents info)
  714. "Transcode a EXAMPLE-BLOCK element from Org to LaTeX.
  715. CONTENTS is nil. INFO is a plist holding contextual information."
  716. (let* ((options (or (org-element-get-property :options example-block) ""))
  717. (value (org-export-handle-code example-block info)))
  718. (org-e-latex--wrap-label
  719. example-block (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
  720. ;;;; Export Snippet
  721. (defun org-e-latex-export-snippet (export-snippet contents info)
  722. "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
  723. CONTENTS is nil. INFO is a plist holding contextual information."
  724. (org-element-get-property :value export-snippet))
  725. ;;;; Export Block
  726. (defun org-e-latex-export-block (export-block contents info)
  727. "Transcode a EXPORT-BLOCK element from Org to LaTeX.
  728. CONTENTS is nil. INFO is a plist holding contextual information."
  729. (when (string= (org-element-get-property :type export-block) "latex")
  730. (org-remove-indentation (org-element-get-property :value export-block))))
  731. ;;;; Fixed Width
  732. (defun org-e-latex-fixed-width (fixed-width contents info)
  733. "Transcode a FIXED-WIDTH element from Org to LaTeX.
  734. CONTENTS is nil. INFO is a plist holding contextual information."
  735. (let* ((value (org-element-normalize-string
  736. (replace-regexp-in-string
  737. "^[ \t]*: ?" ""
  738. (org-element-get-property :value fixed-width)))))
  739. (org-e-latex--wrap-label
  740. fixed-width (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
  741. ;;;; Footnote Definition
  742. ;; Footnote Definitions are ignored.
  743. ;;;; Footnote Reference
  744. (defun org-e-latex-footnote-reference (footnote-reference contents info)
  745. "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
  746. CONTENTS is nil. INFO is a plist holding contextual information."
  747. (concat
  748. ;; Insert separator between two footnotes in a row.
  749. (let ((prev (org-export-get-previous-element footnote-reference info)))
  750. (when (and (listp prev) (eq (car prev) 'footnote-reference))
  751. org-e-latex-footnote-separator))
  752. ;; Use \footnotemark if the footnote has already been defined.
  753. ;; Otherwise, define it with \footnote command.
  754. (cond
  755. ((not (org-export-footnote-first-reference-p footnote-reference info))
  756. (format "\\footnotemark[%s]"
  757. (org-export-get-footnote-number footnote-reference info)))
  758. ;; Inline definitions are secondary strings.
  759. ((eq (org-element-get-property :type footnote-reference) 'inline)
  760. (format "\\footnote{%s}"
  761. (org-trim
  762. (org-export-secondary-string
  763. (org-export-get-footnote-definition footnote-reference info)
  764. 'e-latex info))))
  765. ;; Non-inline footnotes definitions are full Org data.
  766. (t
  767. (format "\\footnote{%s}"
  768. (org-trim
  769. (org-export-data
  770. (org-export-get-footnote-definition footnote-reference info)
  771. 'e-latex info)))))))
  772. ;;;; Headline
  773. (defun org-e-latex-headline (headline contents info)
  774. "Transcode an HEADLINE element from Org to LaTeX.
  775. CONTENTS holds the contents of the headline. INFO is a plist
  776. holding contextual information."
  777. (let* ((class (plist-get info :latex-class))
  778. (numberedp (plist-get info :section-numbers))
  779. ;; Get level relative to current parsed data.
  780. (level (org-export-get-relative-level headline info))
  781. (class-sectionning (assoc class org-e-latex-classes))
  782. ;; Section formatting will set two placeholders: one for the
  783. ;; title and the other for the contents.
  784. (section-fmt
  785. (let ((sec (if (and (symbolp (nth 2 class-sectionning))
  786. (fboundp (nth 2 class-sectionning)))
  787. (funcall (nth 2 class-sectionning) level numberedp)
  788. (nth (1+ level) class-sectionning))))
  789. (cond
  790. ;; No section available for that LEVEL.
  791. ((not sec) nil)
  792. ;; Section format directly returned by a function.
  793. ((stringp sec) sec)
  794. ;; (numbered-section . unnumbered-section)
  795. ((not (consp (cdr sec)))
  796. (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
  797. ;; (numbered-open numbered-close)
  798. ((= (length sec) 2)
  799. (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
  800. ;; (num-in num-out no-num-in no-num-out)
  801. ((= (length sec) 4)
  802. (if numberedp
  803. (concat (car sec) "\n%s" (nth 1 sec))
  804. (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
  805. (text (org-export-secondary-string
  806. (org-element-get-property :title headline) 'e-latex info))
  807. (todo (and (plist-get info :with-todo-keywords)
  808. (let ((todo (org-element-get-property
  809. :todo-keyword headline)))
  810. (and todo
  811. (org-export-secondary-string todo 'e-latex info)))))
  812. (todo-type (and todo (org-element-get-property :todo-type headline)))
  813. (tags (and (plist-get info :with-tags)
  814. (org-element-get-property :tags headline)))
  815. (priority (and (plist-get info :with-priority)
  816. (org-element-get-property :priority headline)))
  817. ;; Create the headline text.
  818. (full-text (if (functionp org-e-latex-format-headline-function)
  819. ;; User-defined formatting function.
  820. (funcall org-e-latex-format-headline-function
  821. todo todo-type priority text tags)
  822. ;; Default formatting.
  823. (concat
  824. (when todo
  825. (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  826. (when priority (format "\\framebox{\\#%c} " priority))
  827. text
  828. (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
  829. ;; Associate some \label to the headline for internal links.
  830. (headline-labels (mapconcat
  831. (lambda (p)
  832. (let ((val (org-element-get-property p headline)))
  833. (when val (format "\\label{%s}\n"
  834. (if (eq p :begin)
  835. (format "headline-%s" val)
  836. val)))))
  837. '(:custom-id :id :begin) ""))
  838. (pre-blanks (make-string (org-element-get-property :pre-blank headline)
  839. 10)))
  840. (cond
  841. ;; Case 1: This is a footnote section: ignore it.
  842. ((org-element-get-property :footnote-section-p headline) nil)
  843. ;; Case 2. This is a deep sub-tree: export it as a list item.
  844. ;; Also export as items headlines for which no section
  845. ;; format has been found.
  846. ((or (not section-fmt)
  847. (and (wholenump (plist-get info :headline-levels))
  848. (> level (plist-get info :headline-levels))))
  849. ;; Build the real contents of the sub-tree.
  850. (let ((low-level-body
  851. (concat
  852. ;; If the headline is the first sibling, start a list.
  853. (when (org-export-first-sibling-p headline info)
  854. (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
  855. ;; Itemize headline
  856. "\\item " full-text "\n" headline-labels pre-blanks contents)))
  857. ;; If headline in the last sibling, close the list, before any
  858. ;; blank line. Otherwise, simply return LOW-LEVEL-BODY.
  859. (if (org-export-last-sibling-p headline info)
  860. (replace-regexp-in-string
  861. "[ \t\n]*\\'"
  862. (format "\n\\\\end{%s}" (if numberedp 'enumerate 'itemize))
  863. low-level-body)
  864. low-level-body)))
  865. ;; Case 3. Standard headline. Export it as a section.
  866. (t (format section-fmt full-text
  867. (concat headline-labels pre-blanks contents))))))
  868. ;;;; Horizontal Rule
  869. (defun org-e-latex-horizontal-rule (horizontal-rule contents info)
  870. "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
  871. CONTENTS is nil. INFO is a plist holding contextual information."
  872. (let ((attr (mapconcat #'identity
  873. (org-element-get-property :attr_latex horizontal-rule)
  874. " ")))
  875. (org-e-latex--wrap-label horizontal-rule (concat "\\hrule " attr))))
  876. ;;;; Inline Babel Call
  877. ;; Inline Babel Calls are ignored.
  878. ;;;; Inline Src Block
  879. (defun org-e-latex-inline-src-block (inline-src-block contents info)
  880. "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
  881. CONTENTS holds the contents of the item. INFO is a plist holding
  882. contextual information."
  883. (let* ((code (org-element-get-property :value inline-src-block))
  884. (separator (org-e-latex--find-verb-separator code)))
  885. (cond
  886. ;; Do not use a special package: transcode it verbatim.
  887. ((not org-e-latex-listings)
  888. (concat "\\verb" separator code separator))
  889. ;; Use minted package.
  890. ((eq org-e-latex-listings 'minted)
  891. (let* ((org-lang (org-element-get-property :language inline-src-block))
  892. (mint-lang (or (cadr (assq (intern org-lang)
  893. org-e-latex-minted-langs))
  894. org-lang))
  895. (options (org-e-latex--make-option-string
  896. org-e-latex-minted-options)))
  897. (concat (format "\\mint%s{%s}"
  898. (if (string= options "") "" (format "[%s]" options))
  899. mint-lang)
  900. separator code separator)))
  901. ;; Use listings package.
  902. (t
  903. ;; Maybe translate language's name.
  904. (let* ((org-lang (org-element-get-property :language inline-src-block))
  905. (lst-lang (or (cadr (assq (intern org-lang)
  906. org-e-latex-listings-langs))
  907. org-lang))
  908. (options (org-e-latex--make-option-string
  909. (append org-e-latex-listings-options
  910. `(("language" ,lst-lang))))))
  911. (concat (format "\\lstinline[%s]" options)
  912. separator code separator))))))
  913. ;;;; Inlinetask
  914. (defun org-e-latex-inlinetask (inlinetask contents info)
  915. "Transcode an INLINETASK element from Org to LaTeX.
  916. CONTENTS holds the contents of the block. INFO is a plist
  917. holding contextual information."
  918. (let ((title (org-export-secondary-string
  919. (org-element-get-property :title inlinetask) 'e-latex info))
  920. (todo (and (plist-get info :with-todo-keywords)
  921. (let ((todo (org-element-get-property
  922. :todo-keyword inlinetask)))
  923. (and todo
  924. (org-export-secondary-string todo 'e-latex info)))))
  925. (todo-type (org-element-get-property :todo-type inlinetask))
  926. (tags (and (plist-get info :with-tags)
  927. (org-element-get-property :tags inlinetask)))
  928. (priority (and (plist-get info :with-priority)
  929. (org-element-get-property :priority inlinetask))))
  930. ;; If `org-e-latex-format-inlinetask-function' is provided, call it
  931. ;; with appropriate arguments.
  932. (if (functionp org-e-latex-format-inlinetask-function)
  933. (funcall org-e-latex-format-inlinetask-function
  934. todo todo-type priority title tags contents)
  935. ;; Otherwise, use a default template.
  936. (org-e-latex--wrap-label
  937. inlinetask
  938. (let ((full-title
  939. (concat
  940. (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
  941. (when priority (format "\\framebox{\\#%c} " priority))
  942. title
  943. (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
  944. (format (concat "\\begin{center}\n"
  945. "\\fbox{\n"
  946. "\\begin{minipage}[c]{.6\\textwidth}\n"
  947. "%s\n\n"
  948. "\\rule[.8em]{\\textwidth}{2pt}\n\n"
  949. "%s"
  950. "\\end{minipage}\n"
  951. "}\n"
  952. "\\end{center}")
  953. full-title contents))))))
  954. ;;;; Item
  955. (defun org-e-latex-item (item contents info)
  956. "Transcode an ITEM element from Org to LaTeX.
  957. CONTENTS holds the contents of the item. INFO is a plist holding
  958. contextual information."
  959. ;; Grab `:level' from plain-list properties, which is always the
  960. ;; first element above current item.
  961. (let* ((level (org-element-get-property
  962. :level (car (plist-get info :genealogy))))
  963. (counter (let ((count (org-element-get-property :counter item)))
  964. (and count
  965. (< level 4)
  966. (format "\\setcounter{enum%s}{%s}\n"
  967. (nth level '("i" "ii" "iii" "iv"))
  968. (1- count)))))
  969. (checkbox (let ((checkbox (org-element-get-property :checkbox item)))
  970. (cond ((eq checkbox 'on) "$\\boxtimes$ ")
  971. ((eq checkbox 'off) "$\\Box$ ")
  972. ((eq checkbox 'trans) "$\\boxminus$ "))))
  973. (tag (let ((tag (org-element-get-property :tag item)))
  974. (and tag
  975. (format "[%s]" (org-export-secondary-string
  976. tag 'e-latex info))))))
  977. (concat counter "\\item" tag " " checkbox contents)))
  978. ;;;; Keyword
  979. (defun org-e-latex-keyword (keyword contents info)
  980. "Transcode a KEYWORD element from Org to LaTeX.
  981. CONTENTS is nil. INFO is a plist holding contextual information."
  982. (let ((key (downcase (org-element-get-property :key keyword)))
  983. (value (org-element-get-property :value keyword)))
  984. (cond
  985. ((string= key "latex") value)
  986. ((string= key "index") (format "\\index{%s}" value))
  987. ((string= key "target")
  988. (format "\\label{%s}" (org-export-solidify-link-text value)))
  989. ((string= key "toc")
  990. (let ((value (downcase value)))
  991. (cond
  992. ((string-match "\\<headlines\\>" value)
  993. (let ((depth (or (and (string-match "[0-9]+" value)
  994. (string-to-number (match-string 0 value)))
  995. (plist-get info :with-toc))))
  996. (concat
  997. (when (wholenump depth)
  998. (format "\\setcounter{tocdepth}{%s}\n" depth))
  999. "\\tableofcontents")))
  1000. ((string= "tables" value) "\\listoftables")
  1001. ((string= "figures" value) "\\listoffigures")
  1002. ((string= "listings" value) "\\listoflistings"))))
  1003. ((string= key "include")
  1004. (org-export-included-file keyword 'e-latex info)))))
  1005. ;;;; Latex Environment
  1006. (defun org-e-latex-latex-environment (latex-environment contents info)
  1007. "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
  1008. CONTENTS is nil. INFO is a plist holding contextual information."
  1009. (org-e-latex--wrap-label
  1010. latex-environment
  1011. (org-remove-indentation (org-element-get-property :value latex-environment))))
  1012. ;;;; Latex Fragment
  1013. (defun org-e-latex-latex-fragment (latex-fragment contents info)
  1014. "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
  1015. CONTENTS is nil. INFO is a plist holding contextual information."
  1016. (org-element-get-property :value latex-fragment))
  1017. ;;;; Line Break
  1018. (defun org-e-latex-line-break (line-break contents info)
  1019. "Transcode a LINE-BREAK object from Org to LaTeX.
  1020. CONTENTS is nil. INFO is a plist holding contextual information."
  1021. "\\\\")
  1022. ;;;; Link
  1023. (defun org-e-latex-link--inline-image (path info)
  1024. "Return LaTeX code for an image at PATH.
  1025. INFO is a plist containing export options."
  1026. (let* ((parent-props (nth 1 (car (plist-get info :genealogy))))
  1027. (caption (org-e-latex--caption/label-string
  1028. (plist-get parent-props :caption)
  1029. (plist-get parent-props :name)
  1030. info))
  1031. ;; Retrieve latex attributes from the element around.
  1032. (attr (let ((raw-attr
  1033. (mapconcat #'identity
  1034. (plist-get parent-props :attr_latex) " ")))
  1035. (unless (string= raw-attr "") raw-attr)))
  1036. (disposition
  1037. (cond
  1038. ((and attr (string-match "\\<wrap\\>" attr)) 'wrap)
  1039. ((and attr (string-match "\\<multicolumn\\>" attr)) 'multicolumn)
  1040. ((or (and attr (string-match "\\<float\\>" attr))
  1041. (not (string= caption "")))
  1042. 'float)))
  1043. (placement
  1044. (cond
  1045. ((and attr (string-match "\\<placement=\\(\\S-+\\)" attr))
  1046. (org-match-string-no-properties 1 attr))
  1047. ((eq disposition 'wrap) "{l}{0.5\\textwidth}")
  1048. ((eq disposition 'float)
  1049. (concat "[" org-e-latex-default-figure-position "]"))
  1050. (t ""))))
  1051. ;; Now clear ATTR from any special keyword and set a default
  1052. ;; value if nothing is left.
  1053. (if (not attr)
  1054. (setq attr "")
  1055. (while (string-match "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)"
  1056. attr)
  1057. (replace-match "" nil nil attr))
  1058. (setq attr (org-trim attr)))
  1059. (setq attr (cond ((not (string= attr "")) attr)
  1060. ((eq disposition 'float) "width=0.7\\textwidth")
  1061. ((eq disposition 'wrap) "width=0.48\\textwidth")
  1062. (t (or org-e-latex-image-default-option ""))))
  1063. ;; Return proper string, depending on DISPOSITION.
  1064. (case disposition
  1065. ('wrap (format "\\begin{wrapfigure}%s
  1066. \\centering
  1067. \\includegraphics[%s]{%s}
  1068. %s\\end{wrapfigure}" placement attr path caption))
  1069. ('mulicolumn (format "\\begin{figure*}%s
  1070. \\centering
  1071. \\includegraphics[%s]{%s}
  1072. %s\\end{figure*}" placement attr path caption))
  1073. ('float (format "\\begin{figure}%s
  1074. \\centering
  1075. \\includegraphics[%s]{%s}
  1076. %s\\end{figure}" placement attr path caption))
  1077. (t (format "\\includegraphics[%s]{%s}" attr path)))))
  1078. (defun org-e-latex-link (link desc info)
  1079. "Transcode a LINK object from Org to LaTeX.
  1080. DESC is the description part of the link, or the empty string.
  1081. INFO is a plist holding contextual information. See
  1082. `org-export-data'."
  1083. (let* ((type (org-element-get-property :type link))
  1084. (raw-path (org-element-get-property :path link))
  1085. ;; Ensure DESC really exists, or set it to nil.
  1086. (desc (and (not (string= desc "")) desc))
  1087. (imagep (org-export-inline-image-p
  1088. link org-e-latex-inline-image-extensions))
  1089. (path (cond
  1090. ((member type '("http" "https" "ftp" "mailto"))
  1091. (concat type ":" raw-path))
  1092. ((and (not imagep) (string= type "file"))
  1093. (when (string-match "\\(.+\\)::.+" raw-path)
  1094. (setq raw-path (match-string 1 raw-path)))
  1095. (if (file-name-absolute-p raw-path)
  1096. (concat "file://" (expand-file-name raw-path))
  1097. ;; TODO: Not implemented yet. Concat also:
  1098. ;; (org-export-directory :LaTeX info)
  1099. (concat "file://" raw-path)))
  1100. (t raw-path)))
  1101. protocol)
  1102. (cond
  1103. ;; Image file.
  1104. (imagep (org-e-latex-link--inline-image path info))
  1105. ;; Id: for now, assume it's an internal link. TODO: do something
  1106. ;; to check if it isn't in the current file.
  1107. ((string= type "id")
  1108. (format "\\hyperref[%s]{%s}" path (or desc path)))
  1109. ;; Custom-id, target or radioed target: replace link with the
  1110. ;; normalized custom-id/target name.
  1111. ((member type '("custom-id" "target" "radio"))
  1112. (format "\\hyperref[%s]{%s}"
  1113. (org-export-solidify-link-text path)
  1114. (or desc (org-export-secondary-string path 'e-latex info))))
  1115. ;; Fuzzy: With the help of `org-export-resolve-fuzzy-link', find
  1116. ;; the destination of the link.
  1117. ((string= type "fuzzy")
  1118. (let ((destination (org-export-resolve-fuzzy-link link info)))
  1119. (cond
  1120. ;; Target match.
  1121. ((stringp destination)
  1122. (format "\\hyperref[%s]{%s}"
  1123. (org-export-solidify-link-text destination)
  1124. (or desc
  1125. (org-export-secondary-string
  1126. (org-element-get-property :raw-link link) 'e-latex info))))
  1127. ;; Headline match.
  1128. ((integerp destination)
  1129. (format "\\hyperref[headline-%d]{%s}"
  1130. destination
  1131. (or desc
  1132. (org-export-secondary-string
  1133. (org-element-get-property :raw-link link) 'e-latex info))))
  1134. ;; No match.
  1135. (t (format "\\texttt{%s}"
  1136. (or desc
  1137. (org-export-secondary-string
  1138. (org-element-get-property :raw-link link)
  1139. 'e-latex info)))))))
  1140. ;; Coderef: replace link with the reference name or the
  1141. ;; equivalent line number.
  1142. ((string= type "coderef")
  1143. (format (org-export-get-coderef-format path (or desc ""))
  1144. (org-export-resolve-coderef path info)))
  1145. ;; Link type is handled by a special function.
  1146. ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
  1147. (funcall protocol (org-link-unescape path) desc 'latex))
  1148. ;; External link with a description part.
  1149. ((and path desc) (format "\\href{%s}{%s}" path desc))
  1150. ;; External link without a description part.
  1151. (path (format "\\url{%s}" path))
  1152. ;; No path, only description. Try to do something useful.
  1153. (t (format "\\texttt{%s}" desc)))))
  1154. ;;;; Babel Call
  1155. ;; Babel Calls are ignored.
  1156. ;;;; Macro
  1157. (defun org-e-latex-macro (macro contents info)
  1158. "Transcode a MACRO element from Org to LaTeX.
  1159. CONTENTS is nil. INFO is a plist holding contextual information."
  1160. ;; Use available tools.
  1161. (org-export-expand-macro macro info))
  1162. ;;;; Paragraph
  1163. (defun org-e-latex-paragraph (paragraph contents info)
  1164. "Transcode a PARAGRAPH element from Org to LaTeX.
  1165. CONTENTS is the contents of the paragraph, as a string. INFO is
  1166. the plist used as a communication channel."
  1167. contents)
  1168. ;;;; Plain List
  1169. (defun org-e-latex-plain-list (plain-list contents info)
  1170. "Transcode a PLAIN-LIST element from Org to LaTeX.
  1171. CONTENTS is the contents of the list. INFO is a plist holding
  1172. contextual information."
  1173. (let* ((type (org-element-get-property :type plain-list))
  1174. (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
  1175. "inparadesc" "asparadesc"))
  1176. (paralist-regexp (concat
  1177. "\\("
  1178. (mapconcat 'identity paralist-types "\\|")
  1179. "\\)"))
  1180. (attr (mapconcat #'identity
  1181. (org-element-get-property :attr_latex plain-list)
  1182. " "))
  1183. (latex-type (cond
  1184. ((and attr
  1185. (string-match
  1186. (format "\\<%s\\>" paralist-regexp) attr))
  1187. (match-string 1 attr))
  1188. ((eq type 'ordered) "enumerate")
  1189. ((eq type 'unordered) "itemize")
  1190. ((eq type 'descriptive) "description"))))
  1191. (org-e-latex--wrap-label
  1192. plain-list
  1193. (format "\\begin{%s}%s\n%s\\end{%s}"
  1194. latex-type
  1195. ;; Once special environment, if any, has been removed, the
  1196. ;; rest of the attributes will be optional arguments.
  1197. ;; They will be put inside square brackets if necessary.
  1198. (let ((opt (replace-regexp-in-string
  1199. (format " *%s *" paralist-regexp) "" attr)))
  1200. (cond ((string= opt "") "")
  1201. ((string-match "\\`\\[[^][]+\\]\\'" opt) opt)
  1202. (t (format "[%s]" opt))))
  1203. contents
  1204. latex-type))))
  1205. ;;;; Plain Text
  1206. (defun org-e-latex-plain-text (text info)
  1207. "Transcode a TEXT string from Org to LaTeX.
  1208. TEXT is the string to transcode. INFO is a plist holding
  1209. contextual information."
  1210. ;; Protect %, #, &, $, ~, ^, _, { and }.
  1211. (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
  1212. (setq text
  1213. (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
  1214. ;; Protect \
  1215. (setq text (replace-regexp-in-string
  1216. "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
  1217. "$\\backslash$" text nil t 1))
  1218. ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
  1219. (let ((case-fold-search nil)
  1220. (start 0))
  1221. (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
  1222. (setq text (replace-match
  1223. (format "\\%s{}" (match-string 1 text)) nil t text)
  1224. start (match-end 0))))
  1225. ;; Handle quotation marks
  1226. (setq text (org-e-latex--quotation-marks text info))
  1227. ;; Convert special strings.
  1228. (when (plist-get info :with-special-strings)
  1229. (while (string-match (regexp-quote "...") text)
  1230. (setq text (replace-match "\\ldots{}" nil t text))))
  1231. ;; Handle break preservation if required.
  1232. (when (plist-get info :preserve-breaks)
  1233. (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
  1234. text)))
  1235. ;; Return value.
  1236. text)
  1237. ;;;; Property Drawer
  1238. (defun org-e-latex-property-drawer (property-drawer contents info)
  1239. "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
  1240. CONTENTS is nil. INFO is a plist holding contextual
  1241. information."
  1242. ;; The property drawer isn't exported but we want separating blank
  1243. ;; lines nonetheless.
  1244. "")
  1245. ;;;; Quote Block
  1246. (defun org-e-latex-quote-block (quote-block contents info)
  1247. "Transcode a QUOTE-BLOCK element from Org to LaTeX.
  1248. CONTENTS holds the contents of the block. INFO is a plist
  1249. holding contextual information."
  1250. (org-e-latex--wrap-label
  1251. quote-block
  1252. (format "\\begin{quote}\n%s\\end{quote}" contents)))
  1253. ;;;; Quote Section
  1254. (defun org-e-latex-quote-section (quote-section contents info)
  1255. "Transcode a QUOTE-SECTION element from Org to LaTeX.
  1256. CONTENTS is nil. INFO is a plist holding contextual information."
  1257. (let ((value (org-remove-indentation
  1258. (org-element-get-property :value quote-section))))
  1259. (when value (format "\\begin{verbatim}\n%s\\end{verbatim}" value))))
  1260. ;;;; Section
  1261. (defun org-e-latex-section (section contents info)
  1262. "Transcode a SECTION element from Org to LaTeX.
  1263. CONTENTS holds the contents of the section. INFO is a plist
  1264. holding contextual information."
  1265. contents)
  1266. ;;;; Radio Target
  1267. (defun org-e-latex-radio-target (radio-target text info)
  1268. "Transcode a RADIO-TARGET object from Org to LaTeX.
  1269. TEXT is the text of the target. INFO is a plist holding
  1270. contextual information."
  1271. (format "\\label{%s}%s"
  1272. (org-export-solidify-link-text
  1273. (org-element-get-property :raw-value radio-target))
  1274. text))
  1275. ;;;; Special Block
  1276. (defun org-e-latex-special-block (special-block contents info)
  1277. "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
  1278. CONTENTS holds the contents of the block. INFO is a plist
  1279. holding contextual information."
  1280. (let ((type (downcase (org-element-get-property :type special-block))))
  1281. (org-e-latex--wrap-label
  1282. special-block
  1283. (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
  1284. ;;;; Src Block
  1285. (defun org-e-latex-src-block (src-block contents info)
  1286. "Transcode a SRC-BLOCK element from Org to LaTeX.
  1287. CONTENTS holds the contents of the item. INFO is a plist holding
  1288. contextual information."
  1289. (let* ((lang (org-element-get-property :language src-block))
  1290. (code (org-export-handle-code src-block info))
  1291. (caption (org-element-get-property :caption src-block))
  1292. (label (org-element-get-property :name src-block))
  1293. (custom-env (and lang
  1294. (cadr (assq (intern lang)
  1295. org-e-latex-custom-lang-environments)))))
  1296. (cond
  1297. ;; No source fontification.
  1298. ((not org-e-latex-listings)
  1299. (let ((caption-str (org-e-latex--caption/label-string
  1300. caption label info))
  1301. (float-env (when caption "\\begin{figure}[H]\n%s\n\\end{figure}")))
  1302. (format (or float-env "%s")
  1303. (concat
  1304. caption-str
  1305. (format "\\begin{verbatim}\n%s\\end{verbatim}" code)))))
  1306. ;; Custom environment.
  1307. (custom-env
  1308. (format "\\begin{%s}\n%s\\end{%s}\n" custom-env code custom-env))
  1309. ;; Use minted package.
  1310. ((eq org-e-latex-listings 'minted)
  1311. (let* ((mint-lang (or (cadr (assq (intern lang) org-e-latex-minted-langs))
  1312. lang))
  1313. (float-env (when (or label caption)
  1314. (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
  1315. (org-e-latex--caption/label-string
  1316. caption label info))))
  1317. (body (format "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
  1318. (org-e-latex--make-option-string
  1319. org-e-latex-minted-options)
  1320. mint-lang code)))
  1321. (if float-env (format float-env body) body)))
  1322. ;; Use listings package.
  1323. (t
  1324. (let ((lst-lang (or (cadr (assq (intern lang) org-e-latex-listings-langs))
  1325. lang))
  1326. (caption-str (and caption
  1327. (org-export-secondary-string
  1328. (org-element-get-property :caption src-block)
  1329. 'e-latex info))))
  1330. (concat (format "\\lstset{%s}\n"
  1331. (org-e-latex--make-option-string
  1332. (append org-e-latex-listings-options
  1333. `(("language" ,lst-lang))
  1334. (when label `(("label" ,label)))
  1335. (when caption-str
  1336. `(("caption" ,caption-str))))))
  1337. (format "\\begin{lstlisting}\n%s\\end{lstlisting}" code)))))))
  1338. ;;;; Statistics Cookie
  1339. (defun org-e-latex-statistics-cookie (statistics-cookie contents info)
  1340. "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
  1341. CONTENTS is nil. INFO is a plist holding contextual information."
  1342. (org-element-get-property :value statistics-cookie))
  1343. ;;;; Subscript
  1344. (defun org-e-latex-subscript (subscript contents info)
  1345. "Transcode a SUBSCRIPT object from Org to LaTeX.
  1346. CONTENTS is the contents of the object. INFO is a plist holding
  1347. contextual information."
  1348. (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents))
  1349. ;;;; Superscript
  1350. (defun org-e-latex-superscript (superscript contents info)
  1351. "Transcode a SUPERSCRIPT object from Org to LaTeX.
  1352. CONTENTS is the contents of the object. INFO is a plist holding
  1353. contextual information."
  1354. (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents))
  1355. ;;;; Table
  1356. (defun org-e-latex-table--format-string (table info)
  1357. "Return an appropriate format string for TABLE.
  1358. INFO is the plist containing format info about the table, as
  1359. returned by `org-export-table-format-info'.
  1360. The format string one placeholder for the body of the table."
  1361. (let* ((label (org-element-get-property :name table))
  1362. (caption (org-e-latex--caption/label-string
  1363. (org-element-get-property :caption table) label info))
  1364. (attr (mapconcat #'identity
  1365. (org-element-get-property :attr_latex table)
  1366. " "))
  1367. ;; Determine alignment string.
  1368. (alignment (org-e-latex-table--align-string attr info))
  1369. ;; Determine environment for the table: longtable, tabular...
  1370. (table-env (cond
  1371. ((not attr) org-e-latex-default-table-environment)
  1372. ((string-match "\\<longtable\\>" attr) "longtable")
  1373. ((string-match "\\(tabular.\\)" attr)
  1374. (org-match-string-no-properties 1 attr))
  1375. (t org-e-latex-default-table-environment)))
  1376. ;; If table is a float, determine environment: table or table*.
  1377. (float-env (cond
  1378. ((string= "longtable" table-env) nil)
  1379. ((and attr
  1380. (or (string-match (regexp-quote "table*") attr)
  1381. (string-match "\\<multicolumn\\>" attr)))
  1382. "table*")
  1383. ((or (not (string= caption "")) label) "table")))
  1384. ;; Extract others display options.
  1385. (width (and attr
  1386. (string-match "\\<width=\\(\\S-+\\)" attr)
  1387. (org-match-string-no-properties 1 attr)))
  1388. (placement (if (and attr
  1389. (string-match "\\<placement=\\(\\S-+\\)" attr))
  1390. (org-match-string-no-properties 1 attr)
  1391. (concat "["
  1392. org-e-latex-default-figure-position
  1393. "]"))))
  1394. ;; Prepare the final format string for the table.
  1395. (cond
  1396. ;; Longtable.
  1397. ((string= "longtable" table-env)
  1398. (format "\\begin{longtable}{%s}\n%s\n%%s\n%s\\end{longtable}"
  1399. alignment
  1400. (if (or (not org-e-latex-table-caption-above)
  1401. (string= "" caption))
  1402. ""
  1403. (concat (org-trim caption) "\\\\"))
  1404. (if (or org-e-latex-table-caption-above
  1405. (string= "" caption))
  1406. ""
  1407. (concat (org-trim caption) "\\\\\n"))))
  1408. ;; Others.
  1409. (t (concat (when float-env
  1410. (concat
  1411. (format "\\begin{%s}%s\n" float-env placement)
  1412. (if org-e-latex-table-caption-above caption "")))
  1413. (when org-e-latex-tables-centered "\\begin{center}\n")
  1414. (format "\\begin{%s}%s{%s}\n%%s\n\\end{%s}"
  1415. table-env
  1416. (if width (format "{%s}" width) "")
  1417. alignment
  1418. table-env)
  1419. (when org-e-latex-tables-centered "\n\\end{center}")
  1420. (when float-env
  1421. (concat (if org-e-latex-table-caption-above "" caption)
  1422. (format "\n\\end{%s}" float-env))))))))
  1423. (defun org-e-latex-table--align-string (attr info)
  1424. "Return an appropriate LaTeX alignment string.
  1425. INFO is the plist containing format info about the table, as
  1426. returned by `org-export-table-format-info'."
  1427. (or (and attr
  1428. (string-match "\\<align=\\(\\S-+\\)" attr)
  1429. (match-string 1 attr))
  1430. (let* ((align (copy-sequence (plist-get info :alignment)))
  1431. (colgroups (copy-sequence (plist-get info :column-groups)))
  1432. (cols (length align))
  1433. (separators (make-vector (1+ cols) "")))
  1434. ;; Ignore the first column if it's special.
  1435. (when (plist-get info :special-column-p)
  1436. (aset align 0 "") (aset colgroups 0 nil))
  1437. (let ((col 0))
  1438. (mapc (lambda (el)
  1439. (let ((gr (aref colgroups col)))
  1440. (when (memq gr '(start start-end))
  1441. (aset separators col "|"))
  1442. (when (memq gr '(end start-end))
  1443. (aset separators (1+ col) "|")))
  1444. (incf col))
  1445. align))
  1446. ;; Build the LaTeX specific alignment string.
  1447. (loop for al across align
  1448. for sep across separators
  1449. concat (concat sep al) into output
  1450. finally return (concat output (aref separators cols))))))
  1451. (defun org-e-latex-table (table contents info)
  1452. "Transcode a TABLE element from Org to LaTeX.
  1453. CONTENTS is nil. INFO is a plist holding contextual information."
  1454. (let ((attr (mapconcat #'identity
  1455. (org-element-get-property :attr_latex table)
  1456. " "))
  1457. (raw-table (org-element-get-property :raw-table table)))
  1458. (cond
  1459. ;; Case 1: verbatim table.
  1460. ((or org-e-latex-tables-verbatim
  1461. (and attr (string-match "\\<verbatim\\>" attr)))
  1462. (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
  1463. (org-export-clean-table
  1464. raw-table
  1465. (plist-get (org-export-table-format-info raw-table)
  1466. :special-column-p))))
  1467. ;; Case 2: table.el table. Convert it using appropriate tools.
  1468. ((eq (org-element-get-property :type table) 'table.el)
  1469. (require 'table)
  1470. ;; Ensure "*org-export-table*" buffer is empty.
  1471. (and (get-buffer "*org-export-table*")
  1472. (kill-buffer (get-buffer "*org-export-table*")))
  1473. (let ((output (with-temp-buffer
  1474. (insert raw-table)
  1475. (goto-char 1)
  1476. (re-search-forward "^[ \t]*|[^|]" nil t)
  1477. (table-generate-source 'latex "*org-export-table*")
  1478. (with-current-buffer "*org-export-table*"
  1479. (org-trim (buffer-string))))))
  1480. (kill-buffer (get-buffer "*org-export-table*"))
  1481. ;; Remove left out comments.
  1482. (while (string-match "^%.*\n" output)
  1483. (setq output (replace-match "" t t output)))
  1484. ;; When the "rmlines" attribute is provided, remove all hlines
  1485. ;; but the the one separating heading from the table body.
  1486. (when (and attr (string-match "\\<rmlines\\>" attr))
  1487. (let ((n 0) (pos 0))
  1488. (while (and (< (length output) pos)
  1489. (setq pos (string-match "^\\\\hline\n?" output pos)))
  1490. (incf n)
  1491. (unless (= n 2) (setq output (replace-match "" nil nil output))))))
  1492. (if org-e-latex-tables-centered
  1493. (format "\\begin{center}\n%s\n\\end{center}" output)
  1494. output)))
  1495. ;; Case 3: Standard table.
  1496. (t
  1497. (let* ((table-info (org-export-table-format-info raw-table))
  1498. (clean-table (org-export-clean-table
  1499. raw-table (plist-get table-info :special-column-p)))
  1500. (columns-number (length (plist-get table-info :alignment))))
  1501. ;; Convert ROWS to send them to `orgtbl-to-latex'. In
  1502. ;; particular, send each cell to
  1503. ;; `org-element-parse-secondary-string' to expand any Org
  1504. ;; object within. Eventually, flesh the format string out with
  1505. ;; the table.
  1506. (format (org-e-latex-table--format-string table table-info)
  1507. (orgtbl-to-latex
  1508. (mapcar
  1509. (lambda (row)
  1510. (if (string-match org-table-hline-regexp row)
  1511. 'hline
  1512. (mapcar
  1513. (lambda (cell)
  1514. (org-export-secondary-string
  1515. (org-element-parse-secondary-string
  1516. cell
  1517. (cdr (assq 'table org-element-string-restrictions)))
  1518. 'e-latex info))
  1519. (org-split-string row "[ \t]*|[ \t]*"))))
  1520. (org-split-string clean-table "\n"))
  1521. `(:tstart nil :tend nil
  1522. ;; Longtable environment requires specific
  1523. ;; header line end.
  1524. :hlend ,(and attr
  1525. (string-match "\\<longtable\\>" attr)
  1526. (format "\\\\
  1527. \\hline
  1528. \\endhead
  1529. \\hline\\multicolumn{%d}{r}{Continued on next page}\\\\
  1530. \\endfoot
  1531. \\endlastfoot"
  1532. columns-number))))))))))
  1533. ;;;; Target
  1534. (defun org-e-latex-target (target text info)
  1535. "Transcode a TARGET object from Org to LaTeX.
  1536. TEXT is the text of the target. INFO is a plist holding
  1537. contextual information."
  1538. (format "\\label{%s}%s"
  1539. (org-export-solidify-link-text
  1540. (org-element-get-property :raw-value target))
  1541. text))
  1542. ;;;; Time-stamp
  1543. (defun org-e-latex-time-stamp (time-stamp contents info)
  1544. "Transcode a TIME-STAMP object from Org to LaTeX.
  1545. CONTENTS is nil. INFO is a plist holding contextual information."
  1546. (let ((value (org-element-get-property :value time-stamp))
  1547. (type (org-element-get-property :type time-stamp))
  1548. (appt-type (org-element-get-property :appt-type time-stamp)))
  1549. (concat (cond ((eq appt-type 'scheduled)
  1550. (format "\\textbf{\\textsc{%s}} " org-scheduled-string))
  1551. ((eq appt-type 'deadline)
  1552. (format "\\textbf{\\textsc{%s}} " org-deadline-string))
  1553. ((eq appt-type 'closed)
  1554. (format "\\textbf{\\textsc{%s}} " org-closed-string)))
  1555. (cond ((memq type '(active active-range))
  1556. (format org-e-latex-active-timestamp-format value))
  1557. ((memq type '(inactive inactive-range))
  1558. (format org-e-latex-inactive-timestamp-format value))
  1559. (t
  1560. (format org-e-latex-diary-timestamp-format value))))))
  1561. ;;;; Verbatim
  1562. (defun org-e-latex-verbatim (element contents info)
  1563. "Return verbatim text in LaTeX."
  1564. (let ((fmt (cdr (assoc (org-element-get-property :marker element)
  1565. org-e-latex-emphasis-alist)))
  1566. (value (org-element-get-property :value element)))
  1567. (cond
  1568. ;; Handle the `verb' special case.
  1569. ((eq 'verb fmt)
  1570. (let ((separator (org-e-latex--find-verb-separator value)))
  1571. (concat "\\verb" separator value separator)))
  1572. ;; Handle the `protectedtexttt' special case.
  1573. ((eq 'protectedtexttt fmt)
  1574. (let ((start 0)
  1575. (trans '(("\\" . "\\textbackslash{}")
  1576. ("~" . "\\textasciitilde{}")
  1577. ("^" . "\\textasciicircum{}")))
  1578. (rtn "")
  1579. char)
  1580. (while (string-match "[\\{}$%&_#~^]" value)
  1581. (setq char (match-string 0 value))
  1582. (if (> (match-beginning 0) 0)
  1583. (setq rtn (concat rtn (substring value 0 (match-beginning 0)))))
  1584. (setq value (substring value (1+ (match-beginning 0))))
  1585. (setq char (or (cdr (assoc char trans)) (concat "\\" char))
  1586. rtn (concat rtn char)))
  1587. (setq value (concat rtn value)
  1588. fmt "\\texttt{%s}")
  1589. (while (string-match "--" value)
  1590. (setq value (replace-match "-{}-" t t value)))
  1591. (format fmt value)))
  1592. ;; Else use format string.
  1593. (t (format fmt value)))))
  1594. ;;;; Verse Block
  1595. (defun org-e-latex-verse-block (verse-block contents info)
  1596. "Transcode a VERSE-BLOCK element from Org to LaTeX.
  1597. CONTENTS is nil. INFO is a plist holding contextual information."
  1598. (org-e-latex--wrap-label
  1599. verse-block
  1600. ;; In a verse environment, add a line break to each newline
  1601. ;; character and change each white space at beginning of a line
  1602. ;; into a space of 1 em. Also change each blank line with
  1603. ;; a vertical space of 1 em.
  1604. (progn
  1605. (setq contents (replace-regexp-in-string
  1606. "^ *\\\\\\\\$" "\\\\vspace*{1em}"
  1607. (replace-regexp-in-string
  1608. "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
  1609. (org-remove-indentation
  1610. (org-export-secondary-string
  1611. (org-element-get-property :value verse-block)
  1612. 'e-latex info)))))
  1613. (while (string-match "^[ \t]+" contents)
  1614. (let ((new-str (format "\\hspace*{%dem}"
  1615. (length (match-string 0 contents)))))
  1616. (setq contents (replace-match new-str nil t contents))))
  1617. (format "\\begin{verse}\n%s\\end{verse}" contents))))
  1618. ;;; Compilation
  1619. (defun org-e-latex-compile (texfile)
  1620. "Compile a TeX file.
  1621. TEXFILE is the name of the file being compiled. Processing is
  1622. done through the command specified in `org-e-latex-pdf-process'.
  1623. Return PDF file name or an error if it couldn't be produced."
  1624. (let* ((wconfig (current-window-configuration))
  1625. (texfile (file-truename texfile))
  1626. (base (file-name-sans-extension texfile))
  1627. errors)
  1628. (message (format "Processing LaTeX file " texfile "..." texfile))
  1629. (unwind-protect
  1630. (progn
  1631. (cond
  1632. ;; A function is provided: Apply it.
  1633. ((functionp org-latex-to-pdf-process)
  1634. (funcall org-latex-to-pdf-process (shell-quote-argument texfile)))
  1635. ;; A list is provided: Replace %b, %f and %o with appropriate
  1636. ;; values in each command before applying it. Output is
  1637. ;; redirected to "*Org PDF LaTeX Output*" buffer.
  1638. ((consp org-e-latex-pdf-process)
  1639. (let* ((out-dir (or (file-name-directory texfile) "./"))
  1640. (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
  1641. (mapc
  1642. (lambda (command)
  1643. (shell-command
  1644. (replace-regexp-in-string
  1645. "%b" (shell-quote-argument base)
  1646. (replace-regexp-in-string
  1647. "%f" (shell-quote-argument texfile)
  1648. (replace-regexp-in-string
  1649. "%o" (shell-quote-argument out-dir) command)))
  1650. outbuf))
  1651. org-e-latex-pdf-process)
  1652. ;; Collect standard errors from output buffer.
  1653. (setq errors (org-e-latex-collect-errors outbuf))))
  1654. (t (error "No valid command to process to PDF")))
  1655. (let ((pdffile (concat base ".pdf")))
  1656. ;; Check for process failure. Provide collected errors if
  1657. ;; possible.
  1658. (if (not (file-exists-p pdffile))
  1659. (error (concat (format "PDF file %s wasn't produced" pdffile)
  1660. (when errors (concat ": " errors))))
  1661. ;; Else remove log files, when specified, and signal end of
  1662. ;; process to user, along with any error encountered.
  1663. (when org-e-latex-remove-logfiles
  1664. (dolist (ext org-e-latex-logfiles-extensions)
  1665. (let ((file (concat base "." ext)))
  1666. (when (file-exists-p file) (delete-file file)))))
  1667. (message (concat "Process completed"
  1668. (if (not errors) "."
  1669. (concat " with errors: " errors)))))
  1670. ;; Return output file name.
  1671. pdffile))
  1672. (set-window-configuration wconfig))))
  1673. (defun org-e-latex-collect-errors (buffer)
  1674. "Collect some kind of errors from \"pdflatex\" command output.
  1675. BUFFER is the buffer containing output.
  1676. Return collected error types as a string, or nil if there was
  1677. none."
  1678. (with-current-buffer buffer
  1679. (save-excursion
  1680. (goto-char (point-max))
  1681. ;; Find final "pdflatex" run.
  1682. (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t)
  1683. (let ((case-fold-search t)
  1684. (errors ""))
  1685. (when (save-excursion
  1686. (re-search-forward "Reference.*?undefined" nil t))
  1687. (setq errors (concat errors " [undefined reference]")))
  1688. (when (save-excursion
  1689. (re-search-forward "Citation.*?undefined" nil t))
  1690. (setq errors (concat errors " [undefined citation]")))
  1691. (when (save-excursion
  1692. (re-search-forward "Undefined control sequence" nil t))
  1693. (setq errors (concat errors " [undefined control sequence]")))
  1694. (when (save-excursion
  1695. (re-search-forward "^! LaTeX.*?Error" nil t))
  1696. (setq errors (concat errors " [LaTeX error]")))
  1697. (when (save-excursion
  1698. (re-search-forward "^! Package.*?Error" nil t))
  1699. (setq errors (concat errors " [package error]")))
  1700. (and (org-string-nw-p errors) (org-trim errors)))))))
  1701. (provide 'org-e-latex)
  1702. ;;; org-e-latex.el ends here