Dings
Dings

Reputation: 103

How to export a org-mode table to LaTeX with the correct placement [h]?

I'm trying to export a table to LaTeX form my org-file which looks like this:

    #+LATEX_HEADER: \usepackage{adjustbox}
    * table test 
    
    #+begin_table 
    #+LATEX: \caption{my caption}
    label:tab:mylabel
    #+ATTR_LATEX: :placement [h]
    #+LATEX: \centering
    #+LATEX: \adjustbox{max width=\linewidth}{
    #+ATTR_LATEX: :center nil
    |    |                          |
    | id | Question                 |
    |----+--------------------------|
    |    |                          |
    |  1 | Does it export with [h]? |
    #+end_table

I want to get the exported TeX to look like this:

    ... 
    \begin{table}[h]
    \caption{my caption}
    \label{tab:mylabel}
    \centering
    \adjustbox{max width=\linewidth}{
    \begin{tabular}{ll}
     & \\
    id & Question\\
    \hline
     & \\
    1 & Does it export with [h]?\\
    \end{tabular}
    \end{table}
    ...

but I'm just getting

    ... 
    \begin{table}
    \caption{my caption}
    \label{tab:mylabel}
    \centering
    \adjustbox{max width=\linewidth}{
    \begin{tabular}{ll}
     & \\
    id & Question\\
    \hline
     & \\
    1 & Does it export with [h]?\\
    \end{tabular}
    \end{table}
    ...

using the following versions

GNU Emacs 25.1.1 (x86_64-apple-darwin15.5.0, NS appkit-1404.47 Version 10.11.5 (Build 15F34)) of 2017-01-06

Org mode version 9.0.5 (release_9.0.5-444-g998576 @ ~/git/org-mode/lisp/)

I also tried

#+ATTR_LATEX: :float t :placement [h]

#+ATTR_LATEX: :center nil :float t :placement [h]

#+ATTR_LATEX: :center nil :placement [h]

#+begin_table :placement [h]

#+begin_table :float t :placement [h]

My search on the internet only found some emails from 2010, when org-latex.el existed. Since it still exists in the documentation I thought it should work somehow.

So can anyone help me? Or leak the magic words I have to feed Google with to find my answer?


Thx @Nick, we're on the right way but sadly not at the end of it.

Well I did not want to break the parsing, it explains some things. I've got some more requirements and things to explain.

At First some of my tables are a bit wider like this:

   | id | Question                                                                                                                  |
   |----+---------------------------------------------------------------------------------------------------------------------------|
   |  1 | Does it export with [h]?                                                                                                  |
   |  2 | I have some tables witch are very wide so What if you have a really wide table which needs to be shrunk to the right size |

The Adjustbox package is the first I found that shrinks the whole table to the correct width. I tried your suggested tabularx but it's not shrinking the contents of the table. Without the shrinking most of my tables look weird.

The second Thing, I'm using org-ref. With your version (which I would prefer when the 2 probs are gone) exports to:

   \caption{\label{tab:org56e3a68}
   My caption}  

I added #+label: tab:my-label as intended in the documentation. Added it before and after #+caption: and #+name: but it does not change anything in the .tex file.

any idea?

Upvotes: 5

Views: 6628

Answers (1)

NickD
NickD

Reputation: 6412

I can do everything you want, except the adjustbox stuff with this:

#+BIND: org-latex-prefer-user-labels t
* table test 


#+caption: My caption
#+name:my-label
#+ATTR_LATEX: :placement [h] :center t
|    |                          |
| id | Question                 |
|----+--------------------------|
|    |                          |
|  1 | Does it export with [h]? |

I don't know what \adjustbox does or how it has to be used and where it has to be placed; however, you can't add #+LATEX: lines in between the #+caption and the table: they apparently break the parsing.

You could add another attribute to the ATTR_LATEX line:

#+ATTR_LATEX: :placement [h] :center t :width \linewidth

but it's not clear that does what you want. Also, I just tested and it does not do anything: that's because the standard environment is tabular which does not take a width argument. The following works:

#+ATTR_LATEX: :environment tabularx :placement [h] :center t :width \linewidth

but you also have to add

#+LATEX_HEADER: \usepackage{tabularx}

at the top of the file.

Upvotes: 8

Related Questions