0

I am trying to write a python script that takes a matrix and converts it to a latex document containing just one table. I have most things sorted out, and I can get e.g. a table TeX file outputted like this:

\documentclass{article}
\usepackage{booktabs}
\begin{document}
\pagenumbering{gobble}
\begin{table}[htp]
\begin{center}
\label{tab:w2}
\begin{tabular}{cccccccccccccccccccccccccccccccccccc}
\toprule
                {a} & {b}\\
                \midrule
                $1$ & $2$ & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-}\\
                $3$ & $4$ & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-}\\
                $5$ & $6$ & $7$ & $8$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$ & $9$\\             
\bottomrule
\end{tabular}
\end{center}
\end{table}
\end{document}

I would like my table to be typeset in either landscape or portrait orientation depending on its aspect ratio (in the example above it should be portrait). This is difficult to do on the python side, since python does not know what font, spacing, etc LaTeX will use. is there any way to specify such a content-dependent page orientation via the LaTeX code?

  • what about standalone class? – touhami Sep 27 '15 at 5:42
1

Here is a solution. We use a savebox measuring it's width here I test if it is larger then \textwidth you can replace it by any value. If you use Koma-script class you change orientation is very easy.

Updated textwidth adjusted. We try

\addtolength{\textwidth}{-2\oddsidemargin}%
\addtolength{\textwidth}{-2\hoffset}%
\addtolength{\textwidth}{-2in}%

to make text centered

\documentclass{article}
\usepackage{geometry}
 \geometry{
 a4paper,
 total={210mm,297mm},
 left=15mm,
 right=15mm,
 top=15mm,
 bottom=15mm,
 }
\usepackage{booktabs}
\newsavebox\mt
\pagenumbering{gobble}
\begin{document}
\sbox\mt{%
        \begin{tabular}{cccccccccccccccccccccccccccccccccccccc}
        \toprule
            a & b & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-}\\
            1 & 2 & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-} & {-}\\
            3 & 4 & $1$ & $2$ & $3$ & $4$ & $1$ & $2$ & $3$ & $4$ & $4$ & $4$ & $4$ & $4$ & $4$ & $4$ & $4$ & $4$ & $4$ & $4$ & $4$ & $4$ & $4$ & $4$ & $4$ & $4$ & $4$ & $4$ & $4$ & $4$ & $4$ & $4$ & $4$ & $4$ & $4$ & $4$ & $4$ & $4$\\
        \bottomrule
        \end{tabular}%
}
\makeatletter
\ifdim\wd\mt>\textwidth
\setlength\@tempdima   {\paperheight}%
\setlength\paperheight {\paperwidth}%
\setlength\paperwidth  {\@tempdima}%
\setlength\pdfpageheight{\paperheight}%
\setlength\pdfpagewidth{\paperwidth}%
\setlength{\textwidth}{\paperwidth}%
\addtolength{\textwidth}{-2\oddsidemargin}%
\addtolength{\textwidth}{-2\hoffset}%
\addtolength{\textwidth}{-2in}%
\setlength{\hsize}{\textwidth}%
\fi
\makeatother
\begin{table}[htp]\setlength{\hsize}{\textwidth}%
\centering
\usebox\mt
\end{table}
\end{document}
| improve this answer | |
  • Sadly, as soon as the page gets flipped due to the size of the table, the centering no longer seems to work (even if the table is slightly smaller than the space it could take up in landscape orientation). Any idea what I can do about that? – TheChymera Oct 1 '15 at 21:46
  • what do you mean? (table is not centered or caption) – touhami Oct 1 '15 at 22:09
  • please try to compile these two options: portrait and landscape. You will notice that the second one is no longer centered. Any ideas what I could do? – TheChymera Oct 2 '15 at 1:10
  • @TheChymera answer updated. – touhami Oct 2 '15 at 7:26
  • thank you so much for your update - sadly, this does no appear to influence the issue at all. Is, e.g. this code compiling to a centered table on your end? – TheChymera Oct 2 '15 at 9:46

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.