使用 \textwidth 和 tabulary 后表格仍然太宽

使用 \textwidth 和 tabulary 后表格仍然太宽

大家好,

我遇到的问题是,某些表格(例如我将在此处引用的表格)超出了文本宽度,尽管我使用了带有制表符的选项 \textwidth。

该表有以下代码:

\usepackage{tabulary}
\usepackage{tabularx}
\begin{table}[htbp]
   \centering
  \caption{Categorization of cantons}
  \scriptsize
    \begin{tabulary}{\textwidth}{|L|L|L|L|L|}
    \hline
    \multicolumn{1}{|c|}{\textbf{Central Switzerland (C)}} & \multicolumn{1}{c|}{\textbf{High Alps (HA)}} & \multicolumn{1}{c|}{\textbf{Northeast Switzerland (NE)}} & \multicolumn{1}{c|}{\textbf{Northwest Switzerland (NW)}} & \multicolumn{1}{c|}{\textbf{West Switzerland (W)}} \bigstrut\\
    \hline
    Lucerne (LU) & Grisons (GR) & Glarus (GL) & Zurich (ZH) & Fribourg (FR) \bigstrut[t]\\
    Central Switzerland (C) & Tessin (TI) & Schaffhausen (SH) & Solothurn (SO) & Vaud (VD) \\
    & Valais (VS) & Appenzell (AP) & Basel-Stadt (BS) & Neuchâtel (NE) \\
     &       & St. Gallen (SG) & Basel-Land (BL) & Geneva (GE) \\
     &       & Thurgau (TG) & Aargau (AG) &  \bigstrut[b]\\
    \hline
    \end{tabulary}%
    \vspace{5pt}
    \captionsetup{font={scriptsize}}
    \caption*{Source: Eidgenössischer Turnverein (1869), p. 67-69, own illustration}
  \label{tab:addlabel}%
\end{table}%

这是表格的图片: 表格太宽
如何让表格的宽度自动适应文本宽度?我不介意第一行(例如瑞士中部、阿尔卑斯高地等)是否必须压缩成两行,只要它能让表格适合文本宽度即可。有没有一个命令可以做到这一点,并且我可以轻松地将其用于未来的表格?

非常感谢您的帮助!

答案1

请始终发布显示所有使用软件包的完整文档。我猜您的代码中有几个命令未由软件包定义,因此我在此处删除了它们。主要问题是您覆盖了不允许换行的L说明符c,因此强制所有条目都太宽。

\documentclass[a4paper]{article}
\usepackage{tabulary}
\begin{document}

\begin{table}[htbp]
   \centering
  \caption{Categorization of cantons}
  \scriptsize
    \begin{tabulary}{\textwidth}{|L|L|L|L|L|}
    \hline
    \centering\textbf{Central Switzerland (C)} & 
\centering\textbf{High Alps (HA)} & 
\centering\textbf{Northeast Switzerland (NE)} & 
\centering\textbf{Northwest Switzerland (NW)} &
\centering\textbf{West Switzerland (W)}\tabularnewline
    \hline
    Lucerne (LU) & Grisons (GR) & Glarus (GL) & Zurich (ZH) & Fribourg (FR) \\
    Central Switzerland (C) & Tessin (TI) & Schaffhausen (SH) & Solothurn (SO) & Vaud (VD) \\
    & Valais (VS) & Appenzell (AP) & Basel-Stadt (BS) & Neuchâtel (NE) \\
     &       & St. Gallen (SG) & Basel-Land (BL) & Geneva (GE) \\
     &       & Thurgau (TG) & Aargau (AG) &  \\
    \hline
    \end{tabulary}%
    \vspace{5pt}
%    \captionsetup{font={scriptsize}}
    \caption{Source: Eidgenössischer Turnverein (1869), p. 67-69, own illustration}
  \label{tab:addlabel}%
\end{table}%

\end{document}

答案2

如果您希望所有列具有相同的宽度并使用文本的最大宽度,则应按照tabularx以下步骤操作:

% arara: pdflatex

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[english]{babel}
\usepackage{caption}
\usepackage{booktabs, tabularx, threeparttable}
\usepackage{ragged2e, array}
\newcolumntype{Z}{>{\raggedright\arraybackslash}X}
\usepackage{microtype}

\begin{document}
\begin{table}[htbp]
    \centering
    \begin{threeparttable}
    \caption{Categorization of cantons}
    \begin{tabularx}{\textwidth}{*{5}{Z}}
        \toprule
        \textbf{Central Switzerland~(C)} &\textbf{High Alps~(HA)} & \textbf{Northeast Switzerland~(NE)} & \textbf{Northwest Switzerland~(NW)} & \textbf{West Switzerland~(W)} \\
        \midrule
        Lucerne~(LU) & Grisons~(GR) & Glarus~(GL) & Zurich~(ZH) & Fribourg~(FR) \\
        Central Switzerland~(C) & Tessin~(TI) & Schaff\-hau\-sen~(SH) & So\-lo\-thurn~(SO) & Vaud~(VD) \\
        & Valais (VS) & Appen\-zell~(AP) & Basel-Stadt (BS) & Neu\-châ\-tel~(NE) \\
        &       & St.~Gal\-len~(SG) & Basel-Land (BL) & Geneva~(GE) \\
        &       & Thurgau~(TG) & Aargau~(AG) & \\
        \bottomrule
    \end{tabularx}
    \begin{tablenotes}
    \item Source: Eidgenössischer Turnverein (1869), p.~67-69, own illustration
    \end{tablenotes}
    \end{threeparttable}
    \label{tab:addlabel}%
\end{table}%
\end{document}

在此处输入图片描述

相关内容