\newcolumntype{Y}{>{\raggedleft\arraybackslash}X} 未定义控制序列

\newcolumntype{Y}{>{\raggedleft\arraybackslash}X} 未定义控制序列

我是 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,并添加表格型书签,您的示例编译时没有错误:

\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}

还要注意 center-environment 会在表格前后添加空间。此外,由于标签和源引用是单独的文本行,因此最终可能会出现其中一个项目排版在单独的页面上的情况。在表格前添加几行文本以查看效果。

您可以通过多种方式避免这种情况。一种快速而粗略的解决方案是将标签和源引用放在表格内,添加新的第一行和最后一行。使用\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环境或一个盒子中。

也许更优雅的解决方案是使用包三部分表或者,如果您接受表格作为浮动对象,则将表格放置在表格环境中。

使用 threeparttable

这是使用 的 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}

在此处输入图片描述

相关内容