自动选择表格的页面方向

自动选择表格的页面方向

我正在尝试编写一个 Python 脚本,该脚本接受一个矩阵并将其转换为仅包含一个表格的 Latex 文档。我已经整理好了大部分内容,并且可以得到例如如下所示的表格 TeX 文件输出:

\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}

我希望我的表格根据其纵横比以横向或纵向排版(在上面的例子中应该是纵向)。这在 Python 方面很难做到,因为 Python 不知道 LaTeX 将使用什么字体、间距等。有没有办法通过 LaTeX 代码指定这种依赖于内容的页面方向?

答案1

这是一个解决方案。我们使用一个保存框来测量它的宽度,我测试它是否更大,然后\textwidth你可以用任何值替换它。如果你使用Koma-script类,你可以很容易地改变方向。

更新文本宽度已调整。我们尝试

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

使文本居中

\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}

相关内容