自动调整表格的列宽

自动调整表格的列宽

我是 Latex 的初学者。我使用 Lyx 创建表格,然后导出脚本pdflatex,然后将其粘贴到 Overleaf 中。我正在尝试创建如下表格。

在此处输入图片描述

如您所见,第 4、5 和 8 列的列宽不必要地宽,这使我的表格几乎到达了页面的边缘。我使用了嵌套表格,这样列宽就减小了,但似乎没有任何效果。

这是pdflatex从 Lyx 导出的。

%% LyX 2.3.6.1 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{array}

\makeatletter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
%% Because html converters don't know tabularnewline
\providecommand{\tabularnewline}{\\}

\makeatother

\usepackage{babel}
\begin{document}

\begin{table}

\caption{Problem 1: Summary of parameters and relative $L^{2}$ error for different
PINN frameworks\label{tab:Problem-1:-Summary}}

\begin{centering}
\begin{tabular}{|c|c|c|c|c|c|c|c|}
\hline 
 & Layers & Nodes & %
\begin{tabular}{c}
Boundary\tabularnewline
points\tabularnewline
\end{tabular} & %
\begin{tabular}{c}
Collocation\tabularnewline
points\tabularnewline
\end{tabular} & $\alpha$ & Iterations & %
\begin{tabular}{c}
Relative $L^{2}$\tabularnewline
error\tabularnewline
\end{tabular}\tabularnewline
\hline 
Model 1 & 8 & 20 & 612 & 5800 & 1e-3 & 30k & 0.13599\tabularnewline
\hline 
Model 2 & 8 & 20 & 800 & 2500 & 1e-3 & 30k & NA\tabularnewline
\hline 
Model 3 & 6 & 512 & 4000 & 4000 & 1e-3 & 20k & 0.08931\tabularnewline
\hline 
Model 4 & 6 & 512 & 4000 & 4000 & 1e-3 & 10k & 0.10124\tabularnewline
\hline 
Model 5 & 6 & 512 & 4000 & 4000 & 1e-3 & 10k & 0.09557\tabularnewline
\hline 
Model 6 & 6 & 512 & 4000 & 4000 & 1e-3 & 20k & 0.06448\tabularnewline
\hline 
Model 7 & 6 & 512 & 4000 & 4000 & 1e-3 & 20k & 0.06433\tabularnewline
\hline 
Model 8 & 6 & 512 & 4000 & 4000 & 1e-3 & 20k & 0.18741\tabularnewline
\hline 
Model 9 & 6 & 512 & 4000 & 4000 & 2e-5 & 11k & 0.12640\tabularnewline
\hline 
Model 10 & 6 & 512 & 4000 & 4000 & 1e-3 & 20k & 0.17577\tabularnewline
\hline 
Model 11 & 6 & 512 & 4000 & 4000 & 1e-3 & 30k & 0.08229\tabularnewline
\hline 
\end{tabular}
\par\end{centering}
\begin{raggedright}
{*} From Model 3 to Model 11, the boundary points and collocation
points are given for per batch.
\par\end{raggedright}
\end{table}
\end{document}

我知道我可以手动设置列宽(以厘米或英寸为单位)。难道没有自动设置列宽的方法吗?我有多个表格,我不想手动修改它们。

编辑 1:以下是 Lyx 中的选项。 在此处输入图片描述

答案1

