Implement longtable support for LaTeX export.

Exporting tables to LaTeX suffered from the problem that Org tables
are often long, but that the tabular environment in LaTeX cannot
handle long tables.  This patch fixes this issue.

LaTeX export can now deal with very long tables, by setting an option
in the "ATTR_LaTeX:" line.  Also, you can specify the alignment by
hand.  Here is an example:

Also, table export now correctly deals with tables that start with a
hline.  In such a case, also the LaTeX version will have this extra
hline before the first line.
This commit is contained in:
Carsten Dominik 2008-12-15 21:28:02 +01:00
parent 45cc93478d
commit 0ea9fb46c5
6 changed files with 100 additions and 28 deletions

View File

@ -44,6 +44,7 @@ link.
- #+CAPTION: :: The caption of the image or table. This string
should be processed according to the export backend, but
this is not yet done.
- #+LABEL: :: A label to identify the figure/table for cross
references. For HTML export, this string will become the
ID for the ~<div class="figure">~ element that encapsulates
@ -51,16 +52,23 @@ link.
string will be used as the argument of a ~\label{...}~
macro. These labels will be available for internal links
like ~[[label][Table] ]~.
- #+ATTR_HTML: :: Attributes for HTML export of image, to be
added as attributes into the ~<img...>~ tag. This string
will not be processed, so it should have immediately the
right format.
- #+ATTR_LaTeX: :: Attributes for LaTeX export of images, to
be inserted into the optional argument of the
~\includegraphics[...]{file}~ command, to specify scaling,
clipping and other options. This string will not be
processed, so it should have immediately the right
format, like =width=5cm,angle=90=
- #+ATTR_LaTeX: :: Attributes for LaTeX export of images and
tables.\\
For /images/, this string is directly inserted into
the optional argument of the ~\includegraphics[...]{file}~
command, to specify scaling, clipping and other options.
This string will not be processed, so it should have
immediately the right format, like =width=5cm,angle=90=.\\
For /tables/, this can currently only be the keyword
=longtable=, to request typesetting of the table using the
longtable package. This LaTeX package will automatically
distribute the table over several pages if needed.
For LaTeX export, if either a caption or a label is given, the
element will be exported as a float, i.e. wrapped into a figure

View File

@ -1,3 +1,7 @@
2008-12-15 Carsten Dominik <carsten.dominik@gmail.com>
* org.texi (Tables in LaTeX export): New section.
2008-12-10 Carsten Dominik <carsten.dominik@gmail.com>
* org.texi (Images in LaTeX export): New section.

View File

