[Tex/LaTex] \newcolumntype{Y}{>{\raggedleft\arraybackslash}X} undefined control sequence

errors

I'm new in LaTex and I need some help. I used tabout in Stata to generate a summary table. Using the code here I get the following error message:

undefined control sequence
\newcolumntype{Y}{>{\raggedleft\arraybackslash}X}

Here the code I used:

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

Best Answer

Modifying your code

If you change your definition of R-columntype to Y, and add tabularx and booktabs, your example compiles without errors:

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

Also be aware that the center-environment adds space before and after the table. In addition, since the label and source reference are separate text line, you may end up that one of those items is typeset on a separate page. Add some lines text before the table to see the effect.

You may avoid that effect in several way. A quick and dirty solution is to put the label and the source reference inside the table, adding new first and last rows. Use \multicolumn to span all cells into one table row:

Label:

\multicolumn{5}{@{}c}{\normalsize\bfseries Table 9: A oneway table}\\\addlinespace[2ex]

Source reference:

\bottomrule\addlinespace[0.5ex]
\multicolumn{5}{@{}l}{\scriptsize{Source: nlsw88.dta}}\\

See updated MWE.

You can also put the whole center-environment including the table in a minipage-environment, or a box.

Perhaps a more elegant solution is to use the package threeparttable, or, if you accept table as floats, place the table inside a table environment.

Using threeparttable

Here is an MWE using threeparttable. I have added some text above and below to demonstrated the setup. If you enclose all your table in a threeparttable with a caption, you will have automatic numbering and same formatting for all. Add labels after the captions, and you may automatically refer to the tables by number in your text:

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

enter image description here