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

在此輸入影像描述

相關內容