问题出在嵌套表格的序言(列标题)。您可以用 替换每个{c}表格{@{]c@{}},或者更简单地使用makecell包及其同名命令。此外,表格对于默认文本宽度来说太宽,所以我建议加载geometry。此外,我加载了 caption 以使标题和表格之间的间距更合理。

        \documentclass[english]{article}
        \usepackage[T1]{fontenc}
        \usepackage[latin9]{inputenc}
        \usepackage[showframe]{geometry}
        \usepackage{array, caption}
        \usepackage{makecell}
        \makeatletter

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
        %% Because html converters don't know tabularnewline
        \providecommand{\tabularnewline}{\\}

        \makeatother
        \usepackage{babel}

        \begin{document}

        \begin{table}

        \caption{Problem 1: Summary of parameters and relative $L^{2}$ error for different
        PINN frameworks\label{tab:Problem-1:-Summary}}


        \centering
        \begin{tabular}{|c|c|c|c|c|c|c|c|}
        \hline
         & Layers & Nodes & %
        \begin{tabular}{@{}c@{}}
        Boundary\tabularnewline
        points\tabularnewline
        \end{tabular} & %
        \begin{tabular}{@{}c@{}}
        Collocation\tabularnewline
        points\tabularnewline
        \end{tabular} & $\alpha$ & Iterations & %
        \begin{tabular}{@{}c@{}}
        Relative $L^{2}$\tabularnewline
        error\tabularnewline
        \end{tabular}\tabularnewline
        \hline
        Model 1 & 8 & 20 & 612 & 5800 & 1e-3 & 30k & 0.13599\tabularnewline
        \hline
        Model 2 & 8 & 20 & 800 & 2500 & 1e-3 & 30k & NA\tabularnewline
        \hline
        Model 3 & 6 & 512 & 4000 & 4000 & 1e-3 & 20k & 0.08931\tabularnewline
        \hline
        Model 4 & 6 & 512 & 4000 & 4000 & 1e-3 & 10k & 0.10124\tabularnewline
        \hline
        Model 5 & 6 & 512 & 4000 & 4000 & 1e-3 & 10k & 0.09557\tabularnewline
        \hline
        Model 6 & 6 & 512 & 4000 & 4000 & 1e-3 & 20k & 0.06448\tabularnewline
        \hline
        Model 7 & 6 & 512 & 4000 & 4000 & 1e-3 & 20k & 0.06433\tabularnewline
        \hline
        Model 8 & 6 & 512 & 4000 & 4000 & 1e-3 & 20k & 0.18741\tabularnewline
        \hline
        Model 9 & 6 & 512 & 4000 & 4000 & 2e-5 & 11k & 0.12640\tabularnewline
        \hline
        Model 10 & 6 & 512 & 4000 & 4000 & 1e-3 & 20k & 0.17577\tabularnewline
        \hline
        Model 11 & 6 & 512 & 4000 & 4000 & 1e-3 & 30k & 0.08229\tabularnewline
        \hline
        \end{tabular}
        \vskip 3ex
        \begin{tabular}{|c|c|c|c|c|c|c|c|}
        \hline
         & Layers & Nodes & %
        \begin{tabular}{@{}c@{}}
        Boundary\tabularnewline
        points\tabularnewline
        \end{tabular} & %
        \begin{tabular}{@{}c@{}}
        Collocation\tabularnewline
        points\tabularnewline
        \end{tabular} & $\alpha$ & Iterations & %
        \begin{tabular}{@{}c@{}}
        Relative $L^{2}$\tabularnewline
        error\tabularnewline
        \end{tabular}\tabularnewline
        \hline
        Model 1 & 8 & 20 & 612 & 5800 & 1e-3 & 30k & 0.13599\tabularnewline
        \hline
        Model 2 & 8 & 20 & 800 & 2500 & 1e-3 & 30k & NA\tabularnewline
        \hline
        Model 3 & 6 & 512 & 4000 & 4000 & 1e-3 & 20k & 0.08931\tabularnewline
        \hline
        Model 4 & 6 & 512 & 4000 & 4000 & 1e-3 & 10k & 0.10124\tabularnewline
        \hline
        Model 5 & 6 & 512 & 4000 & 4000 & 1e-3 & 10k & 0.09557\tabularnewline
        \hline
        Model 6 & 6 & 512 & 4000 & 4000 & 1e-3 & 20k & 0.06448\tabularnewline
        \hline
        Model 7 & 6 & 512 & 4000 & 4000 & 1e-3 & 20k & 0.06433\tabularnewline
        \hline
        Model 8 & 6 & 512 & 4000 & 4000 & 1e-3 & 20k & 0.18741\tabularnewline
        \hline
        Model 9 & 6 & 512 & 4000 & 4000 & 2e-5 & 11k & 0.12640\tabularnewline
        \hline
        Model 10 & 6 & 512 & 4000 & 4000 & 1e-3 & 20k & 0.17577\tabularnewline
        \hline
        Model 11 & 6 & 512 & 4000 & 4000 & 1e-3 & 30k & 0.08229\tabularnewline
        \hline
        \end{tabular}
        \end{table}

        \end{document} 

在此处输入图片描述

答案2

我不熟悉LyX和转换为html,所以我不知道建议的解决方案是否在那里有效。无论如何,使用tabularx表格和siunitx第六列()中的当前数字\alpha,代码很简单,表格看起来不错:

在此处输入图片描述

\documentclass[english]{article}
\usepackage{geometry}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\usepackage[T1]{fontenc}
%\usepackage[latin9]{inputenc}
\usepackage{babel}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\usepackage{siunitx}
\usepackage[skip=0.33\baselineskip]{caption}

\makeatletter
%% LyX specific LaTeX commands.
%% Because html converters don't know tabularnewline
\providecommand{\tabularnewline}{\\}
\makeatother

\begin{document}
    \begin{table}
\caption{Problem 1: Summary of parameters and relative $L^{2}$ error for different PINN frameworks}
\label{tab:Problem-1:-Summary}

    \centering
    \setlength\tabcolsep{4pt}
    \renewcommand\arraystretch{1.2}
\begin{tabularx}{\linewidth}{|C|C|C|C|C|S[table-format=1e2]|C|C|}
    \hline
    & Layers
        & Nodes
            & Boundary points
                &  Collocation points
                    & {$\alpha$}
                        & Iterations
                            & Relative $L^{2}$ error   \\
    \hline
Model 1 & 8 & 20 & 612 & 5800 & 1e-3 & 30k & 0.13599    \\
    \hline
Model 2 & 8 & 20 & 800 & 2500 & 1e-3 & 30k & NA\tabularnewline
\hline
Model 3 & 6 & 512 & 4000 & 4000 & 1e-3 & 20k & 0.08931\tabularnewline
\hline
Model 4 & 6 & 512 & 4000 & 4000 & 1e-3 & 10k & 0.10124\tabularnewline
\hline
Model 5 & 6 & 512 & 4000 & 4000 & 1e-3 & 10k & 0.09557\tabularnewline
\hline
Model 6 & 6 & 512 & 4000 & 4000 & 1e-3 & 20k & 0.06448\tabularnewline
\hline
Model 7 & 6 & 512 & 4000 & 4000 & 1e-3 & 20k & 0.06433\tabularnewline
\hline
Model 8 & 6 & 512 & 4000 & 4000 & 1e-3 & 20k & 0.18741\tabularnewline
\hline
Model 9 & 6 & 512 & 4000 & 4000 & 2e-5 & 11k & 0.12640\tabularnewline
\hline
Model 10 & 6 & 512 & 4000 & 4000 & 1e-3 & 20k & 0.17577\tabularnewline
\hline
Model 11 & 6 & 512 & 4000 & 4000 & 1e-3 & 30k & 0.08229\tabularnewline
\hline
\end{tabularx}
\begin{raggedright}
{*} From Model 3 to Model 11, the boundary points and collocation
points are given for per batch.
\par\end{raggedright}
\end{table}
\end{document}

相关内容