@ -328,6 +328,7 @@ LaTeX and PDF export
* LaTeX/PDF export commands:: Which key invode which commands
* Quoting LaTeX code:: Incorporating literal LaTeX code
* Sectioning structure:: Changing sectioning in LaTeX output
* Tables in LaTeX export:: Options for exporting tables to LaTeX
* Images in LaTeX export:: How to insert figures into LaTeX output
Publishing
@ -7748,7 +7749,7 @@ files while publishing them to a publishing directory see @ref{Publishing
links}.
If you want to specify attributes for links, you can do so using a special
@code{#+ATTR_HTML} line to defin attributes that wil be added to the
@code{#+ATTR_HTML} line to define attributes that wil be added to the
@code{<a>} or @code{<img>} tags. Here is an example that sets @code{alt} and
@code{title} attributes for an inlined image:
@ -7897,6 +7898,7 @@ references, the PDF output file will be fully linked.
* LaTeX/PDF export commands:: Which key invode which commands
* Quoting LaTeX code:: Incorporating literal LaTeX code
* Sectioning structure:: Changing sectioning in LaTeX output
* Tables in LaTeX export:: Options for exporting tables to LaTeX
* Images in LaTeX export:: How to insert figures into LaTeX output
@end menu
@ -7974,7 +7976,7 @@ All lines between these markers are exported literally
#+END_LaTeX
@end example
@node Sectioning structure, Images in LaTeX export, Quoting LaTeX code, LaTeX and PDF export
@node Sectioning structure, Tables in LaTeX export, Quoting LaTeX code, LaTeX and PDF export
@subsection Sectioning structure
@cindex LaTeX class
@cindex LaTeX sectioning structure
@ -7988,7 +7990,25 @@ You can change this globally by setting a different value for
structure for each class, as well as defining additonal classes.
@node Images in LaTeX export, , Sectioning structure, LaTeX and PDF export
@node Tables in LaTeX export, Images in LaTeX export, Sectioning structure, LaTeX and PDF export
@subsection Tables in LaTeX export
@cindex tables, in LaTeX export
For LaTeX export of a table, you can specify a label and a caption
(@pxref{Tables exported}). You can also use the @code{ATTR_LaTeX} line to
request a longtable environment for the table, so that it may span several
pages:
@example
#+CAPTION: A long table
#+LABEL: tbl:long
#+ATTR_LaTeX: longtable
| ..... | ..... |
| ..... | ..... |
@end example
@node Images in LaTeX export, , Tables in LaTeX export, LaTeX and PDF export
@subsection Images in LaTeX export
@cindex images, inline in LaTeX
@cindex inlining images in LaTeX
@ -7997,7 +8017,7 @@ Images that are linked to without a description part in the link, like
@samp{[[file:img.jpg]]} or @samp{[[./img.jpg]]} will be inserted into the PDF
output files resulting from LaTeX output. Org will use an
@code{\includegraphics} macro to insert the image. If you have specified a
caption and/or a label as described in @ref{Inlined images}, the figure will
caption and/or a label as described in @ref{Markup rules}, the figure will
be wrappend into a @code{figure} environment and thus become a floating
element. Finally, you can use an @code{#+ATTR_LaTeX:} line to specify the
options that can be used in the optional argument of the

View File

@ -7,6 +7,14 @@
2008-12-15 Carsten Dominik <carsten.dominik@gmail.com>
* org-export-latex.el (org-export-latex-classes): Add longable as
a default package to all classes.
(org-export-latex-tables): Handle the longtable attribute and the
align attribute.
* org-table.el (orgtbl-to-generic): Handle tables that start with
a hline.
* org-export-latex.el (org-export-latex-emphasis-alist): Switch to
\verb for colde-like snippets.
(org-export-as-latex): Fix issues with region export.

View File

@ -91,6 +91,7 @@
\\usepackage[utf8]{inputenc}
\\usepackage[T1]{fontenc}
\\usepackage{graphicx}
\\usepackage{longtable}
\\usepackage{hyperref}"
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
@ -102,6 +103,7 @@
\\usepackage[utf8]{inputenc}
\\usepackage[T1]{fontenc}
\\usepackage{graphicx}
\\usepackage{longtable}
\\usepackage{hyperref}"
("\\part{%s}" . "\\part*{%s}")
("\\chapter{%s}" . "\\chapter*{%s}")
@ -113,6 +115,7 @@
\\usepackage[utf8]{inputenc}
\\usepackage[T1]{fontenc}
\\usepackage{graphicx}
\\usepackage{longtable}
\\usepackage{hyperref}"
("\\part{%s}" . "\\part*{%s}")
("\\chapter{%s}" . "\\chapter*{%s}")
@ -1034,7 +1037,7 @@ If TIMESTAMPS, convert timestamps, otherwise delete them."
(end (org-table-end))
(raw-table (buffer-substring beg end))
fnum fields line lines olines gr colgropen line-fmt align
caption label attr floatp)
caption label attr floatp longtblp)
(if org-export-latex-tables-verbatim
(let* ((tbl (concat "\\begin{verbatim}\n" raw-table
"\\end{verbatim}\n")))
@ -1046,7 +1049,12 @@ If TIMESTAMPS, convert timestamps, otherwise delete them."
attr (org-find-text-property-in-string
'org-attributes raw-table)
label (org-find-text-property-in-string
'org-attributes raw-table)
'org-label raw-table)
longtblp (and attr (stringp attr)
(string-match "\\<longtable\\>" attr))
align (and attr (stringp attr)
(string-match "\\<align=\\([^ \t\n\r,]+\\)" attr)
(match-string 1 attr))
floatp (or caption label))
(setq lines (split-string raw-table "\n" t))
(apply 'delete-region (list beg end))
@ -1081,10 +1089,11 @@ If TIMESTAMPS, convert timestamps, otherwise delete them."
(string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
(setq line-fmt (match-string 2 line-fmt)))
;; format alignment
(setq align (apply 'format
(cons line-fmt
(mapcar (lambda (x) (if x "r" "l"))
org-table-last-alignment))))
(unless align
(setq align (apply 'format
(cons line-fmt
(mapcar (lambda (x) (if x "r" "l"))
org-table-last-alignment)))))
;; prepare the table to send to orgtbl-to-latex
(setq lines
(mapcar
@ -1095,16 +1104,33 @@ If TIMESTAMPS, convert timestamps, otherwise delete them."
(when insert
(insert (org-export-latex-protect-string
(concat
(if floatp "\\begin{table}[htb]\n")
(if floatp (format
"\\caption{%s%s}\n"
(if label (concat "\\\label{" label "}"))
(or caption "")))
"\\begin{center}\n"
(orgtbl-to-latex
lines `(:tstart ,(concat "\\begin{tabular}{" align "}")))
"\n\\end{center}\n"
(if floatp "\\end{table}")))
(if longtblp
(concat "\\begin{longtable}{" align "}\n")
(if floatp "\\begin{table}[htb]\n"))
(if (or floatp longtblp)
(format
"\\caption{%s%s}"
(if label (concat "\\\label{" label "}") "")
(or caption "")))
(if longtblp "\\\\\n" "\n")
(if (not longtblp) "\\begin{center}\n")
(if (not longtblp) (concat "\\begin{tabular}{" align "}\n"))
(orgtbl-to-latex
lines
`(:tstart nil :tend nil
:hlend ,(if longtblp
(format "\\\\
\\hline
\\endhead
\\hline\\multicolumn{%d}{r}{Continued on next page}\\
\\endfoot
\\endlastfoot" (length org-table-last-alignment))
nil)))
(if (not longtblp) (concat "\n\\end{tabular}"))
(if longtblp "\n" "\n\\end{center}\n")
(if longtblp
"\\end{longtable}"
(if floatp "\\end{table}"))))
"\n\n")))))))
(defun org-export-latex-fontify ()

View File

@ -3871,9 +3871,15 @@ directly by `orgtbl-send-table'. See manual."
;; Do we have a heading section? If so, format it and handle the
;; trailing hline.
(if (and (not splicep) (listp (car *orgtbl-table*))
(memq 'hline *orgtbl-table*))
(if (and (not splicep)
(or (consp (car *orgtbl-table*))
(consp (nth 1 *orgtbl-table*)))
(memq 'hline (cdr *orgtbl-table*)))
(progn
(when (eq 'hline (car *orgtbl-table*))
;; there is a hline before the first data line
(and hline (push hline *orgtbl-rtn*))
(pop *orgtbl-table*))
(let* ((*orgtbl-lstart* (or (plist-get params :hlstart)
*orgtbl-lstart*))
(*orgtbl-llstart* (or (plist-get params :hllstart)