这是我的练习表,是我在看了一些 YouTube 视频和阅读后写的wikibooks 表格教程。 这不会生成 pdf
\documentclass{article}
\usepackage{tabularx}
\usepackage{siunitx}
%\sisetup{
%round-mode = places,
%round-precision = 2,
%}
\usepackage{booktabs}
\usepackage{array}
\begin{document}
\begin{table}[h!]
\begin{center}
\begin{tabularx}{| @{}X | @{}X | @{}X |}
\hline
\textbf{Value 1} & \textbf{Value 2} & \textbf{Value 3}\\
$\alpha$ & $\beta$ & $\gamma$ \\
\hline
1 & 1110.1 & a \\
2 & 10.1 & b \\
3 & 23.113231 & c \\
\hline
\end{tabularx}
\end{center}
\caption{Your first label}
\label{tab:table1}
\end{table}
Second table
\begin{table} [h!]
\begin{center}
\begin{tabular}{|r | l |}
\hline
7C0 & hexadecimal \\
3700 & octal \\
\cline{2-2} 11111000000 & binary \\
\hline
\end{tabular}
\end{center}
\caption{Your second label}
\label{tab:table2}
\end{table}
\begin{tabular}{cc}
boring cell content & \parbox[t]{5cm}{rather long par\\new par}
\end{tabular}
\begin{tabular}{c c}
\hline
\begin{verbatim}
code
\end{verbatim}
& description
\\ \hline
\end{tabular}
\end{document}
我收到以下错误
line 15: Missing number, treated as zero. \begin{tabularx}{| @{}X | @{}X | @{}X |}
line 15: Illegal unit of measure (pt inserted). \begin{tabularx}{| @{}X | @{}X | @{}X |}
line 24: Package array Error: Illegal pream-token (\hline): `c' used. \end{tabularx}
line 24: Extra alignment tab has been changed to \cr. \end{tabularx}
line 24: Extra alignment tab has been changed to \cr. \end{tabularx}
line 24: Extra alignment tab has been changed to \cr. \end{tabularx}
line 24: Extra alignment tab has been changed to \cr. \end{tabularx}
line 24: Extra alignment tab has been changed to \cr. \end{tabularx}
line 24: Extra alignment tab has been changed to \cr. \end{tabularx}
line 24: Extra alignment tab has been changed to \cr. \end{tabularx}
line 24: Extra alignment tab has been changed to \cr. \end{tabularx}
line 24: Extra alignment tab has been changed to \cr. \end{tabularx}
line 24: Extra alignment tab has been changed to \cr. \end{tabularx}
line 24: Package array Error: Illegal pream-token (\hline): `c' used. \end{tabularx}
line 24: Extra alignment tab has been changed to \cr. \end{tabularx}
line 24: Extra alignment tab has been changed to \cr. \end{tabularx}
line 24: Extra alignment tab has been changed to \cr. \end{tabularx}
line 24: Extra alignment tab has been changed to \cr. \end{tabularx}
line 24: Extra alignment tab has been changed to \cr. \end{tabularx}
line 24: Extra alignment tab has been changed to \cr. \end{tabularx}
line 24: Extra alignment tab has been changed to \cr. \end{tabularx}
line 24: Extra alignment tab has been changed to \cr. \end{tabularx}
line 24: Extra alignment tab has been changed to \cr. \end{tabularx}
line 24: Extra alignment tab has been changed to \cr. \end{tabularx}
line 51: Something's wrong--perhaps a missing \item. \begin{verbatim}
line 51: Something's wrong--perhaps a missing \item. \begin{verbatim}
line 51: Something's wrong--perhaps a missing \item. \begin{verbatim}
line 51: Missing \endgroup inserted. \begin{verbatim}
line 51: Missing \endgroup inserted. \begin{verbatim}
line 51: Missing } inserted. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Missing } inserted. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 51: Missing \cr inserted. \begin{verbatim}
line 51: Misplaced \cr. \begin{verbatim}
line 24: X Columns too narrow (table too wide)(tabularx)
line 24: Overfull \hbox (54.77792pt too wide) in alignment
因为我正在尝试学习,所以我想知道我在这段代码中犯了什么概念性错误。
答案1
如前所述,您收到的错误消息通知您缺少以下宽度tabularx
:
正确的语法是\begin{tabularx}{>table width here<}{>column specifiers here<}
。例如:\begin{tabularx}{\textwidth}{XXX}
。
由于您在之前的评论中提到您对美观的表格感兴趣,因此我添加了一些分步示例,说明如何改进第一个表格的布局。代码包含一些有用的注释,以解释更改及其原因。
正如前面提到的,该示例包含另一个错误:verbatim
环境只能在p
(或派生的)列类型中使用。我也已更正了这一点。
\documentclass{article}
\usepackage{tabularx}
\usepackage{siunitx}
%\sisetup{
%round-mode = places,
%round-precision = 2,
%}
\usepackage{booktabs}
\usepackage{array}
\begin{document}
\begin{table}[h!]
\begin{center}
\begin{tabularx}{\textwidth}{| @{}X | @{}X | @{}X |} % added missing width argument \textwidth here
\hline
\textbf{Value 1} & \textbf{Value 2} & \textbf{Value 3}\\
$\alpha$ & $\beta$ & $\gamma$ \\
\hline
1 & 1110.1 & a \\
2 & 10.1 & b \\
3 & 23.113231 & c \\
\hline
\end{tabularx}
\end{center}
\caption{syntax corrected}
\label{tab:table1a}
\end{table}
\begin{table}[h!]
\centering % removed center environment and used centering command instead. The former adds vertical white space that might be undesired
\begin{tabularx}{\textwidth}{| X | X | X |} % Also removed @{} to prevent overlap of the text with the vertical lines
\hline
\textbf{Value 1} & \textbf{Value 2} & \textbf{Value 3}\\
$\alpha$ & $\beta$ & $\gamma$ \\
\hline
1 & 1110.1 & a \\
2 & 10.1 & b \\
3 & 23.113231 & c \\
\hline
\end{tabularx}
\caption{removed overlap with vertical lines}
\label{tab:table1b}
\end{table}
\begin{table}[h!]
\centering
\begin{tabularx}{\textwidth}{X X X} % Entirely removed vertical lines
\toprule % replaced \hline with booktabs line
\textbf{Value 1} & \textbf{Value 2} & \textbf{Value 3}\\
$\alpha$ & $\beta$ & $\gamma$ \\
\midrule % replaced \hline with booktabs line
1 & 1110.1 & a \\
2 & 10.1 & b \\
3 & 23.113231 & c \\
\bottomrule % replaced \hline with booktabs line
\end{tabularx}
\caption{removed vertical lines and used horizontal lines from booktabs}
\label{tab:table1c}
\end{table}
\begin{table}[h!]
\centering
\begin{tabular}{ccc} % replaced tabularx with a regular tabular. There is no need to artificially make the table wider
\toprule
\textbf{Value 1} & \textbf{Value 2} & \textbf{Value 3}\\
$\alpha$ & $\beta$ & $\gamma$ \\
\midrule
1 & 1110.1 & a \\
2 & 10.1 & b \\
3 & 23.113231 & c \\
\bottomrule
\end{tabular}
\caption{replaced tabularx with tabular. no need to make the table wider}
\label{tab:table1d}
\end{table}
\begin{table}[h!]
\centering
\begin{tabular}{cS[table-format=4.6] c} % rreplacesd second column with S type column from siunits to improve alignment of numbers
\toprule
\textbf{Value 1} & \textbf{Value 2} & \textbf{Value 3}\\
$\alpha$ & {$\beta$} & $\gamma$ \\ % adde {} around {$\beta$} because of non-number in S type column
\midrule
1 & 1110.1 & a \\
2 & 10.1 & b \\
3 & 23.113231 & c \\
\bottomrule
\end{tabular}
\caption{used siunitx for second column}
\label{tab:table1d}
\end{table}
\pagebreak
Second table
\begin{table} [h!]
\centering % see table 2
\begin{tabular}{|r | l |}
\hline
7C0 & hexadecimal \\
3700 & octal \\
\cline{2-2} 11111000000 & binary \\
\hline
\end{tabular}
\caption{Your second label}
\label{tab:table2}
\end{table}
\begin{tabular}{cc}
boring cell content & \parbox[t]{5cm}{rather long par\\new par}
\end{tabular}
\bigskip % just for some vertical space in the output. Do not use in actual document!
\begin{tabular}{cp{5cm}} % use a p type column here
boring cell content & rather long par new par \\ % no need for parbox in every cell (automatic linebreak here)
boring cell content & rather \newline long text par new par % no need for parbox in every cell (manual linebreak)
\end{tabular}
\bigskip % just for some vertical space in the output. Do not use in actual document!
\begin{tabular}{p{3cm} c} % verbatim only works in a p type column. Adjust the width to your needs
\hline
\begin{verbatim}
code
\end{verbatim}
& description
\\ \hline
\verb|code| % for shorter code, use verb. For more improvement give more information about the nature of the code. More advanced packages are listings and minted. Which to use depends on the requirements and the desired output.
& description
\\ \hline
\end{tabular}
\end{document}