我必須製作圖中的表格,但出現了一些錯誤。你能解釋一下我的錯誤是什麼嗎? (我沒有將數字放入程式碼中......我只想了解這是如何工作的)
\begin{document}
\begin{tabularx} {\textwidth} {cccccc}
\bottomrule
Regione & Assistiti & di cui over 65 & di cui under 65 & % anziani & % under 65
\midrule
Italia & x & x & x & x& x //
Nord & x & x & x & x& x //
Centro & x & x & x & x& x //
Sud & x & x & x & x& x //
Basilicata & x & x & x & x& x //
\bottomrule
end{tabularx}
\end{document}
答案1
正如我們在評論中所說:
- 您應該使用
\\
, 而不是//
在表格行的 和 處 %
\%
是 LaTeX 的特殊字符,如果將其用作百分比,則應該寫入,否則 LaTeX 會將後面的內容%
視為註釋- 在表格頂部,出於間距原因,您應該使用
\toprule
, 而不是\bottomrule
(這就是他們以這種方式命名的原因) tabularx
如果您沒有列類型,則不應使用X
。
為了更好地解釋最後一點,請看以下範例:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{tabularx}
\begin{document}
In the following example, since the table is longer than the line width
(we have an \verb|Overfull \hbox| error), the lines stop before the text
ends:
\begin{center}
\begin{tabularx}{\linewidth}{cc}
\toprule
Don't try this at home, this is wrong! & Non fatelo, è sbagliato!\\
\midrule
Example of wrong use of \texttt{tabularx} & Esempio di un uso sbagliato di \texttt{tabularx}\\
\bottomrule
\end{tabularx}
\end{center}
In the following example, since the table is too much shorter than the
line width, the lines go too much ahead w.r.t. the text:
\begin{center}
\begin{tabularx}{\linewidth}{cc}
\toprule
This is wrong! & Questo è sbagliato!\\
\midrule
Wrong use & Uso sbagliato\\
\bottomrule
\end{tabularx}
\end{center}
Whereas, with \verb|X| columns:
\begin{center}
\begin{tabularx}{\linewidth}{XX}
\toprule
Try this at home, this is correct! & Questo si può fare, è giusto!\\
\midrule
Example of correct use of \texttt{tabularx} & Esempio di un uso corretto di \texttt{tabularx}\\
\bottomrule
\end{tabularx}
\end{center}
\begin{center}
\begin{tabularx}{\linewidth}{XX}
\toprule
This is correct! & Questo è corretto!\\
\midrule
Correct use & Uso corretto\\
\bottomrule
\end{tabularx}
\end{center}
\end{document}
\footnotesize
應用所有這些,這是一個工作代碼(因為您的表格大於我添加的用於縮小字體的線寬)。
對於對齊方式,請注意這l
意味著左、r
右和c
居中。
\documentclass{article}
\usepackage{booktabs}
\begin{document}
\begin{center}
\footnotesize\begin{tabular}{lrrrrr}
\toprule
Regione & Assistiti & di cui over 65 & di cui under 65 & \% anziani & \% under 65\\
\midrule
Italia & x & x & x & x& x \\
Nord & x & x & x & x& x \\
Centro & x & x & x & x& x \\
Sud & x & x & x & x& x \\
Basilicata & x & x & x & x& x \\
\bottomrule
\end{tabular}
\end{center}
\end{document}
當然還有其他解決方案,這裡以tabularx
.由於預設情況下該X
列是向右參差不齊的,因此我建立了一個新的列類型R
以使列向左參差不齊。
*5R
是 的快捷方式RRRRR
。
\documentclass{article}
\usepackage{array}
\usepackage{tabularx}
\usepackage{booktabs}
\newcolumntype{R}{>{\raggedleft\arraybackslash}X}
\begin{document}
\begin{tabularx}{\linewidth}{l*5R}
\toprule
Regione & Assistiti & di cui over 65 & di cui under 65 & \% anziani & \% under 65\\
\midrule
Italia & x & x & x & x& x \\
Nord & x & x & x & x& x \\
Centro & x & x & x & x& x \\
Sud & x & x & x & x& x \\
Basilicata & x & x & x & x& x \\
\bottomrule
\end{tabularx}
\end{document}
答案2
考慮下面問題的所有評論......並添加一些我的建議:
\documentclass{article}
\usepackage{adjustbox}
%-------------------------------- show page layout, only for test
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{booktabs}
\usepackage{siunitx}
\begin{document}
\begin{table}[htb]
\sisetup{group-minimum-digits=4,
group-separator={.},
output-decimal-marker={,}}
\setlength\tabcolsep{0pt}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}
l
*{2}{S[table-format=6.0]}
S[table-format=5.0]
*{2}{S[table-format=2.2]}
}
\toprule
Regione & {Assistiti} & {di cui over 65} & {di cui under 65} & {\% anziani} & {\% under 65} \\
\midrule
Italia & 123456 & 12345 & 1234 & 12,34 & 12,34 \\
Nord & 12345 & 123456 & 123 & 12,34 & 12,34 \\
Centro & 123456 & 12345 & 12345 & 12,34 & 12,34 \\
Sud & 123456 & 12345 & 1234 & 12,34 & 12,34 \\
\bottomrule
\end{tabular*}
\end{table}
\end{document}
新增的紅線僅用於顯示頁面佈局。
答案3
嘗試這個
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}
\begin{document}
\begin{tabularx} {\textwidth} {cccccc}
\hline
Regione & Assistiti & di cui over 65 & di cui under 65 & \% anziani & \% under 65 \\
\hline
Italia & x & x & x & x & x \\
Nord & x & x & x & x & x \\
Centro & x & x & x & x & x \\
Sud & x & x & x & x & x \\
Basilicata & x & x & x & x & x \\
\hline
\end{tabularx}
\end{document}