我是 LaTex 新手,需要一些幫助。我在Stata中使用tabout來產生匯總表。使用此處的程式碼我收到以下錯誤訊息:
未定義的控制序列
\newcolumntype{Y}{>{\raggedleft\arraybackslash}X}
這是我使用的程式碼:
\documentclass[11pt]{article}
\begin{document}
\begin{center}
\textbf{Table 9: A oneway table} \par \vspace{2ex}
\footnotesize
\newcolumntype{R}{>{\raggedleft\arraybackslash}X}
\begin{tabularx} {11cm} {@{} l Y Y Y Y @{}}
\toprule
& Count & Col \% & Cum \% & Sample \\
\midrule
\textbf{Industry} \\
Ag/Forestry/Fisheries & 84 & 0.8 & 0.8 & 17 \\
Mining & 14 & 0.1 & 0.9 & 4 \\
Construction & 160 & 1.4 & 2.3 & 29 \\
Manufacturing & 1,848 & 16.6 & 18.9 & 367 \\
Transport/Comm/Utility & 433 & 3.9 & 22.8 & 90 \\
Wholesale/Retail Trade & 1,685 & 15.1 & 37.9 & 333 \\
Finance/Ins/Real Estate & 970 & 8.7 & 46.7 & 192 \\
Business/Repair Svc & 429 & 3.9 & 50.5 & 86 \\
Personal Services & 472 & 4.2 & 54.8 & 97 \\
Entertainment/Rec Svc & 99 & 0.9 & 55.6 & 17 \\
Professional Services & 4,151 & 37.3 & 92.9 & 824 \\
Public Administration & 786 & 7.1 & 100.0 & 176 \\
Total & 11,129 & 100.0 & & 2,232 \\
\midrule
\textbf{Occupation} \\
Professional/technical & 1,477 & 13.2 & 13.2 & 317 \\
Managers/admin & 1,322 & 11.8 & 25.1 & 264 \\
Sales & 3,626 & 32.5 & 57.5 & 726 \\
Clerical/unskilled & 511 & 4.6 & 62.1 & 102 \\
Craftsmen & 239 & 2.1 & 64.2 & 53 \\
Operatives & 1,305 & 11.7 & 75.9 & 246 \\
Transport & 136 & 1.2 & 77.1 & 28 \\
Laborers & 1,491 & 13.4 & 90.5 & 286 \\
Farmers & 8 & 0.1 & 90.5 & 1 \\
Farm laborers & 40 & 0.4 & 90.9 & 9 \\
Service & 75 & 0.7 & 91.6 & 16 \\
Household workers & 3 & 0.0 & 91.6 & 2 \\
Other & 938 & 8.4 & 100.0 & 187 \\
Total & 11,171 & 100.0 & & 2,237 \\
\bottomrule
\end{tabularx}
\par\smallskip\noindent\parbox{11cm}{\raggedright \scriptsize Source: nlsw88.dta}
\normalsize
\end{center}
\end{document}
答案1
修改你的程式碼
如果將 R-columntype 的定義更改為 Y,並添加表格x和書本標籤,您的範例編譯時沒有錯誤:
\documentclass[11pt]{article}
\usepackage{tabularx, booktabs} %% Load packages that you use
\begin{document}
\begin{center}
%%\textbf{Table 9: A oneway table} \par \vspace{2ex} %% To avoid label on separate page
\footnotesize
\newcolumntype{Y}{>{\raggedleft\arraybackslash}X} %% You defined R, but used Y
\begin{tabularx} {11cm} {@{} l Y Y Y Y @{}}
\multicolumn{5}{@{}c}{\normalsize\bfseries Table 9: A oneway table}\\\addlinespace[2ex] %% Label as part of the table
\toprule
& Count & Col \% & Cum \% & Sample \\
\midrule
\textbf{Industry} \\
Ag/Forestry/Fisheries & 84 & 0.8 & 0.8 & 17 \\
Mining & 14 & 0.1 & 0.9 & 4 \\
Construction & 160 & 1.4 & 2.3 & 29 \\
Manufacturing & 1,848 & 16.6 & 18.9 & 367 \\
Transport/Comm/Utility & 433 & 3.9 & 22.8 & 90 \\
Wholesale/Retail Trade & 1,685 & 15.1 & 37.9 & 333 \\
Finance/Ins/Real Estate & 970 & 8.7 & 46.7 & 192 \\
Business/Repair Svc & 429 & 3.9 & 50.5 & 86 \\
Personal Services & 472 & 4.2 & 54.8 & 97 \\
Entertainment/Rec Svc & 99 & 0.9 & 55.6 & 17 \\
Professional Services & 4,151 & 37.3 & 92.9 & 824 \\
Public Administration & 786 & 7.1 & 100.0 & 176 \\
Total & 11,129 & 100.0 & & 2,232 \\
\midrule
\textbf{Occupation} \\
Professional/technical & 1,477 & 13.2 & 13.2 & 317 \\
Managers/admin & 1,322 & 11.8 & 25.1 & 264 \\
Sales & 3,626 & 32.5 & 57.5 & 726 \\
Clerical/unskilled & 511 & 4.6 & 62.1 & 102 \\
Craftsmen & 239 & 2.1 & 64.2 & 53 \\
Operatives & 1,305 & 11.7 & 75.9 & 246 \\
Transport & 136 & 1.2 & 77.1 & 28 \\
Laborers & 1,491 & 13.4 & 90.5 & 286 \\
Farmers & 8 & 0.1 & 90.5 & 1 \\
Farm laborers & 40 & 0.4 & 90.9 & 9 \\
Service & 75 & 0.7 & 91.6 & 16 \\
Household workers & 3 & 0.0 & 91.6 & 2 \\
Other & 938 & 8.4 & 100.0 & 187 \\
Total & 11,171 & 100.0 & & 2,237 \\
\bottomrule
\multicolumn{5}{@{}l}{\scriptsize{Source: nlsw88.dta}}\\
\end{tabularx}
%% \par\smallskip\noindent\parbox{11cm}{\raggedright \scriptsize Source: nlsw88.dta} %% to avoid source reference on a separate page
\normalsize
\end{center}
\end{document}
另請注意,中心環境會在桌子前後增加空間。此外,由於標籤和來源參考是單獨的文字行,因此您可能最終會在單獨的頁面上排版其中一項。在表格前面添加一些行文字以查看效果。
您可以透過多種方式避免這種影響。一個快速但骯髒的解決方案是將標籤和來源參考放在表中,並添加新的第一行和最後一行。用於\multicolumn
將所有儲存格跨入一個表格行:
標籤:
\multicolumn{5}{@{}c}{\normalsize\bfseries Table 9: A oneway table}\\\addlinespace[2ex]
來源參考:
\bottomrule\addlinespace[0.5ex]
\multicolumn{5}{@{}l}{\scriptsize{Source: nlsw88.dta}}\\
請參閱更新的 MWE。
您也可以將整個中心環境(包括表)放在一個minipage
環境或一個盒子中。
也許更優雅的解決方案是使用包三部分錶,或者,如果您接受表格作為浮點數,請將表格放置在表格環境中。
使用三部分錶
這是一個使用 的 MWE threeparttable
。我在上面和下面添加了一些文字來演示設定。如果您將所有表格放在一個threeparttable
帶有標題的表格中,則所有表格都會自動編號和相同的格式。添加標籤後標題,您可以在文字中自動按編號引用表格:
\documentclass[11pt]{article}
\usepackage{tabularx, booktabs, threeparttable, caption}
\captionsetup{font=bf, aboveskip=1ex, belowskip=2ex}
\newcolumntype{Y}{>{\raggedleft\arraybackslash}X}
\begin{document}
Text before \emph{threeparttable}. Even more text. Even more text.Even more text.Even more text.Even more text.Even more text.Even more text.Even more text.Even more text.Even more text.Even more text. Even more text. See table~\ref{table:onewaytable}:
\begingroup
\centering
\begin{threeparttable}[b]
\caption{A oneway table}
\label{table:onewaytable} %% Add a label after the caption
\footnotesize
\begin{tabularx} {11cm} {@{} l Y Y Y Y @{}}
\toprule
& Count & Col \% & Cum \% & Sample \\
\midrule
\textbf{Industry} \\
Ag/Forestry/Fisheries & 84 & 0.8 & 0.8 & 17 \\
Mining & 14 & 0.1 & 0.9 & 4 \\
Construction & 160 & 1.4 & 2.3 & 29 \\
Manufacturing & 1,848 & 16.6 & 18.9 & 367 \\
Transport/Comm/Utility & 433 & 3.9 & 22.8 & 90 \\
Wholesale/Retail Trade & 1,685 & 15.1 & 37.9 & 333 \\
Finance/Ins/Real Estate & 970 & 8.7 & 46.7 & 192 \\
Business/Repair Svc & 429 & 3.9 & 50.5 & 86 \\
Personal Services & 472 & 4.2 & 54.8 & 97 \\
Entertainment/Rec Svc & 99 & 0.9 & 55.6 & 17 \\
Professional Services & 4,151 & 37.3 & 92.9 & 824 \\
Public Administration & 786 & 7.1 & 100.0 & 176 \\
Total & 11,129 & 100.0 & & 2,232 \\
\midrule
\textbf{Occupation} \\
Professional/technical & 1,477 & 13.2 & 13.2 & 317 \\
Managers/admin & 1,322 & 11.8 & 25.1 & 264 \\
Sales & 3,626 & 32.5 & 57.5 & 726 \\
Clerical/unskilled & 511 & 4.6 & 62.1 & 102 \\
Craftsmen & 239 & 2.1 & 64.2 & 53 \\
Operatives & 1,305 & 11.7 & 75.9 & 246 \\
Transport & 136 & 1.2 & 77.1 & 28 \\
Laborers & 1,491 & 13.4 & 90.5 & 286 \\
Farmers & 8 & 0.1 & 90.5 & 1 \\
Farm laborers & 40 & 0.4 & 90.9 & 9 \\
Service & 75 & 0.7 & 91.6 & 16 \\
Household workers & 3 & 0.0 & 91.6 & 2 \\
Other & 938 & 8.4 & 100.0 & 187 \\
Total & 11,171 & 100.0 & & 2,237 \\
\bottomrule
\end{tabularx}
\scriptsize
\begin{tablenotes}[flushleft]
\item Source: nlsw88.dta
\end{tablenotes}\smallskip
\end{threeparttable}%
\endgroup
More text. Even more text. Even more text.Even more text.Even more text.Even more text.Even more text.Even more text.Even more text.Even more text.Even more text.
\end{document}