longtable 和 tabular 的左距不同

longtable 和 tabular 的左距不同

我想在文檔中使用 atabular和 a ,但左邊距未對齊。longtable

\documentclass[letterpaper,11pt]{article}

\usepackage{longtable}

\begin{document}

\begin{tabular}{p{2cm} p{15cm}}
entry 1 & bla bla bla bla bla \\
entry 2 & this is a tabular
\end{tabular}

\begin{longtable}{p{2cm} p{15cm}}
entry 3 & This is a longtable \\
entry 4 & with many more lines \\
entry 5 & but it isn't aligned 
\end{longtable}

\end{document}

這個問題是相關的,也是這個

將上面程式碼中的替換\begin{longtable}{...}\begin{longtable}[l]{...}似乎沒有任何作用。

透過摸索,我發現透過使用\setlength{\LTleft}{18pt}我的邊距看起來相當相等。有沒有更優雅的方式來longtables左對齊tabulars?我可以在不指定數字的情況下對齊表格嗎?

答案1

在此輸入影像描述

正如評論中其他成員所指出的,您設定的寬度大於\textwidth,在這種情況下,tabularx可以透過將第一列設定為p{2cm}並將其餘寬度設為第二列來從總可用寬度中受益。\noindent也是必要的tabularx

對於 ,longtable您可以設定相同的值,但是,對於第二列,我們必須計算剩餘寬度。這是由 完成的\dimexpr\linewidth-4\tabcolsep-2cm\relax。要理解這一點,請將行想像為|tabcolsep||p{2cm}||tabcolsep||tabcolsep||p{length}||tabcolsep|,那麼,length應該等於\linewidth-2cm-4\tabcolsep

最後,我使用了包裝中的\toprule和來使桌子具有專業的外觀。\bottomrulebooktabs

\documentclass[letterpaper,11pt]{article}
\usepackage{tabularx,booktabs}
\usepackage{longtable}

\begin{document}

\noindent
\begin{tabularx}{\textwidth}{p{2cm} X}\toprule
entry 1 & bla bla bla bla bla \\
entry 2 & this is a tabular \\ \bottomrule
\end{tabularx}

\begin{longtable}{p{2cm} p{\dimexpr\linewidth-4\tabcolsep-2cm\relax}}\toprule
entry 3 & This is a longtable \\
entry 4 & with many more lines \\
entry 5 & but it isn't aligned  \\ \bottomrule
\end{longtable}

\end{document}

答案2

正如評論中提到的,longtable居中tabular是縮排。第 5 節包文件表示解決方案。透過新增\setlength\LTleft\parindent到序言中,longtable 將設定為向左對齊,但按通常的段落縮排。

\documentclass[letterpaper,11pt]{article}

\usepackage{longtable}
\setlength\LTleft\parindent

\begin{document}

\begin{tabular}{p{2cm} p{15cm}}
entry 1 & bla bla bla bla bla \\
entry 2 & this is a tabular
\end{tabular}

\begin{longtable}{p{2cm} p{15cm}}
entry 3 & This is a longtable \\
entry 4 & with many more lines \\
entry 5 & but it isn't aligned 
\end{longtable}

\end{document}

相關內容