強製表格寬度為文字列寬度

強製表格寬度為文字列寬度

似乎是一個簡單的問題,但我確實是 LaTeX 的新手,即使我在這個論壇上找到了一些 QA,也無法想出解決方案。你能幫忙嗎?如圖所示,表格比文字列寬,並且與其他列發生碰撞。我知道表格中的文字太長,但是有沒有一種簡單的方法可以強製表格與文字寬度相同並將文字換行成同一表格行中的兩行而不使用不同的包?在此輸入影像描述

\usepackage[table]{hypcap}
\begin{table}[ht]
    \centering
    \renewcommand{\arraystretch}{1.8}
    \begin{tabular}{c|c|c|l}
        Parameter & Value & Units & Description\\
\toprule \midrule
        E & 200 & GPa & Young's modulus\\
        $f_{c}$ & 30 & MPa & Ultimate compressive strength\\
        $\varepsilon_{t0}$ & 805 $\cdot 10^{-4}$ & - & Initial threshold of damage for $\varepsilon_t$\\ 
\hline
    \end{tabular}
    \caption{a}
\end{table}

答案1

如果沒有不同的包,我不知道如何自動執行此操作(但我已經添加了缺少的包書本標籤從您的程式碼中以便使用\toprule\midrule從您的程式碼)。

你可以使用p{<length>}列規範為最後一列指定寬度。您需要透過反覆試驗找到長度值(在這段程式碼中,我將尺寸從 2 公分擴大到獲得溢出的水平盒警告;在這個特定的範例中,我收到了有關 value 的警告2.5cm,但沒有收到有關 value 的警告2.49cm

我已經刪除了hypcap包,此處不相關,並添加利蘇姆用於用文字填滿頁面。

在此程式碼中,我還在文件類別中明確添加了letterpaper紙張格式,因為類型列使用的值p取決於頁面的寬度,並且預設情況下,對於某些歐洲用戶,它是a4paper沒有紙張格式時的格式明確給出。

\documentclass[10pt,twocolumn]{article}
\usepackage{lipsum}
\usepackage{booktabs}
\begin{document}
\lipsum[1-7]

\begin{table}[ht]
    \centering
    \renewcommand{\arraystretch}{1.8}
    \begin{tabular}{c|c|c|p{2.49cm}}
        Parameter & Value & Units & Description\\
\toprule \midrule
        E & 200 & GPa & Young's modulus\\
        $f_{c}$ & 30 & MPa & Ultimate compressive strength\\
        $\varepsilon_{t0}$ & 805 $\cdot 10^{-4}$ & - & Initial threshold of damage for $\varepsilon_t$\\ 
\hline
    \end{tabular}
    \caption{a}
\end{table}

\lipsum[8-14]
\end{document}

使用原始{c|c|c|l}表格規範: 在此輸入影像描述

使用{c|c|c|p{2.49cm}}表格規範: 在此輸入影像描述

答案2

  • 歡迎來到 TeX:SE!
  • 請下次提供程式碼片段 MWE(最小工作範例)。現在我們必須猜測文檔類別和(由它)定義的頁面佈局。兩者都會影響表格格式
  • 對於您的表,我將使用帶有tabularray庫/包的包booktabssiunitx並且ragged2e
\documentclass[twocolumn]{article}
\usepackage{lipsum}

\usepackage{ragged2e}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx}

\begin{document}
\lipsum[1]

    \begin{table}[ht]
\begin{tblr}{
             colsep=3pt,
             colspec={@{} c Q[c,si={table-format=3.0}] Q[c,si] X[j, appto=\RaggedRight] @{}},
             cell{3-Z}{1} = {mode=math},
             cell{1,Z}{2,3}  = {guard}
             }
    \toprule
Parameter   & Value & Units & Description\\
    \midrule
E           & 200   & \unit{\giga\pascal} 
                            & Young's modulus\\
f_{c}       & 30    & \unit{\mega\pascal} 
                            & Ultimate compressive strength\\
\varepsilon_{t_0}
            & \num{805E-4}
                    & --    & Initial threshold of damage for $\varepsilon_t$   \\
    \bottomrule
\end{tblr}
\caption{a}
\label{tab:?}
    \end{table}
\lipsum[2-6]
\end{document}

在此輸入影像描述

相關內容