5

I'm trying to figure out how to insert a resize command inside a table environment in org-mode that exports to latex,

Here is my minimal example of raw latex source vs org-mode tables:

#+LATEX_CLASS: article
#+LATEX_HEADER: \usepackage{graphicx}


#+begin_src latex :exports results :results latex
\begin{table}
  \caption{a table that scales to the linewidth}
  \centering
  \resizebox{\linewidth}{!}{%
  \begin{tabular}{ll}
    A & 1\\
    B & 2\\
  \end{tabular}}
  \label{tab:scaled}
\end{table}
#+end_src

How can I make org-mode tables with a \resizebox{\linewidth}{!}{% } command so that the same result is achieved for the table below? (Or alternatively something else that gives the same result).

#+LATEX_CLASS: article
#+LATEX_HEADER: \usepackage{graphicx}
#+CAPTION: a table that does not scale to the linewidth
#+LABEL: tab:uncaled
| A | 1 |
| B | 2 |

My attempt based on the LaTeX-specific-attributes from org-mode manual

#+LATEX_CLASS: article
#+LATEX_HEADER: \usepackage{graphicx}
#+CAPTION: a table that does not scale to the linewidth
#+ATTR_LATEX: :math-prefix \resizebox{\linewidth}{!}{% :math-suffix }
| A | 1 |
| B | 2 |

However the things I've tried has not worked. In the answer provided to the question on tex.stackechange another alternative is presented, which requires insertion of another environment between table and tabular.

#+LATEX_CLASS: article
#+LATEX_HEADER: \usepackage{graphicx}
#+LATEX_HEADER: \usepackage{adjustbox}    
#+begin_src latex :exports results :results latex
\begin{table}
  \caption{a table that scales to the linewidth}
  \centering
  \begin{adjustbox}{width=\textwidth}
    \begin{tabular}{ll}
      A & 1\\
      B & 2\\
    \end{tabular}
  \end{adjustbox}
  \label{tab:scaled}
\end{table}
#+end_src
6

After some investigation i figured out a solution.

Not sure why I didn't see it before or if table export changed in org-mode at some point.

org-mode export tables default as

\begin{center}
\begin{tabular}{lr}
A & 1\\
B & 2\\
\end{tabular}
\end{center}

if the caption and/or label is not specified. Now a table environment can be added around the tabular environment with a caption and label.

#+begin_table
#+LATEX: \caption{Description of table.}
#+LATEX: \label{tab:my-table}
#+LATEX: \centering
#+LATEX: \adjustbox{max width=\linewidth}{
#+ATTR_LATEX: :center nil
| A | 1 |
| B | 2 |
#+end_table

Notice the attr_latex: :center nil. The adjustbox does not play well with \begin{center}...\end{center}.

The specific command I use here only shrinks the table, however any command can be inserted now.

0

A late reply but may I suggest another solution ? Instead of resizing the table, you may want to split the lines when they exceed a given limit, for example textwidth :

#+caption: Your table
#+ATTR_LATEX: :environment tabularx :width \textwidth :align XX
| A | 1 |
| B | 2 |

Pros : less code ! It also avoids very small, unreadable tables.

Cons : You have to specify the number of columns in the :align flag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.